-
Notifications
You must be signed in to change notification settings - Fork 0
/
jest.config.js
83 lines (78 loc) · 2.87 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
// jest --updateSnapshot 或 jest -u 更新快照
const jestConfig = {
roots: ['<rootDir>'],
// 收集测试覆盖率的匹配文件规则集合,!代表排除的文件
collectCoverageFrom: [
"src/**/*.{js,jsx,ts,tsx}",
"!src/**/*.d.ts",
'!src/types/**/*.ts', '!src/index.tsx', '!src/serviceWorker.ts',
// '!src/components/_demos/**/*.{ts,tsx}',
],
setupFiles: ['react-app-polyfill/jsdom'],
setupFilesAfterEnv: [
"<rootDir>/src/setupTests.ts",
],
testMatch: [
"<rootDir>/src/**/__tests__/**/*.{js,jsx,ts,tsx}",
"<rootDir>/src/**/*.{spec,test}.{js,jsx,ts,tsx}",
'<rootDir>/tests/**/*.{spec,test}.{js,jsx,ts,tsx}',
],
testEnvironment: 'jsdom',
transform: {
"^.+\\.(js|jsx|mjs|cjs|ts|tsx)$": "<rootDir>/config/jest/babelTransform.js",
"^.+\\.css$": "<rootDir>/config/jest/cssTransform.js",
"^(?!.*\\.(js|jsx|mjs|cjs|ts|tsx|css|json)$)": "<rootDir>/config/jest/fileTransform.js"
},
transformIgnorePatterns: [
"[/\\\\]node_modules[/\\\\].+\\.(js|jsx|mjs|cjs|ts|tsx)$",
"^.+\\.module\\.(css|sass|scss)$"
],
modulePaths: ['<rootDir>/src'],
moduleNameMapper: {
"^react-native$": "react-native-web",
"^.+\\.module\\.(css|sass|scss)$": "identity-obj-proxy"
},
moduleFileExtensions: [
"web.js",
"js",
"web.ts",
"ts",
"web.tsx",
"tsx",
"json",
"web.jsx",
"jsx",
"node"
],
watchPlugins: [
"jest-watch-typeahead/filename",
"jest-watch-typeahead/testname"
],
resetMocks: true,
reporters: ['default'],
collectCoverage: true,
};
const testMatch = process.env.testMatch;
if (testMatch) {
console.log('------ found customized testMatch, start compute ------');
let prefixedTestMatch = testMatch;
if (!testMatch.startsWith('<rootDir>')) {
if (testMatch.startsWith('/')) prefixedTestMatch = `<rootDir>${testMatch}`;
else prefixedTestMatch = `<rootDir>/${testMatch}`;
}
jestConfig.testMatch = [prefixedTestMatch];
console.log(`computed testMatch: ${JSON.stringify(jestConfig.testMatch)}`);
if (testMatch.includes('__tests__')) {
// from: 'src/components/biz-smart/SomeComponent/__tests__/*.{ts,tsx}'
// to: ['src/components/biz-smart/SomeComponent/', '*.{ts,tsx}']
const [dirPath, filePath] = testMatch.split('__tests__/');
// 只收集这一部分覆盖率,方便本地缩小查看范围
jestConfig.collectCoverageFrom = [`${dirPath}**/${filePath}`];
console.log(`computed collectCoverageFrom: ${JSON.stringify(jestConfig.collectCoverageFrom)}`);
}
} else {
console.log('开始载入jest配置文件,如果是本地执行,想缩小单测范围,可加上testMatch前缀执行,形如');
// 执行类似命令,缩小测试范围:testMatch='src/pages/_DemoTodoList/__tests__/*.{ts,tsx}' npm run test
console.log('testMatch=\'src/components/HelloRemoteReactComp/__tests__/*.{ts,tsx}\' npm run test');
}
module.exports = jestConfig;