-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathjest.ts
150 lines (137 loc) · 4.4 KB
/
jest.ts
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
import { GeneratorType } from '@umijs/core';
import { logger, semver } from '@umijs/utils';
import { existsSync, writeFileSync } from 'fs';
import { join } from 'path';
import { IApi } from '../../types';
import { GeneratorHelper, promptsExitWhenCancel } from './utils';
export default (api: IApi) => {
api.describe({
key: 'generator:jest',
});
api.registerGenerator({
key: 'jest',
name: 'Enable Jest',
description: 'Setup Jest Configuration',
type: GeneratorType.enable,
checkEnable: () => {
return (
!existsSync(join(api.paths.cwd, 'jest.config.ts')) &&
!existsSync(join(api.paths.cwd, 'jest.config.js'))
);
},
disabledDescription:
'jest has already enabled. You can remove jest.config.{ts,js}, then run this again to re-setup.',
fn: async () => {
const h = new GeneratorHelper(api);
const res = await promptsExitWhenCancel({
type: 'confirm',
name: 'willUseTLR',
message: 'Will you use @testing-library/react for UI testing?!',
initial: true,
});
const hasSrc = api.paths.absSrcPath.endsWith('src');
const importSource = api.appData.umi.importSource;
const jestMajorVersion = `^${getJestVersion()}`;
const basicDeps = {
jest: jestMajorVersion,
'@types/jest': jestMajorVersion,
// we use `jest.config.ts` so jest needs ts and ts-node
typescript: '^5',
'ts-node': '^10',
'cross-env': '^7',
};
const reactTestingDeps = {
// `jest-environment-jsdom` is no longer included in jest >= 28
'jest-environment-jsdom': jestMajorVersion,
// RTL
'@testing-library/jest-dom': '^5',
'@testing-library/react': '^14',
};
const packageToInstall: Record<string, string> = res.willUseTLR
? {
...basicDeps,
...reactTestingDeps,
'@types/testing-library__jest-dom': '^5.14.5',
}
: basicDeps;
h.addDevDeps(packageToInstall);
h.addScript(
'test',
'cross-env TS_NODE_TRANSPILE_ONLY=yes jest --passWithNoTests',
);
const setupImports = res.willUseTLR
? [
`import '@testing-library/jest-dom';`,
`import '${api.appData.umi.importSource}/test-setup'`,
]
: [`import '${api.appData.umi.importSource}/test-setup'`];
writeFileSync(join(api.cwd, 'jest-setup.ts'), setupImports.join('\n'));
logger.info('Write jest-setup.ts');
const collectCoverageFrom = hasSrc
? [
'src/**/*.{ts,js,tsx,jsx}',
'!src/.umi/**',
'!src/.umi-test/**',
'!src/.umi-production/**',
]
: [
'**/*.{ts,tsx,js,jsx}',
'!.umi/**',
'!.umi-test/**',
'!.umi-production/**',
'!.umirc.{js,ts}',
'!.umirc.*.{js,ts}',
'!jest.config.{js,ts}',
'!coverage/**',
'!dist/**',
'!config/**',
'!mock/**',
];
writeFileSync(
join(api.cwd, 'jest.config.ts'),
`
import { Config, configUmiAlias, createConfig } from '${importSource}/test';
export default async () => {
try{
return (await configUmiAlias({
...createConfig({
target: 'browser',
jsTransformer: 'esbuild',
// config opts for esbuild , it will pass to esbuild directly
jsTransformerOpts: { jsx: 'automatic' },
}),
${
res.willUseTLR ? `setupFilesAfterEnv: ['<rootDir>/jest-setup.ts'],` : ''
}
collectCoverageFrom: [
${collectCoverageFrom.map((v) => ` '${v}'`).join(',\n')}
],
// if you require some es-module npm package, please uncomment below line and insert your package name
// transformIgnorePatterns: ['node_modules/(?!.*(lodash-es|your-es-pkg-name)/)']
})) as Config.InitialOptions;
} catch (e) {
console.log(e);
throw e;
}
};
`.trimStart(),
);
logger.info('Write jest.config.ts');
h.installDeps();
},
});
};
function getJestVersion() {
try {
const umiPkg = require.resolve('umi/package.json', {
paths: [process.cwd()],
});
const testPkg = require.resolve('@umijs/test/package.json', {
paths: [umiPkg],
});
const version: string = require(testPkg).devDependencies.jest;
return semver.minVersion(version)!.version.split('.')[0];
} catch {
return 29;
}
}