forked from neutrinojs/neutrino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jest_test.js
168 lines (139 loc) · 4.24 KB
/
jest_test.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import path from 'path';
import test from 'ava';
import airbnbPreset from '../../airbnb';
import eslintPreset from '../../eslint';
import reactPreset from '../../react';
import compileMiddleware from '../../compile-loader';
import Neutrino from '../../neutrino/Neutrino';
import neutrino from '../../neutrino';
const mw = (...args) => require('..')(...args);
const originalNodeEnv = process.env.NODE_ENV;
test.afterEach(() => {
// Restore the original NODE_ENV after each test (which Ava defaults to 'test').
process.env.NODE_ENV = originalNodeEnv;
process.env.JEST_BABEL_OPTIONS = undefined;
});
test('loads middleware', (t) => {
t.notThrows(() => require('..'));
});
test('uses middleware', (t) => {
const api = new Neutrino();
t.notThrows(() => {
api.use(mw());
});
});
test('instantiates', (t) => {
const api = new Neutrino();
api.use(mw());
t.notThrows(() => api.config.toConfig());
});
test('instantiates in development', (t) => {
process.env.NODE_ENV = 'development';
const api = new Neutrino();
api.use(mw());
t.notThrows(() => api.config.toConfig());
});
test('instantiates in production', (t) => {
process.env.NODE_ENV = 'production';
const api = new Neutrino();
api.use(mw());
t.notThrows(() => api.config.toConfig());
});
test('exposes jest output handler', (t) => {
const api = new Neutrino();
api.use(mw());
const handler = api.outputHandlers.get('jest');
t.is(typeof handler, 'function');
});
test('exposes jest config from output', (t) => {
const config = neutrino(mw()).output('jest');
t.is(typeof config, 'object');
});
test('exposes jest method', (t) => {
t.is(typeof neutrino(mw()).jest, 'function');
});
test('exposes jest config from method', (t) => {
t.is(typeof neutrino(mw()).jest(), 'object');
});
test('uses middleware with options', (t) => {
const config = neutrino(mw({ testEnvironment: 'node' })).jest();
t.is(config.testEnvironment, 'node');
});
test('updates lint config by default', (t) => {
const api = new Neutrino();
api.use(airbnbPreset());
api.use(mw());
const options = api.config.module.rule('lint').use('eslint').get('options');
t.deepEqual(options.baseConfig.extends, [
require.resolve('eslint-config-airbnb'),
require.resolve('eslint-config-airbnb/hooks'),
'plugin:jest/recommended',
]);
});
test('does not update lint config if useEslintrc true', (t) => {
const api = new Neutrino();
api.use(eslintPreset({ eslint: { useEslintrc: true } }));
api.use(mw());
const options = api.config.module.rule('lint').use('eslint').get('options');
t.deepEqual(options.baseConfig, {});
});
test('configures moduleFileExtensions correctly', (t) => {
const api = new Neutrino();
api.use(reactPreset());
api.use(mw());
const config = api.outputHandlers.get('jest')(api);
t.deepEqual(config.moduleFileExtensions, [
'web.jsx',
'web.js',
'wasm',
'jsx',
'js',
'json',
]);
});
test('exposes babel config', (t) => {
const api = new Neutrino();
api.use(
compileMiddleware({
cacheDirectory: true,
cacheCompression: false,
cacheIdentifier: false,
customize: true,
}),
);
api.use(mw());
const babelOptions = JSON.parse(process.env.JEST_BABEL_OPTIONS);
t.truthy(babelOptions);
});
test('exposes babel config without babel-loader specific options', (t) => {
const api = new Neutrino();
api.use(
compileMiddleware({
babel: {
cacheDirectory: true,
cacheCompression: false,
cacheIdentifier: false,
customize: true,
},
}),
);
api.use(mw());
const babelOptions = JSON.parse(process.env.JEST_BABEL_OPTIONS);
t.false('cacheDirectory' in babelOptions);
t.false('cacheCompression' in babelOptions);
t.false('cacheIdentifier' in babelOptions);
t.false('customize' in babelOptions);
});
test('configures webpack aliases in moduleNameMapper correctly', (t) => {
const api = new Neutrino();
const reactPath = path.resolve(path.join('node_modules', 'react'));
api.use(reactPreset());
api.use(mw());
api.config.resolve.alias.set('react', reactPath);
const config = api.outputHandlers.get('jest')(api);
t.true(
Object.entries(config.moduleNameMapper).some(([key, alias]) => {
return key === '^react$' && alias === reactPath;
}),
);
});