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

Commit

Permalink
Allow using Jest and Mocha presets without Babel (#379)
Browse files Browse the repository at this point in the history
  • Loading branch information
eliperelman authored Oct 26, 2017
1 parent 84088b1 commit ee2e7ba
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
32 changes: 16 additions & 16 deletions packages/neutrino-preset-jest/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function getFinalPath(path) {
join('<rootDir>', 'node_modules', path);
}

function normalizeJestOptions(opts, neutrino) {
function normalizeJestOptions(opts, neutrino, usingBabel) {
const aliases = neutrino.config.resolve.alias.entries() || {};
const moduleNames = Object
.keys(aliases)
Expand Down Expand Up @@ -57,13 +57,9 @@ function normalizeJestOptions(opts, neutrino) {
testRegex: join(basename(neutrino.options.tests), '.*(_test|_spec|\\.test|\\.spec)\\.jsx?$'),
transform: { [jsNames]: require.resolve('./transformer') },
globals: {
BABEL_OPTIONS: omit(
['cacheDirectory'],
neutrino.config.module
.rule('compile')
.use('babel')
.get('options')
)
BABEL_OPTIONS: usingBabel
? omit(['cacheDirectory'], neutrino.config.module.rule('compile').use('babel').get('options'))
: {}
}
},
opts
Expand All @@ -84,14 +80,18 @@ module.exports = (neutrino, opts = {}) => {
}));

neutrino.on('test', (args) => {
neutrino.use(loaderMerge('compile', 'babel'), {
env: {
test: {
retainLines: true,
presets: [require.resolve('babel-preset-jest')],
plugins: [require.resolve('babel-plugin-transform-es2015-modules-commonjs')]
const usingBabel = neutrino.config.module.rules.has('compile');

neutrino.config.when(usingBabel, () => {
neutrino.use(loaderMerge('compile', 'babel'), {
env: {
test: {
retainLines: true,
presets: [require.resolve('babel-preset-jest')],
plugins: [require.resolve('babel-plugin-transform-es2015-modules-commonjs')]
}
}
}
});
});

return new Promise((resolve, reject) => {
Expand All @@ -102,7 +102,7 @@ module.exports = (neutrino, opts = {}) => {
.command('test [files..]', 'Run tests', jestOptions)
.argv;
const configFile = join(tmpdir(), 'config.json');
const options = normalizeJestOptions(opts, neutrino);
const options = normalizeJestOptions(opts, neutrino, usingBabel);
const cliOptions = Object.assign(
jestArgs,
{
Expand Down
23 changes: 11 additions & 12 deletions packages/neutrino-preset-mocha/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,26 @@ const loaderMerge = require('neutrino-middleware-loader-merge');

module.exports = (neutrino, opts = {}) => {
neutrino.on('test', ({ files }) => {
const usingBabel = neutrino.config.module.rules.has('compile');
const options = merge.all([
{ reporter: 'spec', ui: 'tdd', bail: true },
opts,
files.length ? { recursive: true } : {}
]);

neutrino.use(loaderMerge('compile', 'babel'), {
env: {
test: {
plugins: [require.resolve('babel-plugin-transform-es2015-modules-commonjs')]
neutrino.config.when(usingBabel, () => {
neutrino.use(loaderMerge('compile', 'babel'), {
env: {
test: {
plugins: [require.resolve('babel-plugin-transform-es2015-modules-commonjs')]
}
}
}
});
});

const babelOptions = omit(
['cacheDirectory'],
neutrino.config.module
.rule('compile')
.use('babel')
.get('options')
);
const babelOptions = usingBabel
? omit(['cacheDirectory'], neutrino.config.module.rule('compile').use('babel').get('options'))
: {};

return mocha(options, babelOptions, files);
});
Expand Down

0 comments on commit ee2e7ba

Please sign in to comment.