Skip to content

Commit 872d10d

Browse files
authored
Jest task should let jest.json to override Jest task's default options. (microsoft#435)
* Jest task should let jest.config.json to override certain CLI options. * Add change file * Move Jest related API from public to alpha and update IJestConfig. * Removed unused code. * Update description in the change file
1 parent 6cb89c4 commit 872d10d

File tree

4 files changed

+66
-22
lines changed

4 files changed

+66
-22
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@microsoft/gulp-core-build",
5+
"comment": "Allow settings in jest.json to override Jest task's default options",
6+
"type": "patch"
7+
}
8+
],
9+
"packageName": "@microsoft/gulp-core-build",
10+
"email": "qz2017@users.noreply.github.com"
11+
}

common/reviews/api/gulp-core-build.api.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,17 +188,19 @@ interface IExecutable {
188188
onRegister?: () => void;
189189
}
190190

191-
// @public
191+
// @alpha
192192
interface IJestConfig {
193-
configFilePath?: string;
193+
collectCoverageFrom?: string[];
194194
coverage?: boolean;
195+
coverageReporters?: string[];
195196
isEnabled?: boolean;
197+
testPathIgnorePatterns?: string[];
196198
}
197199

198200
// @public
199201
export function initialize(gulp: typeof Gulp): void;
200202

201-
// @public
203+
// @alpha
202204
class JestTask extends GulpTask<IJestConfig> {
203205
constructor();
204206
// (undocumented)

core-build/gulp-core-build/src/tasks/JestTask.ts

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import * as globby from 'globby';
1010

1111
/**
1212
* Configuration for JestTask
13-
* @public
13+
* @alpha
1414
*/
1515
export interface IJestConfig {
1616
/**
@@ -19,16 +19,24 @@ export interface IJestConfig {
1919
isEnabled?: boolean;
2020

2121
/**
22-
* The jest config file relative to project root directory
23-
* If not provided, the default value is 'jest.config.json'.
22+
* Same as Jest CLI option collectCoverageFrom
2423
*/
25-
configFilePath?: string;
24+
collectCoverageFrom?: string[];
2625

2726
/**
28-
* Indicates that test coverage information should be collected and reported in the output
29-
* If not provided, the default value is true.
27+
* Same as Jest CLI option coverage
3028
*/
3129
coverage?: boolean;
30+
31+
/**
32+
* Same as Jest CLI option coverageReporters
33+
*/
34+
coverageReporters?: string[];
35+
36+
/**
37+
* Same as Jest CLI option testPathIgnorePatterns
38+
*/
39+
testPathIgnorePatterns?: string[];
3240
}
3341

3442
const DEFAULT_JEST_CONFIG_FILE_NAME: string = 'jest.config.json';
@@ -50,14 +58,17 @@ export function _isJestEnabled(rootFolder: string): boolean {
5058

5159
/**
5260
* This task takes in a map of dest: [sources], and copies items from one place to another.
53-
* @public
61+
* @alpha
5462
*/
5563
export class JestTask extends GulpTask<IJestConfig> {
5664

5765
constructor() {
5866
super('jest',
5967
{
60-
coverage: true
68+
collectCoverageFrom: ['lib/**/*.js?(x)', '!lib/**/test/**'],
69+
coverage: true,
70+
coverageReporters: ['json', 'html'],
71+
testPathIgnorePatterns: ['<rootDir>/(src|lib-amd|lib-es6|coverage|build|docs|node_modules)/']
6172
});
6273
}
6374

@@ -77,22 +88,26 @@ export class JestTask extends GulpTask<IJestConfig> {
7788
completeCallback: (error?: string | Error) => void
7889
): void {
7990
const configFileFullPath: string = path.join(this.buildConfig.rootPath,
80-
'config', DEFAULT_JEST_CONFIG_FILE_NAME);
91+
'config', 'jest', DEFAULT_JEST_CONFIG_FILE_NAME);
8192

8293
this._copySnapshots(this.buildConfig.srcFolder, this.buildConfig.libFolder);
94+
8395
Jest.runCLI(
8496
{
8597
ci: this.buildConfig.production,
8698
config: configFileFullPath,
99+
collectCoverageFrom: this.taskConfig.collectCoverageFrom,
87100
coverage: this.taskConfig.coverage,
101+
coverageReporters: this.taskConfig.coverageReporters,
102+
coverageDirectory: path.join(this.buildConfig.tempFolder, 'coverage'),
88103
maxWorkers: 1,
89-
runInBand: true,
90-
updateSnapshot: !this.buildConfig.production,
104+
moduleDirectories: ['node_modules', this.buildConfig.libFolder],
105+
reporters: [path.join(__dirname, 'JestReporter.js')],
91106
rootDir: this.buildConfig.rootPath,
107+
runInBand: true,
92108
testMatch: ['**/*.test.js?(x)'],
93-
testPathIgnorePatterns: ['<rootDir>/(src|lib-amd|lib-es6|coverage|build|docs|node_modules)/'],
94-
collectCoverageFrom: ['lib/**/*.js?(x)'],
95-
reporters: [path.join(__dirname, 'JestReporter.js')]
109+
testPathIgnorePatterns: this.taskConfig.testPathIgnorePatterns,
110+
updateSnapshot: !this.buildConfig.production
96111
},
97112
[this.buildConfig.rootPath],
98113
(result) => {

core-build/gulp-core-build/src/tasks/jest.schema.json

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,31 @@
1313
"type": "boolean",
1414
"default": false
1515
},
16-
"configFilePath": {
17-
"description": "The path to a jest config file relative to root project folder",
18-
"type": "string",
19-
"default": "jest.config.json"
16+
"collectCoverageFrom": {
17+
"description": "Same as Jest CLI option collectCoverageFrom",
18+
"type": "array",
19+
"items": {
20+
"type": "string"
21+
}
2022
},
2123
"coverage": {
22-
"description": "Indicates that test coverage information should be collected and reported in the output",
24+
"description": "Same as Jest CLI option coverage",
2325
"type": "boolean",
2426
"default": true
27+
},
28+
"coverageReporters": {
29+
"description": "Same as Jest CLI option coverageReporters",
30+
"type": "array",
31+
"items": {
32+
"type": "string"
33+
}
34+
},
35+
"testPathIgnorePatterns": {
36+
"description": "Same as Jest CLI option testPathIgnorePatterns",
37+
"type": "array",
38+
"items": {
39+
"type": "string"
40+
}
2541
}
2642
},
2743
"additionalProperties": false

0 commit comments

Comments
 (0)