Skip to content

Commit

Permalink
Update toHaveBeenCalledAfter to fail if second mock is never called
Browse files Browse the repository at this point in the history
  • Loading branch information
theryansmee authored and keeganwitt committed Mar 9, 2022
1 parent 7937b08 commit 505974f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/matchers/toHaveBeenCalledAfter.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function toHaveBeenCalledAfter(actual, expected) {
const smallest = ns => ns.reduce((acc, n) => (acc < n ? acc : n));

const predicate = (firstInvocationCallOrder, secondInvocationCallOrder) => {
if (firstInvocationCallOrder.length === 0) return true;
if (firstInvocationCallOrder.length === 0) return false;
if (secondInvocationCallOrder.length === 0) return false;

const firstSmallest = smallest(firstInvocationCallOrder);
Expand Down
18 changes: 9 additions & 9 deletions test/matchers/__snapshots__/toHaveBeenCalledAfter.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`.not.toHaveBeenCalledAfter fails when given a first mock has not been called 1`] = `
"<dim>expect(</><red>received</><dim>).not.toHaveBeenCalledAfter(</><green>expected</><dim>)</>
Expected first mock to not have been called after, invocationCallOrder:
<green>[]</>
Received second mock with invocationCallOrder:
<red>[]</>"
`;
exports[`.not.toHaveBeenCalledAfter fails when given first mock is called after second mock 1`] = `
"<dim>expect(</><red>received</><dim>).not.toHaveBeenCalledAfter(</><green>expected</><dim>)</>
Expand All @@ -27,6 +18,15 @@ Received second mock with invocationCallOrder:
<red>[25]</>"
`;
exports[`.toHaveBeenCalledAfter fails when given first mock has not been called 1`] = `
"<dim>expect(</><red>received</><dim>).toHaveBeenCalledAfter(</><green>expected</><dim>)</>
Expected first mock to have been called after, invocationCallOrder:
<green>[]</>
Received second mock with invocationCallOrder:
<red>[]</>"
`;
exports[`.toHaveBeenCalledAfter fails when given first mock is called before multiple calls to second mock 1`] = `
"<dim>expect(</><red>received</><dim>).toHaveBeenCalledAfter(</><green>expected</><dim>)</>
Expand Down
11 changes: 3 additions & 8 deletions test/matchers/toHaveBeenCalledAfter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import * as matcher from 'src/matchers/toHaveBeenCalledAfter';
expect.extend(matcher);

describe('.toHaveBeenCalledAfter', () => {
test('passes when given a first mock has not been called', () => {
test('fails when given first mock has not been called', () => {
const mock1 = jest.fn();
const mock2 = jest.fn();
expect(mock1).toHaveBeenCalledAfter(mock2);

expect(() => expect(mock1).toHaveBeenCalledAfter(mock2)).toThrowErrorMatchingSnapshot();
});

test('fails when given first mock that has been called and a second mock that has not been called', () => {
Expand Down Expand Up @@ -87,12 +88,6 @@ describe('.toHaveBeenCalledAfter', () => {
});

describe('.not.toHaveBeenCalledAfter', () => {
test('fails when given a first mock has not been called', () => {
const mock1 = jest.fn();
const mock2 = jest.fn();
expect(() => expect(mock1).not.toHaveBeenCalledAfter(mock2)).toThrowErrorMatchingSnapshot();
});

test('passes when given first mock that has been called and a second mock that has not been called', () => {
const mock1 = jest.fn();
const mock2 = jest.fn();
Expand Down

0 comments on commit 505974f

Please sign in to comment.