Skip to content

Commit

Permalink
include custom mock names in error messages (#6199)
Browse files Browse the repository at this point in the history
* add tests for including custom mock names in error messages

* add current behaviour in snapshots

* include custom mock name in toBeCalled matcher

* include custom mock name in toReturn matcher

* include custom mock name in toBeCalledTimes matcher

* include custom mock name in toReturnTimes matcher

* include custom mock name in toBeCalledWith matcher

* include custom mock name in toReturnWith matcher

* include custom mock name in lastCalledWith matcher

* include custom mock name in lastReturnedWith matcher

* include custom mock name in nthCalledWith matcher

* include custom mock name in nthReturnedWith matcher

* address linting issues

* update changelog
  • Loading branch information
leggsimon authored and cpojer committed May 18, 2018
1 parent 08f7904 commit f4f5191
Show file tree
Hide file tree
Showing 4 changed files with 273 additions and 20 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@
([#5879](https://github.com/facebook/jest/pull/5879))
* `[jest-cli]` Improve snapshot summaries
([#6181](https://github.com/facebook/jest/pull/6181))
* `[expect]` Include custom mock names in error messages
([#6199](https://github.com/facebook/jest/pull/6199))

### Fixes

Expand Down
142 changes: 142 additions & 0 deletions packages/expect/src/__tests__/__snapshots__/spy_matchers.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`lastCalledWith includes the custom mock name in the error message 1`] = `
"<dim>expect(</><red>named-mock</><dim>).not.lastCalledWith(</><green>expected</><dim>)</>

Expected mock function \\"named-mock\\" to not have been last called with:
<green>[\\"foo\\", \\"bar\\"]</>"
`;

exports[`lastCalledWith works only on spies or jest.fn 1`] = `
"<dim>expect(</><red>jest.fn()</><dim>)[.not].lastCalledWith(</><dim>)</>

Expand Down Expand Up @@ -118,6 +125,14 @@ Expected mock function to have been last called with:
Did not expect argument 2 but it was called with <red>undefined</>."
`;

exports[`lastReturnedWith includes the custom mock name in the error message 1`] = `
"<dim>expect(</><red>named-mock</><dim>).lastReturnedWith(</><green>expected</><dim>)</>

Expected mock function \\"named-mock\\" to have last returned:
<green>\\"foo\\"</>
But it did <red>not return</>"
`;

exports[`lastReturnedWith works only on spies or jest.fn 1`] = `
"<dim>expect(</><red>jest.fn()</><dim>)[.not].lastReturnedWith(</><dim>)</>

Expand Down Expand Up @@ -206,6 +221,13 @@ But it last returned:
<red>\\"foo\\"</>"
`;

exports[`nthCalledWith includes the custom mock name in the error message 1`] = `
"<dim>expect(</><red>named-mock</><dim>).not.nthCalledWith(</><green>expected</><dim>)</>

Expected mock function \\"named-mock\\" first call to not have been called with:
<green>[\\"foo\\", \\"bar\\"]</>"
`;

exports[`nthCalledWith should reject non integer nth value 1`] = `"nth value <red>0.1</> must be a positive integer greater than <green>0</>"`;

exports[`nthCalledWith should reject nth value smaller than 1 1`] = `"nth value <red>0</> must be a positive integer greater than <green>0</>"`;
Expand Down Expand Up @@ -333,6 +355,14 @@ Expected mock function first call to have been called with:
Did not expect argument 2 but it was called with <red>undefined</>."
`;

exports[`nthReturnedWith includes the custom mock name in the error message 1`] = `
"<dim>expect(</><red>named-mock</><dim>).nthReturnedWith(</><green>expected</><dim>)</>

Expected mock function \\"named-mock\\" first call to have returned with:
<green>\\"foo\\"</>
But it did <red>not return</>"
`;

exports[`nthReturnedWith should reject non integer nth value 1`] = `"nth value <red>0.1</> must be a positive integer greater than <green>0</>"`;

exports[`nthReturnedWith should reject nth value smaller than 1 1`] = `"nth value <red>0</> must be a positive integer greater than <green>0</>"`;
Expand Down Expand Up @@ -474,6 +504,13 @@ Got:
number: <green>555</>"
`;

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

Expected mock function \\"named-mock\\" not to be called but it was called with:
<red>[]</>"
`;

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

Expand Down Expand Up @@ -549,6 +586,12 @@ exports[`toBeCalledTimes .not passes if function called more than expected times
Expected mock function to have been called <green>two times</>, but it was called <red>three times</>."
`;

exports[`toBeCalledTimes includes the custom mock name in the error message 1`] = `
"<dim>expect(</><red>named-mock</><dim>).toBeCalledTimes(</><green>2</><dim>)</>

Expected mock function \\"named-mock\\" to have been called <green>two times</>, but it was called <red>one time</>."
`;

exports[`toBeCalledTimes only accepts a number argument 1`] = `
"<dim>expect(</><red>received</><dim>)[.not].toBeCalledTimes(</><green>expected</><dim>)</>

Expand Down Expand Up @@ -611,6 +654,13 @@ Received:
function: <red>[Function fn]</>"
`;

exports[`toBeCalledWith includes the custom mock name in the error message 1`] = `
"<dim>expect(</><red>named-mock</><dim>).not.toBeCalledWith(</><green>expected</><dim>)</>

Expected mock function \\"named-mock\\" not to have been called with:
<green>[\\"foo\\", \\"bar\\"]</>"
`;

exports[`toBeCalledWith works only on spies or jest.fn 1`] = `
"<dim>expect(</><red>jest.fn()</><dim>)[.not].toBeCalledWith(</><dim>)</>

Expand Down Expand Up @@ -759,6 +809,13 @@ Got:
number: <green>555</>"
`;

exports[`toHaveBeenCalled includes the custom mock name in the error message 1`] = `
"<dim>expect(</><red>named-mock</><dim>).not.toHaveBeenCalled(</><dim>)</>

Expected mock function \\"named-mock\\" not to be called but it was called with:
<red>[]</>"
`;

exports[`toHaveBeenCalled passes when called 1`] = `
"<dim>expect(</><red>jest.fn()</><dim>).not.toHaveBeenCalled(</><dim>)</>

Expand Down Expand Up @@ -834,6 +891,12 @@ exports[`toHaveBeenCalledTimes .not passes if function called more than expected
Expected mock function to have been called <green>two times</>, but it was called <red>three times</>."
`;

exports[`toHaveBeenCalledTimes includes the custom mock name in the error message 1`] = `
"<dim>expect(</><red>named-mock</><dim>).toHaveBeenCalledTimes(</><green>2</><dim>)</>

Expected mock function \\"named-mock\\" to have been called <green>two times</>, but it was called <red>one time</>."
`;

exports[`toHaveBeenCalledTimes only accepts a number argument 1`] = `
"<dim>expect(</><red>received</><dim>)[.not].toHaveBeenCalledTimes(</><green>expected</><dim>)</>

Expand Down Expand Up @@ -896,6 +959,13 @@ Received:
function: <red>[Function fn]</>"
`;

exports[`toHaveBeenCalledWith includes the custom mock name in the error message 1`] = `
"<dim>expect(</><red>named-mock</><dim>).not.toHaveBeenCalledWith(</><green>expected</><dim>)</>

Expected mock function \\"named-mock\\" not to have been called with:
<green>[\\"foo\\", \\"bar\\"]</>"
`;

exports[`toHaveBeenCalledWith works only on spies or jest.fn 1`] = `
"<dim>expect(</><red>jest.fn()</><dim>)[.not].toHaveBeenCalledWith(</><dim>)</>

Expand Down Expand Up @@ -1022,6 +1092,13 @@ Expected mock function to have been called with:
Did not expect argument 2 but it was called with <red>undefined</>."
`;

exports[`toHaveBeenLastCalledWith includes the custom mock name in the error message 1`] = `
"<dim>expect(</><red>named-mock</><dim>).not.toHaveBeenLastCalledWith(</><green>expected</><dim>)</>

Expected mock function \\"named-mock\\" to not have been last called with:
<green>[\\"foo\\", \\"bar\\"]</>"
`;

exports[`toHaveBeenLastCalledWith works only on spies or jest.fn 1`] = `
"<dim>expect(</><red>jest.fn()</><dim>)[.not].toHaveBeenLastCalledWith(</><dim>)</>

Expand Down Expand Up @@ -1140,6 +1217,13 @@ Expected mock function to have been last called with:
Did not expect argument 2 but it was called with <red>undefined</>."
`;

exports[`toHaveBeenNthCalledWith includes the custom mock name in the error message 1`] = `
"<dim>expect(</><red>named-mock</><dim>).not.toHaveBeenNthCalledWith(</><green>expected</><dim>)</>

Expected mock function \\"named-mock\\" first call to not have been called with:
<green>[\\"foo\\", \\"bar\\"]</>"
`;

exports[`toHaveBeenNthCalledWith should reject non integer nth value 1`] = `"nth value <red>0.1</> must be a positive integer greater than <green>0</>"`;

exports[`toHaveBeenNthCalledWith should reject nth value smaller than 1 1`] = `"nth value <red>0</> must be a positive integer greater than <green>0</>"`;
Expand Down Expand Up @@ -1267,6 +1351,14 @@ Expected mock function first call to have been called with:
Did not expect argument 2 but it was called with <red>undefined</>."
`;

exports[`toHaveLastReturnedWith includes the custom mock name in the error message 1`] = `
"<dim>expect(</><red>named-mock</><dim>).toHaveLastReturnedWith(</><green>expected</><dim>)</>

Expected mock function \\"named-mock\\" to have last returned:
<green>\\"foo\\"</>
But it did <red>not return</>"
`;

exports[`toHaveLastReturnedWith works only on spies or jest.fn 1`] = `
"<dim>expect(</><red>jest.fn()</><dim>)[.not].toHaveLastReturnedWith(</><dim>)</>

Expand Down Expand Up @@ -1355,6 +1447,14 @@ But it last returned:
<red>\\"foo\\"</>"
`;

exports[`toHaveNthReturnedWith includes the custom mock name in the error message 1`] = `
"<dim>expect(</><red>named-mock</><dim>).toHaveNthReturnedWith(</><green>expected</><dim>)</>

Expected mock function \\"named-mock\\" first call to have returned with:
<green>\\"foo\\"</>
But it did <red>not return</>"
`;

exports[`toHaveNthReturnedWith should reject non integer nth value 1`] = `"nth value <red>0.1</> must be a positive integer greater than <green>0</>"`;

exports[`toHaveNthReturnedWith should reject nth value smaller than 1 1`] = `"nth value <red>0</> must be a positive integer greater than <green>0</>"`;
Expand Down Expand Up @@ -1496,6 +1596,13 @@ Got:
number: <green>555</>"
`;

exports[`toHaveReturned includes the custom mock name in the error message 1`] = `
"<dim>expect(</><red>named-mock</><dim>).not.toHaveReturned(</><dim>)</>

Expected mock function \\"named-mock\\" not to have returned, but it returned:
<red>42</>"
`;

exports[`toHaveReturned passes when returned 1`] = `
"<dim>expect(</><red>jest.fn()</><dim>).not.toHaveReturned(</><dim>)</>

Expand Down Expand Up @@ -1571,6 +1678,12 @@ exports[`toHaveReturnedTimes .not passes if function returned more than expected
Expected mock function to have returned <green>two times</>, but it returned <red>three times</>."
`;

exports[`toHaveReturnedTimes includes the custom mock name in the error message 1`] = `
"<dim>expect(</><red>named-mock</><dim>).toHaveReturnedTimes(</><green>1</><dim>)</>

Expected mock function \\"named-mock\\" to have returned <green>one time</>, but it returned <red>two times</>."
`;

exports[`toHaveReturnedTimes only accepts a number argument 1`] = `
"<dim>expect(</><red>received</><dim>)[.not].toHaveReturnedTimes(</><green>expected</><dim>)</>

Expand Down Expand Up @@ -1633,6 +1746,14 @@ Received:
function: <red>[Function fn]</>"
`;

exports[`toHaveReturnedWith includes the custom mock name in the error message 1`] = `
"<dim>expect(</><red>named-mock</><dim>).toHaveReturnedWith(</><green>expected</><dim>)</>

Expected mock function \\"named-mock\\" to have returned:
<green>\\"foo\\"</>
But it did <red>not return</>."
`;

exports[`toHaveReturnedWith works only on spies or jest.fn 1`] = `
"<dim>expect(</><red>jest.fn()</><dim>)[.not].toHaveReturnedWith(</><dim>)</>

Expand Down Expand Up @@ -1762,6 +1883,13 @@ Got:
number: <green>555</>"
`;

exports[`toReturn includes the custom mock name in the error message 1`] = `
"<dim>expect(</><red>named-mock</><dim>).not.toReturn(</><dim>)</>

Expected mock function \\"named-mock\\" not to have returned, but it returned:
<red>42</>"
`;

exports[`toReturn passes when returned 1`] = `
"<dim>expect(</><red>jest.fn()</><dim>).not.toReturn(</><dim>)</>

Expand Down Expand Up @@ -1837,6 +1965,12 @@ exports[`toReturnTimes .not passes if function returned more than expected times
Expected mock function to have returned <green>two times</>, but it returned <red>three times</>."
`;

exports[`toReturnTimes includes the custom mock name in the error message 1`] = `
"<dim>expect(</><red>named-mock</><dim>).toReturnTimes(</><green>1</><dim>)</>

Expected mock function \\"named-mock\\" to have returned <green>one time</>, but it returned <red>two times</>."
`;

exports[`toReturnTimes only accepts a number argument 1`] = `
"<dim>expect(</><red>received</><dim>)[.not].toReturnTimes(</><green>expected</><dim>)</>

Expand Down Expand Up @@ -1899,6 +2033,14 @@ Received:
function: <red>[Function fn]</>"
`;

exports[`toReturnWith includes the custom mock name in the error message 1`] = `
"<dim>expect(</><red>named-mock</><dim>).toReturnWith(</><green>expected</><dim>)</>

Expected mock function \\"named-mock\\" to have returned:
<green>\\"foo\\"</>
But it did <red>not return</>."
`;

exports[`toReturnWith works only on spies or jest.fn 1`] = `
"<dim>expect(</><red>jest.fn()</><dim>)[.not].toReturnWith(</><dim>)</>

Expand Down
58 changes: 58 additions & 0 deletions packages/expect/src/__tests__/spy_matchers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ const jestExpect = require('../');
jestExpect(fn).not[called](555),
).toThrowErrorMatchingSnapshot();
});

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();
});
});
});

Expand Down Expand Up @@ -118,6 +126,15 @@ const jestExpect = require('../');
jestExpect(fn)[calledTimes](2),
).toThrowErrorMatchingSnapshot();
});

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

expect(() =>
jestExpect(fn)[calledTimes](2),
).toThrowErrorMatchingSnapshot();
});
});
});

Expand Down Expand Up @@ -340,6 +357,17 @@ const jestExpect = require('../');
}).toThrowErrorMatchingSnapshot();
});
}

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

caller(jestExpect(fn)[calledWith], 'foo', 'bar');

expect(() =>
caller(jestExpect(fn).not[calledWith], 'foo', 'bar'),
).toThrowErrorMatchingSnapshot();
});
});
});

Expand Down Expand Up @@ -383,6 +411,15 @@ const jestExpect = require('../');
jestExpect(fn).not[returned](555),
).toThrowErrorMatchingSnapshot();
});

test(`includes the custom mock name in the error message`, () => {
const fn = jest.fn(() => 42).mockName('named-mock');
fn();
jestExpect(fn)[returned]();
expect(() =>
jestExpect(fn).not[returned](),
).toThrowErrorMatchingSnapshot();
});
});
});

Expand Down Expand Up @@ -456,6 +493,18 @@ const jestExpect = require('../');
jestExpect(fn)[returnedTimes](2),
).toThrowErrorMatchingSnapshot();
});

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

jestExpect(fn)[returnedTimes](2);

expect(() =>
jestExpect(fn)[returnedTimes](1),
).toThrowErrorMatchingSnapshot();
});
});
});

Expand Down Expand Up @@ -674,5 +723,14 @@ const jestExpect = require('../');
}).toThrowErrorMatchingSnapshot();
});
}

test(`includes the custom mock name in the error message`, () => {
const fn = jest.fn().mockName('named-mock');
caller(jestExpect(fn).not[returnedWith], 'foo');

expect(() =>
caller(jestExpect(fn)[returnedWith], 'foo'),
).toThrowErrorMatchingSnapshot();
});
});
});
Loading

0 comments on commit f4f5191

Please sign in to comment.