Skip to content

Commit 8a81bb4

Browse files
committed
minor #10 Add src/index.js tests using Webpack to resolve Stimulus for node (tgalopin)
This PR was merged into the main branch. Discussion ---------- Add src/index.js tests using Webpack to resolve Stimulus for node Commits ------- b02695b Add src/index.js tests using Webpack to resolve Stimulus for node
2 parents 79dfb04 + b02695b commit 8a81bb4

File tree

11 files changed

+88
-8
lines changed

11 files changed

+88
-8
lines changed

dist/webpack/create-controllers-module.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
'use strict';
1010

1111
module.exports = function createControllersVirtualModule(config) {
12-
var file = 'module.exports = {';
12+
var file = 'export default {';
1313

1414
if ('undefined' === typeof config['controllers']) {
1515
throw new Error('Your Stimulus configuration file (assets/controllers.json) lacks a "controllers" key.');

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
},
1111
"scripts": {
1212
"build": "babel src -d dist",
13-
"test": "babel src -d dist && jest"
13+
"test": "webpack --config test/webpack.config.js && jest"
1414
},
1515
"peerDependencies": {
1616
"stimulus": "^2.0"
@@ -24,9 +24,12 @@
2424
"@babel/core": "^7.12.3",
2525
"@babel/plugin-proposal-class-properties": "^7.12.1",
2626
"@babel/preset-env": "^7.12.7",
27+
"@symfony/controllers": "file:test/fixtures/controllers",
2728
"@symfony/mock-module": "file:test/fixtures/module",
2829
"@symfony/stimulus-testing": "^1.0.0",
29-
"stimulus": "^2.0"
30+
"stimulus": "^2.0",
31+
"webpack": "^5.11.1",
32+
"webpack-cli": "^4.3.0"
3033
},
3134
"jest": {
3235
"testRegex": "test/.*\\.test.js",

src/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ import symfonyControllers from '@symfony/controllers';
1515

1616
export function startStimulusApp(context) {
1717
const application = Application.start();
18-
application.load(definitionsFromContext(context));
18+
19+
if (context) {
20+
application.load(definitionsFromContext(context));
21+
}
1922

2023
for (let controllerName in symfonyControllers) {
2124
if (!symfonyControllers.hasOwnProperty(controllerName)) {

test/dist/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

test/fixtures/controllers/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
'@symfony/mock-module/mock-controller': import('@symfony/mock-module/dist/controller.js'),
3+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "@symfony/controllers",
3+
"version": "1.0.0",
4+
"main": "./index.js"
5+
}
Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1-
module.exports = class {
1+
/*
2+
* This file is part of the Symfony package.
3+
*
4+
* (c) Fabien Potencier <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
'use strict';
11+
12+
import { Controller } from 'stimulus';
13+
14+
export default class extends Controller {
215
connect() {}
3-
};
16+
}

test/fixtures/module/dist/style.css

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/fixtures/module/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"name": "@symfony/mock-module",
3-
"license": "MIT",
43
"version": "1.0.0",
54
"symfony": {
65
"controllers": {

test/index.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* This file is part of the Symfony Webpack Encore package.
3+
*
4+
* (c) Fabien Potencier <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
'use strict';
11+
12+
const startStimulusApp = require('./dist/index').startStimulusApp;
13+
14+
describe('startStimulusApp', () => {
15+
it('must start the app', async () => {
16+
const app = startStimulusApp();
17+
18+
// Wait for controllers to be loaded
19+
await new Promise(setImmediate);
20+
21+
expect(app.router.modules.length).toBe(1);
22+
expect(app.router.modules[0].definition.identifier).toBe('@symfony/mock-module/mock-controller');
23+
});
24+
});

test/webpack.config.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* This file is part of the Symfony package.
3+
*
4+
* (c) Fabien Potencier <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
'use strict';
11+
12+
/**
13+
* Webpack config used to build src/index.js with Stimulus embedded
14+
* to allow testing in node.
15+
*/
16+
17+
const path = require('path');
18+
19+
module.exports = {
20+
target: 'node',
21+
mode: 'development',
22+
devtool: 'source-map',
23+
entry: './src/index.js',
24+
output: {
25+
filename: 'index.js',
26+
path: path.resolve(__dirname, 'dist'),
27+
libraryTarget: 'commonjs2'
28+
},
29+
};

0 commit comments

Comments
 (0)