Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove aliased matchers #13192

Closed
Closed
Prev Previous commit
Next Next commit
chore: remove .toBeCalled() matcher
  • Loading branch information
EduardoSCosta committed Aug 30, 2022
commit a3e4db979ef55b10b69b38954e9b3d2711b10b44
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ jest.mock('@@storage/track/Track');

test('relative import', () => {
const track = new Track();
expect(track.someRandomFunction).not.toBeCalled();
expect(track.someRandomFunction).not.toHaveBeenCalled();
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ jest.mock('@@storage/track/Track');

test('through moduleNameMapper', () => {
const track = new Track();
expect(track.someRandomFunction).not.toBeCalled();
expect(track.someRandomFunction).not.toHaveBeenCalled();
});
2 changes: 1 addition & 1 deletion examples/jquery/__tests__/display_user.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ it('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');
});
2 changes: 1 addition & 1 deletion examples/timer/__tests__/infinite_timer_game.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ it('schedules a 10-second timer after 1 second', () => {
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
8 changes: 4 additions & 4 deletions examples/timer/__tests__/timer_game.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ describe('timerGame', () => {
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).toBeCalledTimes(1);
});

Expand All @@ -40,13 +40,13 @@ describe('timerGame', () => {
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).toBeCalledTimes(1);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -819,58 +819,6 @@ Expected: not <g>undefined</>
Number of returns: <r>1</>
`;

exports[`toBeCalled .not fails with any argument passed 1`] = `
<d>expect(</><r>received</><d>).</>not<d>.</>toBeCalled<d>()</>

<b>Matcher error</>: this matcher must not have an expected argument

Expected has type: number
Expected has value: <g>555</>
`;

exports[`toBeCalled .not passes when called 1`] = `
<d>expect(</><r>spy</><d>).</>toBeCalled<d>()</>

Expected number of calls: >= <g>1</>
Received number of calls: <r>0</>
`;

exports[`toBeCalled fails with any argument passed 1`] = `
<d>expect(</><r>received</><d>).</>toBeCalled<d>()</>

<b>Matcher error</>: this matcher must not have an expected argument

Expected has type: number
Expected has value: <g>555</>
`;

exports[`toBeCalled includes the custom mock name in the error message 1`] = `
<d>expect(</><r>named-mock</><d>).</>not<d>.</>toBeCalled<d>()</>

Expected number of calls: <g>0</>
Received number of calls: <r>1</>

1: called with 0 arguments
`;

exports[`toBeCalled passes when called 1`] = `
<d>expect(</><r>jest.fn()</><d>).</>not<d>.</>toBeCalled<d>()</>

Expected number of calls: <g>0</>
Received number of calls: <r>1</>

1: <r>"arg0"</>, <r>"arg1"</>, <r>"arg2"</>
`;

exports[`toBeCalled works only on spies or jest.fn 1`] = `
<d>expect(</><r>received</><d>).</>toBeCalled<d>()</>

<b>Matcher error</>: <r>received</> value must be a mock or spy function

Received has type: function
Received has value: <r>[Function fn]</>
`;

exports[`toBeCalledTimes .not only accepts a number argument 1`] = `
<d>expect(</><r>received</><d>).</>not<d>.</>toBeCalledTimes<d>(</><g>expected</><d>)</>

Expand Down
82 changes: 45 additions & 37 deletions packages/expect/src/__tests__/spyMatchers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,53 +34,61 @@ const createSpy = (fn: jest.Mock) => {
return spy;
};

['toBeCalled', 'toHaveBeenCalled'].forEach(called => {
describe(`${called}`, () => {
test('works only on spies or jest.fn', () => {
const fn = function fn() {};
describe('toHaveBeenCalled', () => {
test('works only on spies or jest.fn', () => {
const fn = function fn() {};

expect(() => jestExpect(fn)[called]()).toThrowErrorMatchingSnapshot();
});
expect(() =>
jestExpect(fn).toHaveBeenCalled(),
).toThrowErrorMatchingSnapshot();
});

test('passes when called', () => {
const fn = jest.fn();
fn('arg0', 'arg1', 'arg2');
jestExpect(createSpy(fn))[called]();
jestExpect(fn)[called]();
expect(() => jestExpect(fn).not[called]()).toThrowErrorMatchingSnapshot();
});
test('passes when called', () => {
const fn = jest.fn();
fn('arg0', 'arg1', 'arg2');
jestExpect(createSpy(fn)).toHaveBeenCalled();
jestExpect(fn).toHaveBeenCalled();
expect(() =>
jestExpect(fn).not.toHaveBeenCalled(),
).toThrowErrorMatchingSnapshot();
});

test('.not passes when called', () => {
const fn = jest.fn();
const spy = createSpy(fn);
test('.not passes when called', () => {
const fn = jest.fn();
const spy = createSpy(fn);

jestExpect(spy).not[called]();
jestExpect(fn).not[called]();
expect(() => jestExpect(spy)[called]()).toThrowErrorMatchingSnapshot();
});
jestExpect(spy).not.toHaveBeenCalled();
jestExpect(fn).not.toHaveBeenCalled();
expect(() =>
jestExpect(spy).toHaveBeenCalled(),
).toThrowErrorMatchingSnapshot();
});

test('fails with any argument passed', () => {
const fn = jest.fn();
test('fails with any argument passed', () => {
const fn = jest.fn();

fn();
expect(() => jestExpect(fn)[called](555)).toThrowErrorMatchingSnapshot();
});
fn();
expect(() =>
jestExpect(fn).toHaveBeenCalled(555),
).toThrowErrorMatchingSnapshot();
});

test('.not fails with any argument passed', () => {
const fn = jest.fn();
test('.not fails with any argument passed', () => {
const fn = jest.fn();

expect(() =>
jestExpect(fn).not[called](555),
).toThrowErrorMatchingSnapshot();
});
expect(() =>
jestExpect(fn).not.toHaveBeenCalled(555),
).toThrowErrorMatchingSnapshot();
});

test('includes the custom mock name in the error message', () => {
const fn = jest.fn().mockName('named-mock');
test('includes the custom mock name in the error message', () => {
const fn = jest.fn().mockName('named-mock');

fn();
jestExpect(fn)[called]();
expect(() => jestExpect(fn).not[called]()).toThrowErrorMatchingSnapshot();
});
fn();
jestExpect(fn).toHaveBeenCalled();
expect(() =>
jestExpect(fn).not.toHaveBeenCalled(),
).toThrowErrorMatchingSnapshot();
});
});

Expand Down
1 change: 0 additions & 1 deletion packages/expect/src/spyMatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1140,7 +1140,6 @@ const spyMatchers: MatchersObject = {
lastReturnedWith: createLastReturnedMatcher('lastReturnedWith'),
nthCalledWith: createNthCalledWithMatcher('nthCalledWith'),
nthReturnedWith: createNthReturnedWithMatcher('nthReturnedWith'),
toBeCalled: createToBeCalledMatcher('toBeCalled'),
toBeCalledTimes: createToBeCalledTimesMatcher('toBeCalledTimes'),
toBeCalledWith: createToBeCalledWithMatcher('toBeCalledWith'),
toHaveBeenCalled: createToBeCalledMatcher('toHaveBeenCalled'),
Expand Down
4 changes: 0 additions & 4 deletions packages/expect/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,6 @@ export interface Matchers<R extends void | Promise<void>> {
* Don't use `toBe` with floating-point numbers.
*/
toBe(expected: unknown): R;
/**
* Ensures that a mock function is called.
*/
toBeCalled(): R;
/**
* Ensures that a mock function is called an exact number of times.
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-core/src/__tests__/TestScheduler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ test('should not bail if less than `n` failures', async () => {
snapshot: {},
testResults: [{}],
});
expect(setState).not.toBeCalled();
expect(setState).not.toHaveBeenCalled();
});

test('should set runInBand to run in serial', async () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-core/src/__tests__/watch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,7 @@ describe('Watch mode flows', () => {

stdin.emit('o');

expect(runJestMock).toBeCalled();
expect(runJestMock).toHaveBeenCalled();
expect(runJestMock.mock.calls[0][0].globalConfig).toMatchObject({
onlyChanged: true,
watch: true,
Expand All @@ -830,7 +830,7 @@ describe('Watch mode flows', () => {

stdin.emit('a');

expect(runJestMock).toBeCalled();
expect(runJestMock).toHaveBeenCalled();
expect(runJestMock.mock.calls[0][0].globalConfig).toMatchObject({
onlyChanged: false,
watch: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ describe('Watch mode flows', () => {
// Runs Jest again
runJestMock.mockReset();
stdin.emit(KEYS.ENTER);
expect(runJestMock).toBeCalled();
expect(runJestMock).toHaveBeenCalled();

// globalConfig is updated with the current pattern
expect(runJestMock.mock.calls[0][0].globalConfig).toMatchSnapshot();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ describe('Watch mode flows', () => {
// Runs Jest again
runJestMock.mockReset();
stdin.emit(KEYS.ENTER);
expect(runJestMock).toBeCalled();
expect(runJestMock).toHaveBeenCalled();

// globalConfig is updated with the current pattern
expect(runJestMock.mock.calls[0][0].globalConfig).toMatchObject({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1119,7 +1119,7 @@ describe('FakeTimers', () => {
}, 0);

timers.runOnlyPendingTimers();
expect(fn).not.toBeCalled();
expect(fn).not.toHaveBeenCalled();
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ describe('FakeTimers', () => {
}, 0);

timers.runOnlyPendingTimers();
expect(fn).not.toBeCalled();
expect(fn).not.toHaveBeenCalled();
});
});

Expand Down
10 changes: 5 additions & 5 deletions packages/jest-haste-map/src/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1336,7 +1336,7 @@ describe('HasteMap', () => {
],
]);

expect(mockEnd).toBeCalled();
expect(mockEnd).toHaveBeenCalled();
});

it('tries to crawl using node as a fallback', async () => {
Expand All @@ -1360,8 +1360,8 @@ describe('HasteMap', () => {
const {__hasteMapForTest: data} = await (
await HasteMap.create(defaultConfig)
).build();
expect(watchman).toBeCalled();
expect(node).toBeCalled();
expect(watchman).toHaveBeenCalled();
expect(node).toHaveBeenCalled();

expect(data.files).toEqual(
createMap({
Expand Down Expand Up @@ -1401,8 +1401,8 @@ describe('HasteMap', () => {
await HasteMap.create(defaultConfig)
).build();

expect(watchman).toBeCalled();
expect(node).toBeCalled();
expect(watchman).toHaveBeenCalled();
expect(node).toHaveBeenCalled();

expect(data.files).toEqual(
createMap({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ describe('watchman watch', () => {
const client = watchman.Client.mock.instances[0];
const calls = client.command.mock.calls;

expect(client.on).toBeCalled();
expect(client.on).toHaveBeenCalled();
expect(client.on).toBeCalledWith('error', expect.any(Function));

// Call 0 and 1 are for ['watch-project']
Expand Down Expand Up @@ -178,7 +178,7 @@ describe('watchman watch', () => {

expect(removedFiles).toEqual(new Map());

expect(client.end).toBeCalled();
expect(client.end).toHaveBeenCalled();
});

test('updates file map and removedFiles when the clock is given', async () => {
Expand Down Expand Up @@ -481,7 +481,7 @@ describe('watchman watch', () => {
const client = watchman.Client.mock.instances[0];
const calls = client.command.mock.calls;

expect(client.on).toBeCalled();
expect(client.on).toHaveBeenCalled();
expect(client.on).toBeCalledWith('error', expect.any(Function));

// First 3 calls are for ['watch-project']
Expand Down Expand Up @@ -515,7 +515,7 @@ describe('watchman watch', () => {

expect(removedFiles).toEqual(new Map());

expect(client.end).toBeCalled();
expect(client.end).toHaveBeenCalled();
});

test('SHA-1 requested and available', async () => {
Expand Down
Loading