Skip to content

Commit

Permalink
feat(testing): add findRelatedTests option to jest builder
Browse files Browse the repository at this point in the history
closes #1527
  • Loading branch information
BuckyMaler authored and vsavkin committed Jul 9, 2019
1 parent 83311ae commit 1f5f0c2
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
6 changes: 6 additions & 0 deletions docs/api-jest/builders/jest.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ Type: `string`

A list of reporter names that Jest uses when writing coverage reports. Any istanbul reporter

### findRelatedTests

Type: `string`

Find and run the tests that cover a comma separated list of source files that were passed in as arguments. (https://jestjs.io/docs/en/cli#findrelatedtests-spaceseparatedlistofsourcefiles)

### jestConfig

Type: `string`
Expand Down
44 changes: 44 additions & 0 deletions packages/jest/src/builders/jest/jest.impl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ describe('Jest Builder', () => {
);
expect(runCLI).toHaveBeenCalledWith(
{
_: [],
globals: JSON.stringify({
'ts-jest': {
tsConfig: '/root/tsconfig.test.json',
Expand Down Expand Up @@ -115,6 +116,47 @@ describe('Jest Builder', () => {
);
});

it('should send appropriate options to jestCLI when findRelatedTests is specified', async () => {
const run = await architect.scheduleBuilder('@nrwl/jest:jest', {
findRelatedTests: 'file1.ts,file2.ts',
jestConfig: './jest.config.js',
tsConfig: './tsconfig.test.json',
codeCoverage: false,
runInBand: true,
testNamePattern: 'should load',
watch: false
});
expect(await run.result).toEqual(
jasmine.objectContaining({
success: true
})
);

expect(runCLI).toHaveBeenCalledWith(
{
_: ['file1.ts', 'file2.ts'],
globals: JSON.stringify({
'ts-jest': {
tsConfig: '/root/tsconfig.test.json',
diagnostics: {
warnOnly: true
},
stringifyContentPathRegex: '\\.html$',
astTransformers: [
'jest-preset-angular/InlineHtmlStripStylesTransformer'
]
}
}),
coverage: false,
findRelatedTests: true,
runInBand: true,
testNamePattern: 'should load',
watch: false
},
['/root/jest.config.js']
);
});

it('should send other options to jestCLI', async () => {
const run = await architect.scheduleBuilder('@nrwl/jest:jest', {
jestConfig: './jest.config.js',
Expand Down Expand Up @@ -149,6 +191,7 @@ describe('Jest Builder', () => {
);
expect(runCLI).toHaveBeenCalledWith(
{
_: [],
globals: JSON.stringify({
'ts-jest': {
tsConfig: '/root/tsconfig.test.json',
Expand Down Expand Up @@ -201,6 +244,7 @@ describe('Jest Builder', () => {
);
expect(runCLI).toHaveBeenCalledWith(
{
_: [],
globals: JSON.stringify({
'ts-jest': {
tsConfig: '/root/tsconfig.test.json',
Expand Down
10 changes: 9 additions & 1 deletion packages/jest/src/builders/jest/jest.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface JestBuilderOptions extends JsonObject {
ci?: boolean;
color?: boolean;
clearCache?: boolean;
findRelatedTests?: string;
json?: boolean;
maxWorkers?: number;
onlyChanged?: boolean;
Expand Down Expand Up @@ -75,6 +76,7 @@ function run(
} catch (e) {}

const config: any = {
_: [],
coverage: options.codeCoverage,
bail: options.bail,
ci: options.ci,
Expand Down Expand Up @@ -110,7 +112,13 @@ function run(
}

if (options.testFile) {
config._ = [options.testFile];
config._.push(options.testFile);
}

if (options.findRelatedTests) {
const parsedTests = options.findRelatedTests.split(',').map(s => s.trim());
config._.push(...parsedTests);
config.findRelatedTests = true;
}

if (options.clearCache) {
Expand Down
4 changes: 4 additions & 0 deletions packages/jest/src/builders/jest/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
"description": "Forces test results output color highlighting (even if stdout is not a TTY). Set to false if you would like to have no colors. (https://jestjs.io/docs/en/cli#colors)",
"type": "boolean"
},
"findRelatedTests": {
"description": "Find and run the tests that cover a comma separated list of source files that were passed in as arguments. (https://jestjs.io/docs/en/cli#findrelatedtests-spaceseparatedlistofsourcefiles)",
"type": "string"
},
"json": {
"description": "Prints the test results in JSON. This mode will send all other test output and user messages to stderr. (https://jestjs.io/docs/en/cli#json)",
"type": "boolean"
Expand Down

0 comments on commit 1f5f0c2

Please sign in to comment.