Skip to content

Commit

Permalink
fix(preprocess): auto fix missing webpack framework (#471)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
codymikol authored Jan 30, 2021
1 parent b044404 commit ea5dc8e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/karma-webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const fs = require('fs');
const glob = require('glob');
const minimatch = require('minimatch');

const { ensureWebpackFrameworkSet } = require('./karma/karmaConfigValidator');

const { hash } = require('./utils/hash');

const { KarmaWebpackController } = require('./KarmaWebpackController');
Expand Down Expand Up @@ -80,6 +82,8 @@ function configToWebpackEntries(config) {
}

function preprocessorFactory(config, emitter) {
ensureWebpackFrameworkSet(config);

// one time setup
if (controller.isActive === false) {
controller.updateWebpackOptions({
Expand Down
13 changes: 13 additions & 0 deletions lib/karma/karmaConfigValidator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function ensureWebpackFrameworkSet(karmaConfig) {
if (!Array.isArray(karmaConfig.frameworks)) {
karmaConfig.frameworks = [];
}
if (karmaConfig.frameworks.indexOf('webpack') === -1) {
console.warn(
'webpack was not included as a framework in karma configuration, setting this automatically...'
);
karmaConfig.frameworks.push('webpack');
}
}

module.exports = { ensureWebpackFrameworkSet };
2 changes: 1 addition & 1 deletion test/integration/scenarios/basic-setup/basic-setup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('A basic karma-webpack setup', () => {
const TEST_PATH = path.resolve(__dirname, './index.scenario.js');

const config = {
frameworks: ['webpack', 'mocha', 'chai'],
frameworks: ['mocha', 'chai'],
files: [{ pattern: TEST_PATH }],
preprocessors: { [TEST_PATH]: ['webpack'] },
webpack: {},
Expand Down
29 changes: 29 additions & 0 deletions test/unit/util/karmaConfigValidation.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const {
ensureWebpackFrameworkSet,
} = require('../../../lib/karma/karmaConfigValidator');

describe('karmaConfigValidation', () => {
describe('ensureWebpackFrameworkExists', () => {
let config;
beforeEach(() => (config = { frameworks: [] }));

it('should add webpack to the list of karma config frameworks if it did not already exist', () => {
ensureWebpackFrameworkSet(config);
expect(config.frameworks.length).toBe(1);
expect(config.frameworks[0]).toBe('webpack');
});

it('should add webpack to an existing list of frameworks', () => {
config.frameworks = ['foo', 'bar'];
ensureWebpackFrameworkSet(config);
expect(config.frameworks.length).toBe(3);
expect(config.frameworks).toContain('webpack');
});
it('should create a frameworks array if one does not exist', () => {
delete config.frameworks;
ensureWebpackFrameworkSet(config);
expect(config.frameworks.length).toBe(1);
expect(config.frameworks[0]).toBe('webpack');
});
});
});

0 comments on commit ea5dc8e

Please sign in to comment.