Skip to content

Commit 4ff102b

Browse files
authored
Merge branch 'master' into update-spy-on-property
2 parents 6d52fcb + 664cdbb commit 4ff102b

File tree

25 files changed

+332
-69
lines changed

25 files changed

+332
-69
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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
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
* `[jest-mock]` Update `spyOnProperty` to support spying on the prototype
2027
chain ([#5753](https://github.com/facebook/jest/pull/5753))
2128

docs/Configuration.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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));

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
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ exports[`--showConfig outputs config info and exits 1`] = `
4545
\\"testLocationInResults\\": false,
4646
\\"testMatch\\": [
4747
\\"**/__tests__/**/*.js?(x)\\",
48-
\\"**/?(*.)(spec|test).js?(x)\\"
48+
\\"**/?(*.)+(spec|test).js?(x)\\"
4949
],
5050
\\"testPathIgnorePatterns\\": [
5151
\\"/node_modules/\\"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright 2004-present Facebook. All Rights Reserved.
22

3-
it('adds 1 + 2 to equal 3 in TScript', ()=> {
3+
it('adds 1 + 2 to equal 3 in TScript', () => {
44
const sum = require('../covered.ts');
55
expect(sum(1, 2)).toBe(3);
66
});

0 commit comments

Comments
 (0)