Skip to content
This repository was archived by the owner on Feb 18, 2024. It is now read-only.

Commit 4de016f

Browse files
authored
Support options for jest and karma presets in v9 (#1081)
* Support options for jest and karma presets in v9 * Add test for karma and jest merging options
1 parent c11b1c9 commit 4de016f

File tree

5 files changed

+38
-10
lines changed

5 files changed

+38
-10
lines changed

packages/jest/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"@neutrinojs/compile-loader": "9.0.0-0",
2828
"@neutrinojs/loader-merge": "9.0.0-0",
2929
"babel-plugin-jest-hoist": "^23.0.0",
30+
"deepmerge": "^1.5.2",
3031
"eslint-plugin-jest": "^21.21.0",
3132
"lodash.omit": "^4.5.0"
3233
},

packages/jest/src/index.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
const loaderMerge = require('@neutrinojs/loader-merge');
2-
const { merge } = require('@neutrinojs/compile-loader');
2+
const compileLoader = require('@neutrinojs/compile-loader');
3+
const merge = require('deepmerge');
34
const omit = require('lodash.omit');
45
const { basename, isAbsolute, join, relative } = require('path');
56
const { media, style } = require('neutrino/extensions');
67

7-
module.exports = neutrino => {
8+
module.exports = (neutrino, options = {}) => {
89
neutrino.config.when(neutrino.config.module.rules.has('lint'), () => {
910
neutrino.use(loaderMerge('lint', 'eslint'), {
1011
plugins: ['jest'],
@@ -25,7 +26,7 @@ module.exports = neutrino => {
2526
neutrino.config.module
2627
.rule('compile')
2728
.use('babel')
28-
.tap(options => merge(options, {
29+
.tap(options => compileLoader.merge(options, {
2930
plugins: [
3031
// Once babel-preset-jest has better Babel 7 support we should
3132
// switch back to it (or even use babel-jest, which will allow
@@ -43,7 +44,7 @@ module.exports = neutrino => {
4344
}
4445

4546
const babelOptions = usingBabel
46-
? merge(
47+
? compileLoader.merge(
4748
omit(
4849
neutrino.config.module.rule('compile').use('babel').get('options'),
4950
['cacheDirectory']
@@ -76,7 +77,7 @@ module.exports = neutrino => {
7677
const modulesConfig = neutrino.config.resolve.modules.values();
7778
const aliases = neutrino.config.resolve.alias.entries() || {};
7879

79-
return {
80+
return merge({
8081
rootDir: root,
8182
moduleDirectories: modulesConfig.length ? modulesConfig : ['node_modules'],
8283
moduleFileExtensions: neutrino.config.resolve.extensions
@@ -112,6 +113,6 @@ module.exports = neutrino => {
112113
globals: {
113114
BABEL_OPTIONS: babelOptions
114115
}
115-
};
116+
}, options);
116117
});
117118
};

packages/jest/test/jest_test.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ test('loads middleware', t => {
1515
});
1616

1717
test('uses middleware', t => {
18+
const api = new Neutrino();
19+
1820
t.notThrows(() => {
19-
const api = new Neutrino();
2021
api.use(mw());
2122
});
2223
});
@@ -66,3 +67,9 @@ test('exposes jest method', t => {
6667
test('exposes jest config from method', t => {
6768
t.is(typeof neutrino(mw()).jest(), 'object');
6869
});
70+
71+
test('uses middleware with options', t => {
72+
const config = neutrino([mw(), { testEnvironment: 'node' }]).jest();
73+
74+
t.is(config.testEnvironment, 'node');
75+
});

packages/karma/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const merge = require('deepmerge');
44
const omit = require('lodash.omit');
55
const { join } = require('path');
66

7-
module.exports = neutrino => {
7+
module.exports = (neutrino, options = {}) => {
88
if (neutrino.config.module.rules.has('lint')) {
99
neutrino.use(loaderMerge('lint', 'eslint'), {
1010
envs: ['mocha']
@@ -24,7 +24,7 @@ module.exports = neutrino => {
2424
const tests = join(neutrino.options.tests, '**/*_test.js');
2525
const sources = join(neutrino.options.source, '**/*.js*');
2626

27-
config.set({
27+
config.set(merge({
2828
basePath: neutrino.options.root,
2929
browsers: [process.env.CI ? 'ChromeCI' : 'ChromeHeadless'],
3030
customLaunchers: {
@@ -80,7 +80,7 @@ module.exports = neutrino => {
8080
{ type: 'lcov', subdir: 'report-lcov' }
8181
]
8282
}
83-
});
83+
}, options));
8484

8585
return config;
8686
});

packages/karma/test/karma_test.js

+19
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,22 @@ test('exposes karma config from method', t => {
7474

7575
t.is(config, fakeKarma);
7676
});
77+
78+
test('uses middleware with options', t => {
79+
// Karma's config handler returns a function.
80+
// Force evaluation by calling it.
81+
const fakeKarma = new Map();
82+
const config = neutrino([mw(), {
83+
webpackMiddleware: {
84+
stats: {
85+
errors: false
86+
}
87+
}
88+
}]).karma()(fakeKarma);
89+
90+
// Since we are faking out the Karma API with a Map, we need to get the
91+
// object back out of the map and check that the merge happened correctly.
92+
const [options] = [...config][0];
93+
94+
t.is(options.webpackMiddleware.stats.errors, false);
95+
});

0 commit comments

Comments
 (0)