-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjest.config.js
180 lines (159 loc) · 4.43 KB
/
jest.config.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
169
170
171
172
173
174
175
176
177
178
179
180
/** @typedef {import('@jest/types').Config.InitialOptions} JestConfig */
/** @typedef {import('@swc-node/core').Options} SwcNodeOptions */
const {dirname} = require('path')
const merge = require('lodash.merge')
const {
readDefaultTsConfig,
tsCompilerOptionsToSwcConfig,
} = require('@swc-node/register/read-default-tsconfig')
const {ifAnyDep, hasFile, fromRoot, hasDevDep, getDebug} = require('../utils')
const {pathsToModuleNameMapper} = require('../api/test')
const {
testMatch,
testMatchGlob,
testMatchExtensions,
} = require('./helpers/test-match')
const debug = getDebug('jest')
const ignores = [
'/node_modules/',
'/__fixtures__/',
'/fixtures/',
'/__tests__/helpers/',
'/__tests__/utils/',
'__mocks__',
]
/**
* @type {SwcNodeOptions}
*/
const DEFAULT_SWC_OPTIONS = {
esModuleInterop: true,
module: 'commonjs',
react: {runtime: 'automatic'},
swc: {
jsc: {
target: 'es2020',
experimental: {
plugins: [[require.resolve('swc_mut_cjs_exports'), {}]],
},
parser: {
syntax: 'typescript',
tsx: true,
decorators: false,
dynamicimport: true,
},
loose: true,
externalHelpers: false,
transform: {
react: {
runtime: 'automatic',
},
},
},
},
}
const tsConfig = readDefaultTsConfig()
const swcConfig = merge(
tsCompilerOptionsToSwcConfig(tsConfig, ''),
DEFAULT_SWC_OPTIONS,
)
debug.prefix('tsconfig:paths')(tsConfig.paths)
/**
* Get the path at which `@hover/javascript/jest` is installed in a dependent
* project in order to resolve the Jest preset as sometimes package managers
* nest the preset installation within the `@hover/javascript` installation.
*/
const getResolvePaths = () => {
try {
const nested = require.resolve('@hover/javascript/jest')
return {paths: [dirname(nested)]}
} catch {
return undefined
}
}
/**
* The default transform is now SWC, however, `ts-jest` will
* still be used if it is installed in the host project
*
* @returns {JestConfig['transform']}
*/
const getTransform = () => {
const log = debug.prefix('transform')
if (hasDevDep('ts-jest')) {
log('Detected `ts-jest` package, using ts-jest transform')
const transform = Object.fromEntries(
// Ensure we can resolve the preset even when
// it's in a nested `node_modules` installation
Object.entries(require('ts-jest/presets').jsWithTs.transform).map(
([glob, [transformer, options]]) => [
glob,
[
require.resolve(transformer),
{
...options,
diagnostics: false,
},
],
],
),
)
log(transform)
return transform
}
log('No `ts-jest` package detected, using default SWC transform')
const transform = {
'^.+\\.(t|j|mj)sx?$': [
require.resolve('@swc-node/jest', getResolvePaths()),
swcConfig,
],
}
log(transform)
return transform
}
/** @type JestConfig */
const jestConfig = {
roots: [fromRoot('.')],
// Here we're preserving Jest <= 28 snapshot format to prevent the need
// to update snapshots when upgrading Jest via @hover/javascript, see:
// https://jestjs.io/docs/upgrading-to-jest29#snapshot-format
snapshotFormat: {escapeString: true, printBasicPrototype: true},
testEnvironment: ifAnyDep(['webpack', 'rollup', 'react'], 'jsdom', 'node'),
testEnvironmentOptions: {url: 'http://localhost'},
moduleFileExtensions: testMatchExtensions.concat('json'),
collectCoverageFrom: [`**/${testMatchGlob}`],
testMatch,
testPathIgnorePatterns: [...ignores, '<rootDir>/dist'],
testLocationInResults: true,
moduleNameMapper: debug.trace(
pathsToModuleNameMapper(
debug.trace(swcConfig.paths, 'moduleNameMapper:paths'),
),
'moduleNameMapper',
),
transform: getTransform(),
coveragePathIgnorePatterns: [
...ignores,
'src/(umd|cjs|esm)-entry.js$',
'<rootDir>/dist',
],
transformIgnorePatterns: [
'[/\\\\]node_modules[/\\\\].+\\.(js|jsx)$',
'/dist/',
],
watchPathIgnorePatterns: ['<rootDir>/dist'],
coverageThreshold: {
global: {
branches: 100,
functions: 100,
lines: 100,
statements: 100,
},
},
watchPlugins: [
require.resolve('jest-watch-typeahead/filename'),
require.resolve('jest-watch-typeahead/testname'),
],
setupFilesAfterEnv: hasFile('tests/setup-env.js')
? [fromRoot('tests/setup-env.js')]
: undefined,
}
module.exports = jestConfig