Skip to content

Commit

Permalink
docs: remove eliminated matchers from docs
Browse files Browse the repository at this point in the history
  • Loading branch information
EduardoSCosta committed Aug 29, 2022
1 parent 964fd66 commit 68a14cf
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 41 deletions.
40 changes: 9 additions & 31 deletions docs/ExpectAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ it('transitions as expected', () => {
test('map calls its argument with a non-null argument', () => {
const mock = jest.fn();
[1].map(x => mock(x));
expect(mock).toBeCalledWith(expect.anything());
expect(mock).toHaveBeenCalledWith(expect.anything());
});
```

Expand All @@ -369,7 +369,7 @@ function getCat(fn) {
test('randocall calls its callback with a class instance', () => {
const mock = jest.fn();
getCat(mock);
expect(mock).toBeCalledWith(expect.any(Cat));
expect(mock).toHaveBeenCalledWith(expect.any(Cat));
});

function randocall(fn) {
Expand All @@ -379,7 +379,7 @@ function randocall(fn) {
test('randocall calls its callback with a number', () => {
const mock = jest.fn();
randocall(mock);
expect(mock).toBeCalledWith(expect.any(Number));
expect(mock).toHaveBeenCalledWith(expect.any(Number));
});
```

Expand Down Expand Up @@ -558,7 +558,7 @@ For example, let's say that we expect an `onPress` function to be called with an
test('onPress gets called with the right thing', () => {
const onPress = jest.fn();
simulatePresses(onPress);
expect(onPress).toBeCalledWith(
expect(onPress).toHaveBeenCalledWith(
expect.objectContaining({
x: expect.any(Number),
y: expect.any(Number),
Expand Down Expand Up @@ -713,8 +713,6 @@ Although the `.toBe` matcher **checks** referential identity, it **reports** a d

### `.toHaveBeenCalled()`

Also under the alias: `.toBeCalled()`

Use `.toHaveBeenCalledWith` to ensure that a mock function was called with specific arguments. The arguments are checked with the same algorithm that `.toEqual` uses.

For example, let's say you have a `drinkAll(drink, flavour)` function that takes a `drink` function and applies it to all available beverages. You might want to check that `drink` gets called for `'lemon'`, but not for `'octopus'`, because `'octopus'` flavour is really weird and why would anything be octopus-flavoured? You can do that with this test suite:
Expand Down Expand Up @@ -743,8 +741,6 @@ describe('drinkAll', () => {

### `.toHaveBeenCalledTimes(number)`

Also under the alias: `.toBeCalledTimes(number)`

Use `.toHaveBeenCalledTimes` to ensure that a mock function got called exact number of times.

For example, let's say you have a `drinkEach(drink, Array<flavor>)` function that takes a `drink` function and applies it to array of passed beverages. You might want to check that drink function was called exact number of times. You can do that with this test suite:
Expand All @@ -759,8 +755,6 @@ test('drinkEach drinks each drink', () => {

### `.toHaveBeenCalledWith(arg1, arg2, ...)`

Also under the alias: `.toBeCalledWith()`

Use `.toHaveBeenCalledWith` to ensure that a mock function was called with specific arguments. The arguments are checked with the same algorithm that `.toEqual` uses.

For example, let's say that you can register a beverage with a `register` function, and `applyToAll(f)` should apply the function `f` to all registered beverages. To make sure this works, you could write:
Expand All @@ -777,8 +771,6 @@ test('registration applies correctly to orange La Croix', () => {

### `.toHaveBeenLastCalledWith(arg1, arg2, ...)`

Also under the alias: `.lastCalledWith(arg1, arg2, ...)`

If you have a mock function, you can use `.toHaveBeenLastCalledWith` to test what arguments it was last called with. For example, let's say you have a `applyToAllFlavors(f)` function that applies `f` to a bunch of flavors, and you want to ensure that when you call it, the last flavor it operates on is `'mango'`. You can write:

```js
Expand All @@ -791,8 +783,6 @@ test('applying to all flavors does mango last', () => {

### `.toHaveBeenNthCalledWith(nthCall, arg1, arg2, ....)`

Also under the alias: `.nthCalledWith(nthCall, arg1, arg2, ...)`

If you have a mock function, you can use `.toHaveBeenNthCalledWith` to test what arguments it was nth called with. For example, let's say you have a `drinkEach(drink, Array<flavor>)` function that applies `f` to a bunch of flavors, and you want to ensure that when you call it, the first flavor it operates on is `'lemon'` and the second one is `'octopus'`. You can write:

```js
Expand All @@ -812,8 +802,6 @@ The nth argument must be positive integer starting from 1.

### `.toHaveReturned()`

Also under the alias: `.toReturn()`

If you have a mock function, you can use `.toHaveReturned` to test that the mock function successfully returned (i.e., did not throw an error) at least one time. For example, let's say you have a mock `drink` that returns `true`. You can write:

```js
Expand All @@ -828,8 +816,6 @@ test('drinks returns', () => {

### `.toHaveReturnedTimes(number)`

Also under the alias: `.toReturnTimes(number)`

Use `.toHaveReturnedTimes` to ensure that a mock function returned successfully (i.e., did not throw an error) an exact number of times. Any calls to the mock function that throw an error are not counted toward the number of times the function returned.

For example, let's say you have a mock `drink` that returns `true`. You can write:
Expand All @@ -847,8 +833,6 @@ test('drink returns twice', () => {

### `.toHaveReturnedWith(value)`

Also under the alias: `.toReturnWith(value)`

Use `.toHaveReturnedWith` to ensure that a mock function returned a specific value.

For example, let's say you have a mock `drink` that returns the name of the beverage that was consumed. You can write:
Expand All @@ -866,8 +850,6 @@ test('drink returns La Croix', () => {

### `.toHaveLastReturnedWith(value)`

Also under the alias: `.lastReturnedWith(value)`

Use `.toHaveLastReturnedWith` to test the specific value that a mock function last returned. If the last call to the mock function threw an error, then this matcher will fail no matter what value you provided as the expected return value.

For example, let's say you have a mock `drink` that returns the name of the beverage that was consumed. You can write:
Expand All @@ -887,8 +869,6 @@ test('drink returns La Croix (Orange) last', () => {

### `.toHaveNthReturnedWith(nthCall, value)`

Also under the alias: `.nthReturnedWith(nthCall, value)`

Use `.toHaveNthReturnedWith` to test the specific value that a mock function returned for the nth call. If the nth call to the mock function threw an error, then this matcher will fail no matter what value you provided as the expected return value.

For example, let's say you have a mock `drink` that returns the name of the beverage that was consumed. You can write:
Expand Down Expand Up @@ -1344,8 +1324,6 @@ describe('the La Croix cans on my desk', () => {

### `.toThrow(error?)`

Also under the alias: `.toThrowError(error?)`

Use `.toThrow` to test that a function throws when it is called. For example, if we want to test that `drinkFlavor('octopus')` throws, because octopus flavor is too disgusting to drink, we could write:

```js
Expand Down Expand Up @@ -1389,15 +1367,15 @@ test('throws on octopus', () => {
}

// Test that the error message says "yuck" somewhere: these are equivalent
expect(drinkOctopus).toThrowError(/yuck/);
expect(drinkOctopus).toThrowError('yuck');
expect(drinkOctopus).toThrow(/yuck/);
expect(drinkOctopus).toThrow('yuck');

// Test the exact error message
expect(drinkOctopus).toThrowError(/^yuck, octopus flavor$/);
expect(drinkOctopus).toThrowError(new Error('yuck, octopus flavor'));
expect(drinkOctopus).toThrow(/^yuck, octopus flavor$/);
expect(drinkOctopus).toThrow(new Error('yuck, octopus flavor'));

// Test that we get a DisgustingFlavorError
expect(drinkOctopus).toThrowError(DisgustingFlavorError);
expect(drinkOctopus).toThrow(DisgustingFlavorError);
});
```

Expand Down
4 changes: 2 additions & 2 deletions docs/GlobalAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,11 @@ const binaryStringToNumber = binString => {
describe('binaryStringToNumber', () => {
describe('given an invalid binary string', () => {
test('composed of non-numbers throws CustomError', () => {
expect(() => binaryStringToNumber('abc')).toThrowError(CustomError);
expect(() => binaryStringToNumber('abc')).toThrow(CustomError);
});

test('with extra whitespace throws CustomError', () => {
expect(() => binaryStringToNumber(' 100')).toThrowError(CustomError);
expect(() => binaryStringToNumber(' 100')).toThrow(CustomError);
});
});

Expand Down
4 changes: 2 additions & 2 deletions docs/MockFunctionAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,8 @@ test('calculate calls add', () => {
// requiring `add`.
calculate(mockAdd, 1, 2);

expect(mockAdd).toBeCalledTimes(1);
expect(mockAdd).toBeCalledWith(1, 2);
expect(mockAdd).toHaveBeenCalledTimes(1);
expect(mockAdd).toHaveBeenCalledWith(1, 2);
});
```

Expand Down
10 changes: 5 additions & 5 deletions docs/TimerMocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ test('calls the callback after 1 second', () => {
timerGame(callback);

// At this point in time, the callback should not have been called yet
expect(callback).not.toBeCalled();
expect(callback).not.toHaveBeenCalled();

// Fast-forward until all timers have been executed
jest.runAllTimers();

// Now our callback should have been called!
expect(callback).toBeCalled();
expect(callback).toHaveBeenCalled();
expect(callback).toHaveBeenCalledTimes(1);
});
```
Expand Down Expand Up @@ -109,7 +109,7 @@ describe('infiniteTimerGame', () => {
jest.runOnlyPendingTimers();

// At this point, our 1-second timer should have fired its callback
expect(callback).toBeCalled();
expect(callback).toHaveBeenCalled();

// And it should have created a new timer to start the game over in
// 10 seconds
Expand Down Expand Up @@ -154,13 +154,13 @@ it('calls the callback after 1 second via advanceTimersByTime', () => {
timerGame(callback);

// At this point in time, the callback should not have been called yet
expect(callback).not.toBeCalled();
expect(callback).not.toHaveBeenCalled();

// Fast-forward until all timers have been executed
jest.advanceTimersByTime(1000);

// Now our callback should have been called!
expect(callback).toBeCalled();
expect(callback).toHaveBeenCalled();
expect(callback).toHaveBeenCalledTimes(1);
});
```
Expand Down
2 changes: 1 addition & 1 deletion docs/TutorialjQuery.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ test('displays a user after a click', () => {

// Assert that the fetchCurrentUser function was called, and that the
// #username span's inner text was updated as we'd expect it to.
expect(fetchCurrentUser).toBeCalled();
expect(fetchCurrentUser).toHaveBeenCalled();
expect($('#username').text()).toEqual('Johnny Cash - Logged In');
});
```
Expand Down

0 comments on commit 68a14cf

Please sign in to comment.