Skip to content

Add src/index.js tests using Webpack to resolve Stimulus for node #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/webpack/create-controllers-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
'use strict';

module.exports = function createControllersVirtualModule(config) {
var file = 'module.exports = {';
var file = 'export default {';

if ('undefined' === typeof config['controllers']) {
throw new Error('Your Stimulus configuration file (assets/controllers.json) lacks a "controllers" key.');
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"scripts": {
"build": "babel src -d dist",
"test": "babel src -d dist && jest"
"test": "webpack --config test/webpack.config.js && jest"
},
"peerDependencies": {
"stimulus": "^2.0"
Expand All @@ -24,9 +24,12 @@
"@babel/core": "^7.12.3",
"@babel/plugin-proposal-class-properties": "^7.12.1",
"@babel/preset-env": "^7.12.7",
"@symfony/controllers": "file:test/fixtures/controllers",
"@symfony/mock-module": "file:test/fixtures/module",
"@symfony/stimulus-testing": "^1.0.0",
"stimulus": "^2.0"
"stimulus": "^2.0",
"webpack": "^5.11.1",
"webpack-cli": "^4.3.0"
},
"jest": {
"testRegex": "test/.*\\.test.js",
Expand Down
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ import symfonyControllers from '@symfony/controllers';

export function startStimulusApp(context) {
const application = Application.start();
application.load(definitionsFromContext(context));

if (context) {
application.load(definitionsFromContext(context));
}

for (let controllerName in symfonyControllers) {
if (!symfonyControllers.hasOwnProperty(controllerName)) {
Expand Down
2 changes: 2 additions & 0 deletions test/dist/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
3 changes: 3 additions & 0 deletions test/fixtures/controllers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
'@symfony/mock-module/mock-controller': import('@symfony/mock-module/dist/controller.js'),
};
5 changes: 5 additions & 0 deletions test/fixtures/controllers/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "@symfony/controllers",
"version": "1.0.0",
"main": "./index.js"
}
17 changes: 15 additions & 2 deletions test/fixtures/module/dist/controller.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
module.exports = class {
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

'use strict';

import { Controller } from 'stimulus';

export default class extends Controller {
connect() {}
};
}
1 change: 0 additions & 1 deletion test/fixtures/module/dist/style.css

This file was deleted.

1 change: 0 additions & 1 deletion test/fixtures/module/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"name": "@symfony/mock-module",
"license": "MIT",
"version": "1.0.0",
"symfony": {
"controllers": {
Expand Down
24 changes: 24 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* This file is part of the Symfony Webpack Encore package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

'use strict';

const startStimulusApp = require('./dist/index').startStimulusApp;

describe('startStimulusApp', () => {
it('must start the app', async () => {
const app = startStimulusApp();

// Wait for controllers to be loaded
await new Promise(setImmediate);

expect(app.router.modules.length).toBe(1);
expect(app.router.modules[0].definition.identifier).toBe('@symfony/mock-module/mock-controller');
});
});
29 changes: 29 additions & 0 deletions test/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

'use strict';

/**
* Webpack config used to build src/index.js with Stimulus embedded
* to allow testing in node.
*/

const path = require('path');

module.exports = {
target: 'node',
mode: 'development',
devtool: 'source-map',
entry: './src/index.js',
output: {
filename: 'index.js',
path: path.resolve(__dirname, 'dist'),
libraryTarget: 'commonjs2'
},
};