Skip to content

Commit ea5dc8e

Browse files
authored
fix(preprocess): auto fix missing webpack framework (#471)
this is an issue that comes up often. As this is something that karma-webpack needs to function, we will automatically fix the configuration on the fly and emit a warning that the change was made. Fixes N/A
1 parent b044404 commit ea5dc8e

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

lib/karma-webpack.js

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const fs = require('fs');
44
const glob = require('glob');
55
const minimatch = require('minimatch');
66

7+
const { ensureWebpackFrameworkSet } = require('./karma/karmaConfigValidator');
8+
79
const { hash } = require('./utils/hash');
810

911
const { KarmaWebpackController } = require('./KarmaWebpackController');
@@ -80,6 +82,8 @@ function configToWebpackEntries(config) {
8082
}
8183

8284
function preprocessorFactory(config, emitter) {
85+
ensureWebpackFrameworkSet(config);
86+
8387
// one time setup
8488
if (controller.isActive === false) {
8589
controller.updateWebpackOptions({

lib/karma/karmaConfigValidator.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function ensureWebpackFrameworkSet(karmaConfig) {
2+
if (!Array.isArray(karmaConfig.frameworks)) {
3+
karmaConfig.frameworks = [];
4+
}
5+
if (karmaConfig.frameworks.indexOf('webpack') === -1) {
6+
console.warn(
7+
'webpack was not included as a framework in karma configuration, setting this automatically...'
8+
);
9+
karmaConfig.frameworks.push('webpack');
10+
}
11+
}
12+
13+
module.exports = { ensureWebpackFrameworkSet };

test/integration/scenarios/basic-setup/basic-setup.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('A basic karma-webpack setup', () => {
2222
const TEST_PATH = path.resolve(__dirname, './index.scenario.js');
2323

2424
const config = {
25-
frameworks: ['webpack', 'mocha', 'chai'],
25+
frameworks: ['mocha', 'chai'],
2626
files: [{ pattern: TEST_PATH }],
2727
preprocessors: { [TEST_PATH]: ['webpack'] },
2828
webpack: {},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const {
2+
ensureWebpackFrameworkSet,
3+
} = require('../../../lib/karma/karmaConfigValidator');
4+
5+
describe('karmaConfigValidation', () => {
6+
describe('ensureWebpackFrameworkExists', () => {
7+
let config;
8+
beforeEach(() => (config = { frameworks: [] }));
9+
10+
it('should add webpack to the list of karma config frameworks if it did not already exist', () => {
11+
ensureWebpackFrameworkSet(config);
12+
expect(config.frameworks.length).toBe(1);
13+
expect(config.frameworks[0]).toBe('webpack');
14+
});
15+
16+
it('should add webpack to an existing list of frameworks', () => {
17+
config.frameworks = ['foo', 'bar'];
18+
ensureWebpackFrameworkSet(config);
19+
expect(config.frameworks.length).toBe(3);
20+
expect(config.frameworks).toContain('webpack');
21+
});
22+
it('should create a frameworks array if one does not exist', () => {
23+
delete config.frameworks;
24+
ensureWebpackFrameworkSet(config);
25+
expect(config.frameworks.length).toBe(1);
26+
expect(config.frameworks[0]).toBe('webpack');
27+
});
28+
});
29+
});

0 commit comments

Comments
 (0)