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

jest: Use babel-jest instead of a custom implementation #1183

Merged
merged 1 commit into from
Oct 21, 2018
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
9 changes: 3 additions & 6 deletions packages/jest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@
"yarn": ">=1.2.1"
},
"dependencies": {
"@babel/core": "^7.1.2",
"@babel/plugin-transform-modules-commonjs": "^7.1.0",
"@neutrinojs/compile-loader": "9.0.0-0",
"@neutrinojs/loader-merge": "9.0.0-0",
"babel-plugin-jest-hoist": "^23.2.0",
"babel-core": "^7.0.0-bridge",
"babel-jest": "^23.6.0",
"deepmerge": "^1.5.2",
"eslint-plugin-jest": "^21.25.0",
"lodash.omit": "^4.5.0"
"eslint-plugin-jest": "^21.25.0"
},
"peerDependencies": {
"jest": "^23.0.0",
Expand Down
46 changes: 5 additions & 41 deletions packages/jest/src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const loaderMerge = require('@neutrinojs/loader-merge');
const compileLoader = require('@neutrinojs/compile-loader');
const merge = require('deepmerge');
const omit = require('lodash.omit');
const { basename, isAbsolute, join, relative } = require('path');
const { media, style } = require('neutrino/extensions');

Expand All @@ -20,43 +18,12 @@ module.exports = (neutrino, options = {}) => {
});

neutrino.register('jest', (neutrino) => {
const usingBabel = neutrino.config.module.rules.has('compile');
const compileRule = neutrino.config.module.rules.get('compile');
const babelOptions = compileRule ? compileRule.use('babel').get('options') : {};
// Any parts of the babel config that are not serializable will be omitted, however
// that also occurs when passing to the custom transformer using `globals` instead.
process.env.JEST_BABEL_OPTIONS = JSON.stringify(babelOptions);

if (usingBabel) {
neutrino.config.module
.rule('compile')
.use('babel')
.tap(options => compileLoader.merge(options, {
plugins: [
// Once babel-preset-jest has better Babel 7 support we should
// switch back to it (or even use babel-jest, which will allow
// simplifying the transformer too):
// https://github.com/facebook/jest/issues/6126
// For now this plugin is taken from here (we don't need
// object-rest-spread since node >=8.3):
// https://github.com/facebook/jest/blob/v22.4.2/packages/babel-preset-jest/index.js#L11-L12
require.resolve('babel-plugin-jest-hoist'),
// Since the tests will be run by node which doesn't yet support
// ES2015 modules
require.resolve('@babel/plugin-transform-modules-commonjs')
]
}));
}

const babelOptions = usingBabel
? compileLoader.merge(
omit(
neutrino.config.module.rule('compile').use('babel').get('options'),
['cacheDirectory']
),
{
retainLines: true,
plugins: [
require.resolve('@babel/plugin-transform-modules-commonjs')
]
}
)
: {};
const getFinalPath = path => {
if (isAbsolute(path)) {
return path;
Expand Down Expand Up @@ -109,9 +76,6 @@ module.exports = (neutrino, options = {}) => {
// property name where a Regex object will cause issues. e.g.:
// https://github.com/neutrinojs/neutrino/issues/638.
[extensionsToNames(extensions)]: require.resolve('./transformer')
},
globals: {
BABEL_OPTIONS: babelOptions
}
}, options);
});
Expand Down
16 changes: 4 additions & 12 deletions packages/jest/src/transformer.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
const { transform } = require('@babel/core');
const { createTransformer } = require('babel-jest');

module.exports = {
// This is inspired by:
// https://github.com/facebook/jest/blob/v22.4.2/packages/babel-jest/src/index.js#L105-L147
// And is required due to:
// https://github.com/facebook/jest/issues/1468
// TODO: See if it would be easier to wrap the higher-level babel-jest instead.
process(src, filename, config) {
// Babel 7 returns null if the file was ignored.
return transform(src, { filename, ...config.globals.BABEL_OPTIONS }) || src;
}
};
const babelOptions = JSON.parse(process.env.JEST_BABEL_OPTIONS);

module.exports = createTransformer(babelOptions);
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1536,6 +1536,11 @@ babel-core@^6.0.0, babel-core@^6.26.0:
slash "^1.0.0"
source-map "^0.5.7"

babel-core@^7.0.0-bridge:
version "7.0.0-bridge.0"
resolved "https://registry.yarnpkg.com/babel-core/-/babel-core-7.0.0-bridge.0.tgz#95a492ddd90f9b4e9a4a1da14eb335b87b634ece"
integrity sha512-poPX9mZH/5CSanm50Q+1toVci6pv5KSRv/5TWCwtzQS5XEwn40BcCrgIeMFWP9CKKIniKXNxoIOnOq4VVlGXhg==

babel-eslint@^10.0.1:
version "10.0.1"
resolved "https://registry.yarnpkg.com/babel-eslint/-/babel-eslint-10.0.1.tgz#919681dc099614cd7d31d45c8908695092a1faed"
Expand Down