Skip to content

Commit

Permalink
Merge remote-tracking branch 'jest/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
mpontus committed May 4, 2017
2 parents 8633a21 + 79dfafd commit 140378c
Show file tree
Hide file tree
Showing 49 changed files with 437 additions and 199 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ bin/
docs/
flow-typed/**
packages/*/build/**
packages/*/build-es5/**
types/**
website/build
website/node_modules
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
/node_modules

/packages/*/build/
/packages/*/build-es5/
/packages/*/coverage/
/packages/*/node_modules/
/website/build
Expand Down
1 change: 1 addition & 0 deletions crowdin.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
project_identifier_env: CROWDIN_PROJECT_ID
api_key_env: CROWDIN_API_KEY
base_path: "./"
preserve_hierarchy: true

files:
-
Expand Down
73 changes: 73 additions & 0 deletions docs/en/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,79 @@ Default: `false`

Automatically clear mock calls and instances between every test. Equivalent to calling `jest.clearAllMocks()` between each test.

### `reporters` [array<moduleName | [moduleName, options]>]
Default: `undefined`

##### available in Jest **20.0.0+**

Use this configuration option to add custom reporters to Jest. A custom reporter is a class that implements `onRunStart`, `onTestStart`, `onTestResult`, `onRunComplete` methods that will be called when any of those events occurs.

If custom reporters are specified, the default Jest reporters will be overridden. To keep default reporters, `default` can be passed as a module name.

This will override default reporters:
```json
{
"reporters": [
"<rootDir>/my-custom-reporter.js"
]
}
```

This will use custom reporter in addition to default reporters that Jest provides:
```json
{
"reporters": [
"default",
"<rootDir>/my-custom-reporter.js"
]
}
```

Additionally, custom reporters can be configured by passing an `options` object as a second argument:
```json
{
"reporters": [
"default",
["<rootDir>/my-custom-reporter.js", {"banana": "yes", "pineapple": "no"}]
]
}
```

Custom reporter modules must define a class that takes a `GlobalConfig` and reporter options as constructor arguments:

Example reporter:
```js
// my-custom-reporter.js
class MyCustomReporter {
constructor(globalConfig, options) {
this._globalConfig = globalConfig;
this._options = options;
}

onRunComplete(contexts, results) {
console.log('Custom reporter output:');
console.log('GlobalConfig: ', this._globalConfig);
console.log('Options: ', this._options);
},
}

module.exports = MyCustomReporter;
```

Custom reporters can also force Jest to exit with non-0 code by returning an Error from `getLastError()` methods
```js
class MyCustomReporter {
// ...
getLastError() {
if (this._shouldFail) {
return new Error('my-custom-reporter.js reported an error');
}
}
}
```

For the full list of methods and argument types see `Reporter` type in [types/TestRunner.js](https://github.com/facebook/jest/blob/master/types/TestRunner.js)

### `resetMocks` [boolean]
Default: `false`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ exports[`jest --showConfig outputs config info and exits 1`] = `
"rootDir": "/mocked/root/path/jest/integration_tests/verbose_reporter",
"testPathPattern": "",
"testResultsProcessor": null,
"updateSnapshot": "all",
"useStderr": false,
"verbose": null,
"watch": false,
Expand Down
11 changes: 10 additions & 1 deletion integration_tests/__tests__/__snapshots__/snapshot-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ Ran all test suites.
"
`;
exports[`Snapshot Validation does not save snapshots in CI mode by default 1`] = `
"Test Suites: 3 failed, 3 total
Tests: 7 failed, 2 passed, 9 total
Snapshots: 9 failed, 9 total
Time: <<REPLACED>>
Ran all test suites.
"
`;
exports[`Snapshot Validation updates the snapshot when a test removes some snapshots 1`] = `
"Test Suites: 3 passed, 3 total
Tests: 9 passed, 9 total
Expand Down Expand Up @@ -72,7 +81,7 @@ Ran all test suites.
"
`;
exports[`Snapshot works as expected 1`] = `
exports[`Snapshot stores new snapshots on the first run 1`] = `
"Test Suites: 2 passed, 2 total
Tests: 5 passed, 5 total
Snapshots: 5 added, 5 total
Expand Down
7 changes: 6 additions & 1 deletion integration_tests/__tests__/showConfig-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ describe('jest --showConfig', () => {
.replace(/"cacheDirectory": "(.+)"/, '"cacheDirectory": "/tmp/jest"'),
test: val => typeof val === 'string',
});
const {stdout} = runJest(dir, ['--showConfig', '--no-cache']);
const {stdout} = runJest(dir, [
'--showConfig',
'--no-cache',
// Make the snapshot flag stable on CI.
'--updateSnapshot',
]);
expect(stdout).toMatchSnapshot();
});
});
6 changes: 5 additions & 1 deletion integration_tests/__tests__/snapshot-serializers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ const snapshotsDir = path.resolve(testDir, '__tests__/__snapshots__');
const snapshotPath = path.resolve(snapshotsDir, 'snapshot-test.js.snap');

const runAndAssert = () => {
const result = runJest.json('snapshot-serializers', ['--no-cache']);
const result = runJest.json('snapshot-serializers', [
'-w=1',
'--ci=false',
'--no-cache',
]);
const json = result.json;
expect(json.numTotalTests).toBe(7);
expect(json.numPassedTests).toBe(7);
Expand Down
66 changes: 52 additions & 14 deletions integration_tests/__tests__/snapshot-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ describe('Snapshot', () => {
beforeEach(cleanup);
afterAll(cleanup);

it('works as expected', () => {
const result = runJest.json('snapshot', []);
it('stores new snapshots on the first run', () => {
const result = runJest.json('snapshot', ['-w=1', '--ci=false']);
const json = result.json;

expect(json.numTotalTests).toBe(5);
Expand All @@ -122,7 +122,11 @@ describe('Snapshot', () => {

it('works with escaped characters', () => {
// Write the first snapshot
let result = runJest('snapshot-escape', ['snapshot-test.js']);
let result = runJest('snapshot-escape', [
'-w=1',
'--ci=false',
'snapshot-test.js',
]);
let stderr = result.stderr.toString();

expect(stderr).toMatch('1 snapshot written');
Expand All @@ -136,7 +140,12 @@ describe('Snapshot', () => {
const newTestData = initialTestData + testData;
fs.writeFileSync(snapshotEscapeTestFile, newTestData, 'utf8');

result = runJest('snapshot-escape', ['snapshot-test.js']);
result = runJest('snapshot-escape', [
'-w=1',
'--ci=false',
'--updateSnapshot',
'snapshot-test.js',
]);
stderr = result.stderr.toString();

expect(stderr).toMatch('1 snapshot written');
Expand All @@ -145,7 +154,11 @@ describe('Snapshot', () => {

// Now let's check again if everything still passes.
// If this test doesn't pass, some snapshot data was not properly escaped.
result = runJest('snapshot-escape', ['snapshot-test.js']);
result = runJest('snapshot-escape', [
'-w=1',
'--ci=false',
'snapshot-test.js',
]);
stderr = result.stderr.toString();

expect(stderr).not.toMatch('Snapshot Summary');
Expand All @@ -155,14 +168,22 @@ describe('Snapshot', () => {

it('works with escaped regex', () => {
// Write the first snapshot
let result = runJest('snapshot-escape', ['snapshot-escape-regex.js']);
let result = runJest('snapshot-escape', [
'-w=1',
'--ci=false',
'snapshot-escape-regex.js',
]);
let stderr = result.stderr.toString();

expect(stderr).toMatch('2 snapshots written in 1 test suite.');
expect(result.status).toBe(0);
expect(extractSummary(stderr).summary).toMatchSnapshot();

result = runJest('snapshot-escape', ['snapshot-escape-regex.js']);
result = runJest('snapshot-escape', [
'-w=1',
'--ci=false',
'snapshot-escape-regex.js',
]);
stderr = result.stderr.toString();

// Make sure we aren't writing a snapshot this time which would
Expand All @@ -175,6 +196,8 @@ describe('Snapshot', () => {
it('works with template literal subsitutions', () => {
// Write the first snapshot
let result = runJest('snapshot-escape', [
'-w=1',
'--ci=false',
'snapshot-escape-substitution-test.js',
]);
let stderr = result.stderr.toString();
Expand All @@ -184,6 +207,8 @@ describe('Snapshot', () => {
expect(extractSummary(stderr).summary).toMatchSnapshot();

result = runJest('snapshot-escape', [
'-w=1',
'--ci=false',
'snapshot-escape-substitution-test.js',
]);
stderr = result.stderr.toString();
Expand All @@ -200,8 +225,21 @@ describe('Snapshot', () => {
fs.writeFileSync(copyOfTestPath, originalTestContent);
});

it('does not save snapshots in CI mode by default', () => {
const result = runJest.json('snapshot', ['-w=1', '--ci=true']);

expect(result.json.success).toBe(false);
expect(result.json.numTotalTests).toBe(9);
expect(result.json.snapshot.added).toBe(0);
expect(result.json.snapshot.total).toBe(9);
const {rest, summary} = extractSummary(result.stderr.toString());

expect(rest).toMatch('New snapshot was not written');
expect(summary).toMatchSnapshot();
});

it('works on subsequent runs without `-u`', () => {
const firstRun = runJest.json('snapshot', []);
const firstRun = runJest.json('snapshot', ['-w=1', '--ci=false']);

const content = require(snapshotOfCopy);
expect(content).not.toBe(undefined);
Expand All @@ -220,12 +258,12 @@ describe('Snapshot', () => {
});

it('deletes the snapshot if the test suite has been removed', () => {
const firstRun = runJest.json('snapshot', []);
const firstRun = runJest.json('snapshot', ['-w=1', '--ci=false']);
fs.unlinkSync(copyOfTestPath);

const content = require(snapshotOfCopy);
expect(content).not.toBe(undefined);
const secondRun = runJest.json('snapshot', ['-u']);
const secondRun = runJest.json('snapshot', ['-w=1', '--ci=false', '-u']);

expect(firstRun.json.numTotalTests).toBe(9);
expect(secondRun.json.numTotalTests).toBe(5);
Expand All @@ -240,10 +278,10 @@ describe('Snapshot', () => {
});

it('deletes a snapshot when a test does removes all the snapshots', () => {
const firstRun = runJest.json('snapshot', []);
const firstRun = runJest.json('snapshot', ['-w=1', '--ci=false']);

fs.writeFileSync(copyOfTestPath, emptyTest);
const secondRun = runJest.json('snapshot', ['-u']);
const secondRun = runJest.json('snapshot', ['-w=1', '--ci=false', '-u']);
fs.unlinkSync(copyOfTestPath);

expect(firstRun.json.numTotalTests).toBe(9);
Expand All @@ -259,7 +297,7 @@ describe('Snapshot', () => {
});

it('updates the snapshot when a test removes some snapshots', () => {
const firstRun = runJest.json('snapshot', []);
const firstRun = runJest.json('snapshot', ['-w=1', '--ci=false']);
fs.unlinkSync(copyOfTestPath);
const beforeRemovingSnapshot = getSnapshotOfCopy();

Expand All @@ -270,7 +308,7 @@ describe('Snapshot', () => {
'.not.toBe(undefined)',
),
);
const secondRun = runJest.json('snapshot', ['-u']);
const secondRun = runJest.json('snapshot', ['-w=1', '--ci=false', '-u']);
fs.unlinkSync(copyOfTestPath);

expect(firstRun.json.numTotalTests).toBe(9);
Expand Down
Loading

0 comments on commit 140378c

Please sign in to comment.