Skip to content

Commit 3c9ca83

Browse files
authored
Merge pull request #95 from LiangCY/feat/jest-config
feat: support customize jest config
2 parents 16a2024 + fd1fd15 commit 3c9ca83

File tree

10 files changed

+97
-65
lines changed

10 files changed

+97
-65
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@
129129
"postcss-scss": "2.0.0",
130130
"prettier": "1.19.1",
131131
"raw-loader": "4.0.0",
132-
"react": "16.12.0",
133-
"react-dom": "16.12.0",
132+
"react": "^16.12.0",
133+
"react-dom": "^16.12.0",
134134
"react-hot-loader": "4.12.19",
135135
"react-router-dom": "5.1.2",
136136
"rimraf": "3.0.0",

src/built-in-plugins/command-test/plugin/index.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ pri.commands.registerCommand({
2121
skipLint: {
2222
description: 'Skip lint',
2323
},
24+
updateSnapshot: {
25+
alias: 'u',
26+
description: 'Update snapshot',
27+
},
28+
watch: {
29+
description: 'Watch files for changes and rerun tests related to changed files',
30+
},
31+
watchAll: {
32+
description: 'Watch files for changes and rerun all tests',
33+
},
2434
},
2535
action: async (options: IOpts) => {
2636
if (!options.skipLint) {
@@ -36,6 +46,6 @@ pri.commands.registerCommand({
3646
await pri.project.checkProjectFiles();
3747

3848
const runTestModule = await import('./run-test');
39-
runTestModule.runTest();
49+
runTestModule.runTest(options);
4050
},
4151
});
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1-
export interface IOpts {
1+
import { Arguments } from 'yargs';
2+
3+
export type IOpts = Arguments<{
24
skipLint?: boolean;
3-
}
5+
updateSnapshot?: boolean;
6+
watch?: boolean;
7+
watchAll?: boolean;
8+
}>;

src/built-in-plugins/command-test/plugin/run-test.ts

+42-42
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,53 @@
1-
import { execSync } from 'child_process';
21
import * as path from 'path';
2+
import * as jest from 'jest';
33
import { pri } from '../../../node';
4+
import { plugin } from '../../../utils/plugins';
45
import { logText } from '../../../utils/log';
5-
import { findNearestNodemodulesFile } from '../../../utils/npm-finder';
66
import { testsPath } from '../../../utils/structor-config';
77
import { globalState } from '../../../utils/global-state';
8+
import { IOpts } from './interface';
89

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(
4745
{
48-
stdio: 'inherit',
49-
cwd: pri.projectRootPath,
46+
_: options._,
47+
$0: options.$0,
48+
...plugin.jestConfigPipes.reduce((config, fn) => fn(config), jestConfig),
5049
},
50+
[pri.projectRootPath],
5151
);
5252

5353
logText(

src/node/index.ts

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import * as project from './project/index';
99
import * as self from './self';
1010
import * as serviceWorker from './service-worker';
1111
import * as webpackCommand from './webpack';
12+
import * as test from './test';
1213

1314
type IPri = typeof globalState &
1415
typeof self & {
@@ -46,6 +47,10 @@ type IPri = typeof globalState &
4647
webpack: typeof webpackCommand;
4748

4849
event: typeof priEvent;
50+
/**
51+
* Test configs
52+
*/
53+
test: typeof test;
4954
};
5055

5156
const globalWithPri = global as typeof global & { pri: IPri };
@@ -61,6 +66,7 @@ if (!globalWithPri.pri) {
6166
event: priEvent,
6267
webpack: webpackCommand,
6368
cli,
69+
test,
6470
...self,
6571
} as any;
6672

src/node/test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { plugin } from '../utils/plugins';
2+
import { IJestConfigPipe } from '../utils/define';
3+
4+
export const pipeJestConfig = (pipe: IJestConfigPipe) => {
5+
plugin.jestConfigPipes.push(pipe);
6+
};

src/utils/define.ts

+2
Original file line numberDiff line numberDiff line change
@@ -326,3 +326,5 @@ export interface IPluginModule {
326326
}
327327

328328
export type IDevDllList = (list: string[]) => string[];
329+
330+
export type IJestConfigPipe = (options: any) => any;

src/utils/plugins.ts

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
CommandRegister,
1919
ProjectType,
2020
IDevDllList,
21+
IJestConfigPipe,
2122
} from './define';
2223

2324
import * as pluginClientSsr from '../built-in-plugins/client-ssr';
@@ -89,6 +90,8 @@ export class IPluginConfig {
8990

9091
public devDllPipes: IDevDllList[] = [];
9192

93+
public jestConfigPipes: IJestConfigPipe[] = [];
94+
9295
// Lock init type
9396
public initType: ProjectType = null;
9497
}

src/utils/webpack-config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export const getWebpackConfig = async (opts: IOptions) => {
139139

140140
const extraCssInProd = (...loaders: any[]) => {
141141
// Enable cssExtract, but not in bundle command.
142-
if (globalState.sourceConfig.cssExtract && yargs.argv._[0] !== 'bundle' && yargs.argv._[0] !== 'debug') {
142+
if (globalState.sourceConfig.cssExtract && yargs.argv._[0] !== 'bundle' && !yargs.argv._[0].startsWith('debug')) {
143143
if (globalState.isDevelopment) {
144144
return [styleLoader, ...loaders];
145145
}

yarn.lock

+17-17
Original file line numberDiff line numberDiff line change
@@ -11644,15 +11644,15 @@ rc@^1.2.8:
1164411644
minimist "^1.2.0"
1164511645
strip-json-comments "~2.0.1"
1164611646

11647-
react-dom@16.13.0:
11648-
version "16.13.0"
11649-
resolved "https://registry.npm.alibaba-inc.com/react-dom/download/react-dom-16.13.0.tgz#cdde54b48eb9e8a0ca1b3dc9943d9bb409b81866"
11650-
integrity sha1-zd5UtI656KDKGz3JlD2btAm4GGY=
11647+
react-dom@^16.12.0:
11648+
version "16.14.0"
11649+
resolved "https://registry.npm.alibaba-inc.com/react-dom/download/react-dom-16.14.0.tgz#7ad838ec29a777fb3c75c3a190f661cf92ab8b89"
11650+
integrity sha1-etg47Cmnd/s8dcOhkPZhz5Kri4k=
1165111651
dependencies:
1165211652
loose-envify "^1.1.0"
1165311653
object-assign "^4.1.1"
1165411654
prop-types "^15.6.2"
11655-
scheduler "^0.19.0"
11655+
scheduler "^0.19.1"
1165611656

1165711657
1165811658
version "4.12.19"
@@ -11727,10 +11727,10 @@ react-transition-group@^4.3.0:
1172711727
loose-envify "^1.4.0"
1172811728
prop-types "^15.6.2"
1172911729

11730-
react@16.13.0:
11731-
version "16.13.0"
11732-
resolved "https://registry.npm.alibaba-inc.com/react/download/react-16.13.0.tgz#d046eabcdf64e457bbeed1e792e235e1b9934cf7"
11733-
integrity sha1-0EbqvN9k5Fe77tHnkuI14bmTTPc=
11730+
react@^16.12.0:
11731+
version "16.14.0"
11732+
resolved "https://registry.npm.alibaba-inc.com/react/download/react-16.14.0.tgz#94d776ddd0aaa37da3eda8fc5b6b18a4c9a3114d"
11733+
integrity sha1-lNd23dCqo32j7aj8W2sYpMmjEU0=
1173411734
dependencies:
1173511735
loose-envify "^1.1.0"
1173611736
object-assign "^4.1.1"
@@ -12475,10 +12475,10 @@ scheduler@^0.17.0:
1247512475
loose-envify "^1.1.0"
1247612476
object-assign "^4.1.1"
1247712477

12478-
scheduler@^0.19.0:
12479-
version "0.19.0"
12480-
resolved "https://registry.npm.alibaba-inc.com/scheduler/download/scheduler-0.19.0.tgz#a715d56302de403df742f4a9be11975b32f5698d"
12481-
integrity sha1-pxXVYwLeQD33QvSpvhGXWzL1aY0=
12478+
scheduler@^0.19.0, scheduler@^0.19.1:
12479+
version "0.19.1"
12480+
resolved "https://registry.npm.alibaba-inc.com/scheduler/download/scheduler-0.19.1.tgz#4f3e2ed2c1a7d65681f4c854fa8c5a1ccb40f196"
12481+
integrity sha1-Tz4u0sGn1laB9MhU+oxaHMtA8ZY=
1248212482
dependencies:
1248312483
loose-envify "^1.1.0"
1248412484
object-assign "^4.1.1"
@@ -14606,10 +14606,10 @@ [email protected], webpack-sources@^1.1.0, webpack-sources@^1.4.0, webpack-s
1460614606
source-list-map "^2.0.0"
1460714607
source-map "~0.6.1"
1460814608

14609-
webpack@4.42.0:
14610-
version "4.42.0"
14611-
resolved "https://registry.npm.alibaba-inc.com/webpack/download/webpack-4.42.0.tgz#b901635dd6179391d90740a63c93f76f39883eb8"
14612-
integrity sha1-uQFjXdYXk5HZB0CmPJP3bzmIPrg=
14609+
webpack@4.41.5:
14610+
version "4.41.5"
14611+
resolved "https://registry.npm.alibaba-inc.com/webpack/download/webpack-4.41.5.tgz#3210f1886bce5310e62bb97204d18c263341b77c"
14612+
integrity sha1-MhDxiGvOUxDmK7lyBNGMJjNBt3w=
1461314613
dependencies:
1461414614
"@webassemblyjs/ast" "1.8.5"
1461514615
"@webassemblyjs/helper-module-context" "1.8.5"

0 commit comments

Comments
 (0)