Skip to content

Commit

Permalink
feat(testing): support for vscode-jest integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexei Domratchev committed Mar 22, 2019
1 parent 95830f9 commit 3cdb8e6
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 105 deletions.
68 changes: 54 additions & 14 deletions docs/api-builders/jest.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,73 +6,103 @@ Run Jest unit tests

### bail

Type: `boolean`
Alias(es): b

Type: `number`

Exit the test suite immediately upon the first failing test suite. (https://jestjs.io/docs/en/cli#bail)
Exit the test suite immediately after `n` number of failing tests. (https://jestjs.io/docs/en/cli#bail)

### ci

Type: `boolean`

Fail on missing snapshots. (https://jestjs.io/docs/en/cli#ci)
Whether to run Jest in continuous integration (CI) mode. This option is on by default in most popular CI environments. It will prevent snapshots from being written unless explicitly requested. (https://jestjs.io/docs/en/cli#ci)

### codeCoverage

Type: `boolean`

Export a code coverage report. (https://jestjs.io/docs/en/cli#coverage)
Indicates that test coverage information should be collected and reported in the output. (https://jestjs.io/docs/en/cli#coverage)

### color

Type: `boolean`

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)

### jestConfig

Type: `string`

The path of the Jest configuration. (https://jestjs.io/docs/en/configuration.html)
The path of the Jest configuration. (https://jestjs.io/docs/en/configuration)

### json

Type: `boolean`

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)

### maxWorkers

Alias(es): w

Type: `number`

Max number of workers to run tests across. Useful for CI. (https://jestjs.io/docs/en/cli.html#maxworkers-num)
Specifies the maximum number of workers the worker-pool will spawn for running tests. This defaults to the number of the cores available on your machine. Useful for CI. (its usually best not to override this default) (https://jestjs.io/docs/en/cli#maxworkers-num)

### onlyChanged

Alias(es): o

Type: `boolean`

Isolate tests affected by uncommitted changes. (https://jestjs.io/docs/en/cli#onlychanged)
Attempts to identify which tests to run based on which files have changed in the current repository. Only works if you're running tests in a git or hg repository at the moment. (https://jestjs.io/docs/en/cli#onlychanged)

### outputFile

Type: `string`

Write test results to a file when the --json option is also specified. (https://jestjs.io/docs/en/cli#outputfile-filename)

### passWithNoTests

Type: `boolean`

Allow test suite to pass when no test files are found. (https://jestjs.io/docs/en/cli#passwithnotests)
Will not fail if no tests are found (for example while using `--testPathPattern`.) (https://jestjs.io/docs/en/cli#passwithnotests)

### runInBand

Alias(es): i

Type: `boolean`

Run tests in a single process as opposed to multiple workers. Useful for CI. (https://jestjs.io/docs/en/cli.html#runinband)
Run all tests serially in the current process (rather than creating a worker pool of child processes that run tests). This is sometimes useful for debugging, but such use cases are pretty rare. Useful for CI. (https://jestjs.io/docs/en/cli#runinband)

### setupFile

Type: `string`

The name of a setup file used by Jest. (https://jestjs.io/docs/en/configuration.html#setuptestframeworkscriptfile-string)
The name of a setup file used by Jest. (https://jestjs.io/docs/en/configuration#setupfilesafterenv-array)

### silent

Type: `boolean`

Prevent tests from printing messages through the console. (https://jestjs.io/docs/en/cli#silent)

### testFile

Type: `string`

The name of the file to test.

### testNamePattern

Alias(es): t

Type: `string`

Run only tests with a name that matches the regex. (https://jestjs.io/docs/en/cli.html#testnamepattern-regex)
Run only tests with a name that matches the regex pattern. (https://jestjs.io/docs/en/cli#testnamepattern-regex)

### tsConfig

Expand All @@ -86,12 +116,22 @@ Alias(es): u

Type: `boolean`

Re-record all failing snapshots. (https://jestjs.io/docs/en/cli#updatesnapshot)
Use this flag to re-record snapshots. Can be used together with a test suite pattern or with `--testNamePattern` to re-record snapshot for test matching the pattern. (https://jestjs.io/docs/en/cli#updatesnapshot)

### useStderr

Type: `boolean`

Divert all output to stderr.

### watch

Default: `false`
Type: `boolean`

Watch files for changes and rerun tests related to changed files. If you want to re-run all tests when a file has changed, use the `--watchAll` option. (https://jestjs.io/docs/en/cli#watch)

### watchAll

Type: `boolean`

Run tests when files change. (https://jestjs.io/docs/en/cli#watch)
Watch files for changes and rerun all tests when something changes. If you want to re-run only the tests that depend on the changed files, use the `--watch` option. (https://jestjs.io/docs/en/cli#watchall)
103 changes: 75 additions & 28 deletions packages/builders/src/jest/jest.builder.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import JestBuilder from './jest.builder';
import { normalize } from '@angular-devkit/core';
import { TestLogger } from '@angular-devkit/architect/testing';
jest.mock('jest');
const { runCLI } = require('jest');
import * as path from 'path';
Expand All @@ -8,7 +9,14 @@ describe('Jest Builder', () => {
let builder: JestBuilder;

beforeEach(() => {
builder = new JestBuilder();
builder = new JestBuilder({
host: <any>{},
logger: new TestLogger('test'),
workspace: <any>{
root: '/root'
},
architect: <any>{}
});
runCLI.mockReturnValue(
Promise.resolve({
results: {
Expand Down Expand Up @@ -36,10 +44,7 @@ describe('Jest Builder', () => {
{
globals: JSON.stringify({
'ts-jest': {
tsConfig: path.join(
'<rootDir>',
path.relative(root, './tsconfig.test.json')
),
tsConfig: '/root/tsconfig.test.json',
stringifyContentPathRegex: '\\.html$',
astTransformers: [
'jest-preset-angular/InlineHtmlStripStylesTransformer'
Expand All @@ -48,7 +53,47 @@ describe('Jest Builder', () => {
}),
watch: false
},
['./jest.config.js']
['/root/jest.config.js']
);
});

it('should send appropriate options to jestCLI when testFile is specified', () => {
const root = normalize('/root');

builder
.run({
root,
builder: '',
projectType: 'application',
options: {
testFile: 'lib.spec.ts',
jestConfig: './jest.config.js',
tsConfig: './tsconfig.test.json',
codeCoverage: false,
runInBand: true,
testNamePattern: 'should load',
watch: false
}
})
.toPromise();
expect(runCLI).toHaveBeenCalledWith(
{
_: ['lib.spec.ts'],
globals: JSON.stringify({
'ts-jest': {
tsConfig: '/root/tsconfig.test.json',
stringifyContentPathRegex: '\\.html$',
astTransformers: [
'jest-preset-angular/InlineHtmlStripStylesTransformer'
]
}
}),
coverage: false,
runInBand: true,
testNamePattern: 'should load',
watch: false
},
['/root/jest.config.js']
);
});

Expand All @@ -62,47 +107,52 @@ describe('Jest Builder', () => {
options: {
jestConfig: './jest.config.js',
tsConfig: './tsconfig.test.json',
watch: false,
codeCoverage: true,
bail: true,
color: false,
ci: true,
updateSnapshot: true,
json: true,
maxWorkers: 2,
onlyChanged: true,
outputFile: 'abc.txt',
passWithNoTests: true,
bail: true,
silent: true,
runInBand: true,
maxWorkers: 2,
testNamePattern: 'test'
testNamePattern: 'test',
updateSnapshot: true,
useStderr: true,
watch: false,
watchAll: false
}
})
.toPromise();
expect(runCLI).toHaveBeenCalledWith(
{
globals: JSON.stringify({
'ts-jest': {
tsConfig: path.join(
'<rootDir>',
path.relative(root, './tsconfig.test.json')
),
tsConfig: '/root/tsconfig.test.json',
stringifyContentPathRegex: '\\.html$',
astTransformers: [
'jest-preset-angular/InlineHtmlStripStylesTransformer'
]
}
}),
watch: false,
coverage: true,
bail: true,
color: false,
ci: true,
updateSnapshot: true,
json: true,
maxWorkers: 2,
onlyChanged: true,
outputFile: 'abc.txt',
passWithNoTests: true,
bail: true,
silent: true,
runInBand: true,
maxWorkers: 2,
testNamePattern: 'test'
testNamePattern: 'test',
updateSnapshot: true,
useStderr: true,
watch: false,
watchAll: false
},
['./jest.config.js']
['/root/jest.config.js']
);
});

Expand All @@ -125,10 +175,7 @@ describe('Jest Builder', () => {
{
globals: JSON.stringify({
'ts-jest': {
tsConfig: path.join(
'<rootDir>',
path.relative(root, './tsconfig.test.json')
),
tsConfig: '/root/tsconfig.test.json',
stringifyContentPathRegex: '\\.html$',
astTransformers: [
'jest-preset-angular/InlineHtmlStripStylesTransformer'
Expand All @@ -141,7 +188,7 @@ describe('Jest Builder', () => {
),
watch: false
},
['./jest.config.js']
['/root/jest.config.js']
);
});

Expand Down
Loading

0 comments on commit 3cdb8e6

Please sign in to comment.