Skip to content

Commit 92e1712

Browse files
authored
Merge branch 'master' into changedFilesToContributeTo
2 parents 3ab5cc6 + e8eb1d1 commit 92e1712

50 files changed

Lines changed: 726 additions & 105 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,26 @@
55
changed since arbitrary revisions.
66
([#5312](https://github.com/facebook/jest/pull/5312))
77

8+
## jest 22.1.3
9+
10+
### Fixes
11+
12+
* `[jest-cli]` Check if the file belongs to the checked project before adding it
13+
to the list, also checking that the file name is not explicitly blacklisted
14+
([#5341](https://github.com/facebook/jest/pull/5341))
15+
* `[jest-editor-support]` Add option to spawn command in shell
16+
([#5340](https://github.com/facebook/jest/pull/5340))
17+
18+
## jest 22.1.2
19+
820
### Fixes
921

22+
* `[jest-cli]` Check if the file belongs to the checked project before adding it
23+
to the list ([#5335](https://github.com/facebook/jest/pull/5335))
1024
* `[jest-cli]` Fix `EISDIR` when a directory is passed as an argument to `jest`.
1125
([#5317](https://github.com/facebook/jest/pull/5317))
26+
* `[jest-config]` Added restoreMocks config option.
27+
([#5327](https://github.com/facebook/jest/pull/5327))
1228

1329
## jest 22.1.0
1430

docs/Configuration.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,15 @@ argument:
605605
The function should either return a path to the module that should be resolved
606606
or throw an error if the module can't be found.
607607

608+
### `restoreMocks` [boolean]
609+
610+
Default: `false`
611+
612+
Automatically restore mock state between every test. Equivalent to calling
613+
`jest.restoreAllMocks()` between each test. This will lead to any mocks having
614+
their fake implementations removed and restores their initial
615+
implementation.
616+
608617
### `rootDir` [string]
609618

610619
Default: The root of the directory containing your jest's [config file](#) _or_

docs/GettingStarted.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,12 @@ _Note: Explicitly installing `regenerator-runtime` is not needed if you use
9797
Don't forget to add a [`.babelrc`](https://babeljs.io/docs/usage/babelrc/) file
9898
in your project's root folder. For example, if you are using ES6 and
9999
[React.js](https://facebook.github.io/react/) with the
100-
[`babel-preset-es2015`](https://babeljs.io/docs/plugins/preset-es2015/) and
100+
[`babel-preset-env`](https://babeljs.io/docs/plugins/preset-env/) and
101101
[`babel-preset-react`](https://babeljs.io/docs/plugins/preset-react/) presets:
102102
103103
```json
104104
{
105-
"presets": ["es2015", "react"]
105+
"presets": ["env", "react"]
106106
}
107107
```
108108
@@ -113,16 +113,16 @@ You are now set up to use all ES6 features and React specific syntax.
113113
> `test`. It will not use `development` section like Babel does by default when
114114
> no `NODE_ENV` is set.
115115
116-
> Note: If you've turned off transpilation of ES2015 modules with the option
116+
> Note: If you've turned off transpilation of ES6 modules with the option
117117
> `{ "modules": false }`, you have to make sure to turn this on in your test
118118
> environment.
119119
120120
```json
121121
{
122-
"presets": [["es2015", {"modules": false}], "react"],
122+
"presets": [["env", {"modules": false}], "react"],
123123
"env": {
124124
"test": {
125-
"presets": [["es2015"], "react"]
125+
"presets": [["env"], "react"]
126126
}
127127
}
128128
}

docs/MockFunctionAPI.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ Beware that `mockFn.mockRestore` only works when mock was created with
9696
`jest.spyOn`. Thus you have to take care of restoration yourself when manually
9797
assigning `jest.fn()`.
9898

99+
The [`restoreMocks`](configuration.html#restoremocks-boolean) configuration
100+
option is available to restore mocks automatically between tests.
101+
99102
### `mockFn.mockImplementation(fn)`
100103

101104
Accepts a function that should be used as the implementation of the mock. The
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Tests are executed only once even in an MPR 1`] = `
4+
"PASS foo/folder/my-test-bar.js
5+
✓ bar
6+
7+
"
8+
`;

integration-tests/__tests__/__snapshots__/show_config.test.js.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ exports[`--showConfig outputs config info and exits 1`] = `
3232
\\"name\\": \\"[md5 hash]\\",
3333
\\"resetMocks\\": false,
3434
\\"resetModules\\": false,
35+
\\"restoreMocks\\": false,
3536
\\"rootDir\\": \\"<<REPLACED_ROOT_DIR>>\\",
3637
\\"roots\\": [
3738
\\"<<REPLACED_ROOT_DIR>>\\"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
'use strict';
10+
11+
const runJest = require('../runJest');
12+
13+
test('suite with auto-restore', () => {
14+
const result = runJest('auto-restore-mocks/with-auto-restore');
15+
expect(result.status).toBe(0);
16+
});
17+
18+
test('suite without auto-restore', () => {
19+
const result = runJest('auto-restore-mocks/without-auto-restore');
20+
expect(result.status).toBe(0);
21+
});

integration-tests/__tests__/cli-accepts-exact-filenames.test.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ test('CLI accepts exact filenames', () => {
4040
'./foo/baz.js',
4141
'./foo',
4242
]);
43-
const {rest, summary} = extractSummary(stderr);
43+
4444
expect(status).toBe(0);
45+
46+
const {rest, summary} = extractSummary(stderr);
47+
4548
expect(rest).toMatchSnapshot();
4649
expect(summary).toMatchSnapshot();
4750
expect(stdout).toMatchSnapshot();
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Copyright (c) 2017-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
'use strict';
11+
12+
const path = require('path');
13+
const skipOnWindows = require('../../scripts/skip_on_windows');
14+
const {extractSummary, cleanup, writeFiles} = require('../utils');
15+
const runJest = require('../runJest');
16+
17+
const DIR = path.resolve(__dirname, '../execute-tests-once-in-mpr');
18+
19+
skipOnWindows.suite();
20+
21+
beforeEach(() => cleanup(DIR));
22+
afterAll(() => cleanup(DIR));
23+
24+
test('Tests are executed only once even in an MPR', () => {
25+
// Make a global config that ignores all sub-projects.
26+
const config = {
27+
jest: {
28+
projects: ['<rootDir>', '<rootDir>/foo/*/'],
29+
testPathIgnorePatterns: ['/foo/'],
30+
testRegex: /my-test-.*\.js/.source,
31+
},
32+
};
33+
34+
// Make a child config with a special regexp to ensure we execute the tests.
35+
const childConfig = {
36+
jest: {
37+
testRegex: /my-test-.*\.js/.source,
38+
},
39+
};
40+
41+
/* eslint-disable sort-keys */
42+
writeFiles(DIR, {
43+
'foo/folder/my-test-bar.js': `test('bar', () => console.log('Bar!'));`,
44+
'foo/folder/package.json': JSON.stringify(childConfig, null, 2),
45+
46+
'foo/directory/my-test-baz.js': `test('baz', () => console.log('Baz!'));`,
47+
'foo/directory/package.json': JSON.stringify(childConfig, null, 2),
48+
49+
'foo/whatever/my-test-qux.js': `test('qux', () => console.log('Qux!'));`,
50+
'foo/whatever/package.json': JSON.stringify(childConfig, null, 2),
51+
52+
'package.json': JSON.stringify(config, null, 2),
53+
});
54+
/* eslint-enable sort-keys */
55+
56+
const {stderr, status} = runJest(DIR, ['foo/folder/my-test-bar.js']);
57+
58+
expect(status).toBe(0);
59+
60+
const {rest, summary} = extractSummary(stderr);
61+
62+
// We have only one test passed, so total should equal to one, despite we have
63+
// three projects.
64+
expect(rest).toMatchSnapshot();
65+
expect(summary).toMatch(/1 total/);
66+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
'use strict';
9+
10+
const TestClass = require('../');
11+
const localClass = new TestClass();
12+
13+
test('first test', () => {
14+
jest.spyOn(localClass, 'test').mockImplementation(() => 'ABCD');
15+
expect(localClass.test()).toEqual('ABCD');
16+
expect(localClass.test).toHaveBeenCalledTimes(1);
17+
});
18+
19+
test('second test', () => {
20+
expect(localClass.test()).toEqual('12345');
21+
expect(localClass.test.mock).toBe(undefined);
22+
});

0 commit comments

Comments
 (0)