|
1 | | -import { execSync } from 'child_process'; |
2 | 1 | import * as path from 'path'; |
| 2 | +import * as jest from 'jest'; |
3 | 3 | import { pri } from '../../../node'; |
| 4 | +import { plugin } from '../../../utils/plugins'; |
4 | 5 | import { logText } from '../../../utils/log'; |
5 | | -import { findNearestNodemodulesFile } from '../../../utils/npm-finder'; |
6 | 6 | import { testsPath } from '../../../utils/structor-config'; |
7 | 7 | import { globalState } from '../../../utils/global-state'; |
| 8 | +import { IOpts } from './interface'; |
8 | 9 |
|
9 | | -export const runTest = async () => { |
10 | | - execSync( |
11 | | - [ |
12 | | - findNearestNodemodulesFile('/.bin/jest'), |
13 | | - `--roots "${pri.sourceRoot}"`, |
14 | | - `--testRegex "${path.join(pri.sourceRoot, testsPath.dir)}/.*\\.tsx?$"`, |
15 | | - '--moduleFileExtensions ts tsx js jsx', |
16 | | - `--transform '${JSON.stringify({ |
17 | | - [`${pri.sourceRoot}/.*\\.tsx?$`]: path.join(__dirname, './jest-transformer'), |
18 | | - ...globalState.packages.reduce((obj, eachPackage) => { |
19 | | - if (eachPackage.rootPath) { |
20 | | - return { |
21 | | - ...obj, |
22 | | - [`${path.join(eachPackage.rootPath, 'src')}/.*\\.tsx?$`]: path.join(__dirname, './jest-transformer'), |
23 | | - }; |
24 | | - } |
25 | | - return obj; |
26 | | - }, {}), |
27 | | - })}'`, |
28 | | - // `--setupFilesAfterEnv '${path.join(__dirname, './jest-setup')}'`, |
29 | | - '--coverage', |
30 | | - // 测试支持自定义 packages 别名 |
31 | | - `--moduleNameMapper '${JSON.stringify( |
32 | | - globalState.packages.reduce((obj, eachPackage) => { |
33 | | - if (eachPackage.packageJson && eachPackage.packageJson.name) { |
34 | | - return { |
35 | | - ...obj, |
36 | | - [eachPackage.packageJson.name]: path.join(eachPackage.rootPath, 'src'), |
37 | | - }; |
38 | | - } |
39 | | - return obj; |
40 | | - }, {}), |
41 | | - )}'`, |
42 | | - ] |
43 | | - .map(each => { |
44 | | - return each.trim(); |
45 | | - }) |
46 | | - .join(' '), |
| 10 | +export const runTest = async (options: IOpts) => { |
| 11 | + const jestConfig = { |
| 12 | + rootDir: pri.sourceRoot, |
| 13 | + testRegex: `${path.join(pri.sourceRoot, testsPath.dir)}/.*\\.tsx?$`, |
| 14 | + moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'], |
| 15 | + coverage: true, |
| 16 | + updateSnapshot: options.updateSnapshot, |
| 17 | + watch: options.watch, |
| 18 | + watchAll: options.watchAll, |
| 19 | + transform: JSON.stringify({ |
| 20 | + [`${pri.sourceRoot}/.*\\.tsx?$`]: path.join(__dirname, './jest-transformer'), |
| 21 | + ...globalState.packages.reduce((obj, eachPackage) => { |
| 22 | + if (eachPackage.rootPath) { |
| 23 | + return { |
| 24 | + ...obj, |
| 25 | + [`${path.join(eachPackage.rootPath, 'src')}/.*\\.tsx?$`]: path.join(__dirname, './jest-transformer'), |
| 26 | + }; |
| 27 | + } |
| 28 | + return obj; |
| 29 | + }, {}), |
| 30 | + }), |
| 31 | + moduleNameMapper: JSON.stringify( |
| 32 | + globalState.packages.reduce((obj, eachPackage) => { |
| 33 | + if (eachPackage.packageJson && eachPackage.packageJson.name) { |
| 34 | + return { |
| 35 | + ...obj, |
| 36 | + [eachPackage.packageJson.name]: path.join(eachPackage.rootPath, 'src'), |
| 37 | + }; |
| 38 | + } |
| 39 | + return obj; |
| 40 | + }, {}), |
| 41 | + ), |
| 42 | + }; |
| 43 | + |
| 44 | + await jest.runCLI( |
47 | 45 | { |
48 | | - stdio: 'inherit', |
49 | | - cwd: pri.projectRootPath, |
| 46 | + _: options._, |
| 47 | + $0: options.$0, |
| 48 | + ...plugin.jestConfigPipes.reduce((config, fn) => fn(config), jestConfig), |
50 | 49 | }, |
| 50 | + [pri.projectRootPath], |
51 | 51 | ); |
52 | 52 |
|
53 | 53 | logText( |
|
0 commit comments