This repository has been archived by the owner on Feb 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 214
/
index.js
82 lines (77 loc) · 2.91 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const merge = require('deepmerge');
const { basename, isAbsolute, join, relative } = require('path');
const { media, style } = require('neutrino/extensions');
module.exports = (neutrino, options = {}) => {
const lintRule = neutrino.config.module.rules.get('lint');
if (lintRule) {
lintRule.use('eslint').tap(
// Don't adjust the lint configuration for projects using their own .eslintrc.
lintOptions => lintOptions.useEslintrc
? lintOptions
: merge(lintOptions, {
baseConfig: {
extends: ['plugin:jest/recommended']
}
})
);
}
neutrino.register('jest', (neutrino) => {
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);
const getFinalPath = path => {
if (isAbsolute(path)) {
return path;
}
return path.startsWith('.')
? join('<rootDir>', path)
: join('<rootDir>', 'node_modules', path);
};
const extensionsToNames = extensions => `\\.(${extensions.join('|')})$`;
const {
extensions,
source,
tests,
root,
debug
} = neutrino.options;
const modulesConfig = neutrino.config.resolve.modules.values();
const aliases = neutrino.config.resolve.alias.entries() || {};
return merge({
rootDir: root,
moduleDirectories: modulesConfig.length ? modulesConfig : ['node_modules'],
moduleFileExtensions: neutrino.config.resolve.extensions
.values()
.map(extension => extension.replace('.', '')),
moduleNameMapper:
Object
.keys(aliases)
.reduce((mapper, key) => ({
...mapper,
[`^${key}$`]: `${getFinalPath(aliases[key])}$1`
}), {
[extensionsToNames(media)]: require.resolve('./file-mock'),
[extensionsToNames(style)]: require.resolve('./style-mock')
}),
bail: true,
collectCoverageFrom: [join(
relative(root, source),
`**/*.{${extensions.join(',')}}`
)],
testEnvironment: neutrino.config.get('target') === 'node' ? 'node' : 'jsdom',
testRegex: `${basename(tests)}/.*(_test|_spec|\\.test|\\.spec)\\.(${
extensions.join('|')
})$`,
verbose: debug,
transform: {
// neutrino.options.extensions should be used instead of
// neutrino.regexFromExtensions() because transformNames is used as a
// property name where a Regex object will cause issues. e.g.:
// https://github.com/neutrinojs/neutrino/issues/638.
[extensionsToNames(extensions)]: require.resolve('./transformer')
}
}, options);
});
};