Skip to content

Commit 48d097e

Browse files
committed
Merge branch 'master' into aymericbouzy
2 parents 3ed393f + b43764b commit 48d097e

Some content is hidden

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

50 files changed

+6211
-80
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package.json

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,19 @@
1616
([#5670](https://github.com/facebook/jest/pull/5670))
1717
* `[expect]` Add inverse matchers (`expect.not.arrayContaining`, etc.,
1818
[#5517](https://github.com/facebook/jest/pull/5517))
19+
* `[jest-mock]` Add tracking of return values in the `mock` property
20+
([#5738](https://github.com/facebook/jest/issues/5738))
21+
* `[expect]`Add nthCalledWith spy matcher
22+
([#5605](https://github.com/facebook/jest/pull/5605))
23+
* `[jest-cli]` Add `isSerial` property that runners can expose to specify that
24+
they can not run in parallel
25+
[#5706](https://github.com/facebook/jest/pull/5706)
1926

2027
### Fixes
2128

29+
* `[expect]` Do not rely on `instanceof RegExp`, since it will not work for
30+
RegExps created inside of a different VM
31+
([#5729](https://github.com/facebook/jest/pull/5729))
2232
* `[jest-resolve]` Update node module resolution algorithm to correctly handle
2333
symlinked paths ([#5085](https://github.com/facebook/jest/pull/5085))
2434
* `[jest-editor-support]` Update `Settings` to use spawn in shell option
@@ -42,6 +52,12 @@
4252
([#5648](https://github.com/facebook/jest/pull/5708))
4353
* `[docs]` Add docs on using `jest.mock(...)`
4454
([#5648](https://github.com/facebook/jest/pull/5648))
55+
* `[docs]` Mention Jest Puppeteer Preset
56+
([#5722](https://github.com/facebook/jest/pull/5722))
57+
* `[docs]` Add jest-community section to website
58+
([#5675](https://github.com/facebook/jest/pull/5675))
59+
* `[docs]` Add versioned docs for v22.4
60+
([##5733](https://github.com/facebook/jest/pull/#5733))
4561

4662
## 22.4.2
4763

docs/Configuration.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,14 @@ Jest attempts to scan your dependency tree once (up-front) and cache it in order
123123
to ease some of the filesystem raking that needs to happen while running tests.
124124
This config option lets you customize where Jest stores that cache data on disk.
125125

126+
### `clearMocks` [boolean]
127+
128+
Default: `false`
129+
130+
Automatically clear mock calls and instances between every test. Equivalent to
131+
calling `jest.clearAllMocks()` between each test. This does not remove any mock
132+
implementation that may have been provided.
133+
126134
### `collectCoverage` [boolean]
127135

128136
Default: `false`
@@ -494,14 +502,6 @@ _Note: When using multi project runner, it's recommended to add a `displayName`
494502
for each project. This will show the `displayName` of a project next to its
495503
tests._
496504

497-
### `clearMocks` [boolean]
498-
499-
Default: `false`
500-
501-
Automatically clear mock calls and instances between every test. Equivalent to
502-
calling `jest.clearAllMocks()` between each test. This does not remove any mock
503-
implementation that may have been provided.
504-
505505
### `reporters` [array<moduleName | [moduleName, options]>]
506506

507507
Default: `undefined`
@@ -703,6 +703,10 @@ async runTests(
703703
): Promise<void>
704704
```
705705

706+
If you need to restrict your test-runner to only run in serial rather then being
707+
executed in parallel your class should have the property `isSerial` to be set as
708+
`true`.
709+
706710
### `setupFiles` [array]
707711

708712
Default: `[]`
@@ -889,7 +893,7 @@ given to [jsdom](https://github.com/tmpvar/jsdom) such as
889893

890894
##### available in Jest **19.0.0+**
891895

892-
(default: `[ '**/__tests__/**/*.js?(x)', '**/?(*.)(spec|test).js?(x)' ]`)
896+
(default: `[ '**/__tests__/**/*.js?(x)', '**/?(*.)+(spec|test).js?(x)' ]`)
893897

894898
The glob patterns Jest uses to detect test files. By default it looks for `.js`
895899
and `.jsx` files inside of `__tests__` folders, as well as any files with a

docs/ExpectAPI.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,25 @@ test('applying to all flavors does mango last', () => {
622622
});
623623
```
624624

625+
### `.nthCalledWith(nthCall, arg1, arg2, ....)`
626+
627+
If you have a mock function, you can use `.nthCalledWith` to test what arguments
628+
it was nth called with. For example, let's say you have a
629+
`drinkEach(drink, Array<flavor>)` function that applies `f` to a bunch of
630+
flavors, and you want to ensure that when you call it, the first flavor it
631+
operates on is `'lemon'` and the second one is `'octopus'`. You can write:
632+
633+
Note that, nth argument must be positive integer starting from 1.
634+
635+
```js
636+
test('drinkEach drinks each drink', () => {
637+
const drink = jest.fn();
638+
drinkEach(drink, ['lemon', 'octopus']);
639+
expect(drink).nthCalledWith(1, 'lemon');
640+
expect(drink).nthCalledWith(2, 'octopus');
641+
});
642+
```
643+
625644
### `.toBeCloseTo(number, numDigits)`
626645

627646
Using exact equality with floating point numbers is a bad idea. Rounding means

docs/ManualMocks.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ accordingly.
5353
```
5454

5555
When a manual mock exists for a given module, Jest's module system will use that
56-
module when explicitly calling `jest.mock('moduleName')`. However, manual mocks
57-
will take precedence over node modules even if `jest.mock('moduleName')` is not
56+
module when explicitly calling `jest.mock('moduleName')`. However, when
57+
`automock` is set to `true`, the manual mock implementation will be used instead
58+
of the automatically created mock, even if `jest.mock('moduleName')` is not
5859
called. To opt out of this behavior you will need to explicitly call
5960
`jest.unmock('moduleName')` in tests that should use the actual module
6061
implementation.

docs/MockFunctionAPI.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,26 @@ Each call is represented by an array of arguments that were passed during the
2929
call.
3030

3131
For example: A mock function `f` that has been called twice, with the arguments
32-
`f('arg1', 'arg2')`, and then with the arguments `f('arg3', 'arg4')` would have
32+
`f('arg1', 'arg2')`, and then with the arguments `f('arg3', 'arg4')`, would have
3333
a `mock.calls` array that looks like this:
3434

3535
```js
3636
[['arg1', 'arg2'], ['arg3', 'arg4']];
3737
```
3838

39+
### `mockFn.mock.returnValues`
40+
41+
An array containing values that have been returned by all calls to this mock
42+
function.
43+
44+
For example: A mock function `f` that has been called twice, returning
45+
`result1`, and then returning `result2`, would have a `mock.returnValues` array
46+
that looks like this:
47+
48+
```js
49+
['result1', 'result2'];
50+
```
51+
3952
### `mockFn.mock.instances`
4053

4154
An array that contains all the object instances that have been instantiated from

docs/MockFunctions.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,17 @@ expect(mockCallback.mock.calls[0][0]).toBe(0);
4141

4242
// The first argument of the second call to the function was 1
4343
expect(mockCallback.mock.calls[1][0]).toBe(1);
44+
45+
// The return value of the first call to the function was 42
46+
expect(mockCallback.mock.returnValues[0]).toBe(42);
4447
```
4548

4649
## `.mock` property
4750

4851
All mock functions have this special `.mock` property, which is where data about
49-
how the function has been called is kept. The `.mock` property also tracks the
50-
value of `this` for each call, so it is possible to inspect this as well:
52+
how the function has been called and what the function returned is kept. The
53+
`.mock` property also tracks the value of `this` for each call, so it is
54+
possible to inspect this as well:
5155

5256
```javascript
5357
const myMock = jest.fn();
@@ -62,7 +66,7 @@ console.log(myMock.mock.instances);
6266
```
6367

6468
These mock members are very useful in tests to assert how these functions get
65-
called, or instantiated:
69+
called, instantiated, or what they returned:
6670

6771
```javascript
6872
// The function was called exactly once
@@ -74,6 +78,9 @@ expect(someMockFunction.mock.calls[0][0]).toBe('first arg');
7478
// The second arg of the first call to the function was 'second arg'
7579
expect(someMockFunction.mock.calls[0][1]).toBe('second arg');
7680

81+
// The return value of the first call to the function was 'return value'
82+
expect(someMockFunction.mock.returnValues[0]).toBe('return value');
83+
7784
// This function was instantiated exactly twice
7885
expect(someMockFunction.mock.instances.length).toBe(2);
7986

@@ -165,7 +172,7 @@ test('should fetch users', () => {
165172
const resp = {data: [{name: 'Bob'}]};
166173
axios.get.mockResolvedValue(resp);
167174

168-
// or you could use the follwing depending on your use case:
175+
// or you could use the following depending on your use case:
169176
// axios.get.mockImpementation(() => Promise.resolve(resp))
170177

171178
return Users.all().then(users => expect(users).toEqual(resp.data));

docs/Puppeteer.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,28 @@ With the [Global Setup/Teardown](Configuration.md#globalsetup-string) and
77
[Async Test Environment](Configuration.md#testenvironment-string) APIs, Jest can
88
work smoothly with [puppeteer](https://github.com/GoogleChrome/puppeteer).
99

10-
## A jest-puppeteer example
10+
## Use Puppeteer Preset
11+
12+
[Jest Puppeteer Preset](https://github.com/smooth-code/jest-puppeteer) provides
13+
all required configuration to run your tests using Puppeteer.
14+
15+
1. First install `jest-puppeteer-preset`
16+
17+
```
18+
yarn add --dev jest-puppeteer-preset
19+
```
20+
21+
2. Specify preset in your Jest configuration:
22+
23+
```json
24+
{
25+
"preset": "jest-puppeteer-preset"
26+
}
27+
```
28+
29+
See [documentation](https://github.com/smooth-code/jest-puppeteer).
30+
31+
## Custom example
1132

1233
The basic idea is to:
1334

docs/TestingFrameworks.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,4 @@ about integrating Jest into other popular JS libraries.
3434

3535
## Redux
3636

37-
* [Writing Tests](http://redux.js.org/docs/recipes/WritingTests.html) by Redux
38-
docs
37+
* [Writing Tests](https://redux.js.org/recipes/writing-tests) by Redux docs

examples/typescript/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717
"transform": {
1818
"^.+\\.(ts|tsx)$": "<rootDir>/preprocessor.js"
1919
},
20-
"testMatch": ["**/__tests__/*.(ts|tsx|js)"]
20+
"testMatch": ["**/__tests__/*.+(ts|tsx|js)"]
2121
}
2222
}

0 commit comments

Comments
 (0)