From 7aa8cf8d4fad84aba03f018dea6c071636affeeb Mon Sep 17 00:00:00 2001 From: elad_yosifon Date: Sun, 18 Feb 2018 18:36:58 +0200 Subject: [PATCH 1/2] add support for expect(spy).nthCalledWith(int, args...) --- .../__snapshots__/spy_matchers.test.js.snap | 302 ++++++++++++++++++ .../expect/src/__tests__/spy_matchers.test.js | 236 +++++++++----- packages/expect/src/spy_matchers.js | 54 ++++ 3 files changed, 512 insertions(+), 80 deletions(-) diff --git a/packages/expect/src/__tests__/__snapshots__/spy_matchers.test.js.snap b/packages/expect/src/__tests__/__snapshots__/spy_matchers.test.js.snap index 484e8f52260e..1b85c3521e01 100644 --- a/packages/expect/src/__tests__/__snapshots__/spy_matchers.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/spy_matchers.test.js.snap @@ -72,6 +72,27 @@ Expected mock function to not have been last called with: [\\"foo\\", \\"bar\\"]" `; +exports[`lastCalledWith works with many arguments 2`] = ` +"expect(jest.fn()).not.lastCalledWith(expected) + +Expected mock function to not have been last called with: + [\\"foo\\", \\"bar\\"]" +`; + +exports[`lastCalledWith works with many arguments 3`] = ` +"expect(jest.fn()).not.lastCalledWith(expected) + +Expected mock function to not have been last called with: + [\\"foo\\", \\"bar\\"]" +`; + +exports[`lastCalledWith works with many arguments 4`] = ` +"expect(jest.fn()).not.lastCalledWith(expected) + +Expected mock function to not have been last called with: + [\\"foo\\", \\"bar\\"]" +`; + exports[`lastCalledWith works with many arguments that don't match 1`] = ` "expect(jest.fn()).lastCalledWith(expected) @@ -79,6 +100,27 @@ Expected mock function to have been last called with: \\"bar\\" as argument 2, but it was called with \\"bar3\\"." `; +exports[`lastCalledWith works with many arguments that don't match 2`] = ` +"expect(jest.fn()).lastCalledWith(expected) + +Expected mock function to have been last called with: + \\"bar\\" as argument 2, but it was called with \\"bar3\\"." +`; + +exports[`lastCalledWith works with many arguments that don't match 3`] = ` +"expect(jest.fn()).lastCalledWith(expected) + +Expected mock function to have been last called with: + \\"bar\\" as argument 2, but it was called with \\"bar3\\"." +`; + +exports[`lastCalledWith works with many arguments that don't match 4`] = ` +"expect(jest.fn()).lastCalledWith(expected) + +Expected mock function to have been last called with: + \\"bar\\" as argument 2, but it was called with \\"bar3\\"." +`; + exports[`lastCalledWith works with trailing undefined arguments 1`] = ` "expect(jest.fn()).lastCalledWith(expected) @@ -86,6 +128,170 @@ Expected mock function to have been last called with: Did not expect argument 2 but it was called with undefined." `; +exports[`nthCalledWith nthCalledWith 1`] = ` +"expect(jest.fn()).not.nthCalledWith(expected) + +Expected mock function first call to not have been called with: + [\\"foo1\\", \\"bar\\"]" +`; + +exports[`nthCalledWith nthCalledWith 2`] = ` +"expect(jest.fn()).not.nthCalledWith(expected) + +Expected mock function first call to not have been called with: + [\\"foo1\\", \\"bar\\"]" +`; + +exports[`nthCalledWith nthCalledWith 3`] = ` +"expect(jest.fn()).not.nthCalledWith(expected) + +Expected mock function first call to not have been called with: + [\\"foo1\\", \\"bar\\"]" +`; + +exports[`nthCalledWith nthCalledWith 4`] = ` +"expect(jest.fn()).not.nthCalledWith(expected) + +Expected mock function first call to not have been called with: + [\\"foo1\\", \\"bar\\"]" +`; + +exports[`nthCalledWith should reject non integer nth value 1`] = `"nth value must be a positive integer greater than 0"`; + +exports[`nthCalledWith should reject non integer nth value 2`] = `"nth value must be a positive integer greater than 0"`; + +exports[`nthCalledWith should reject non integer nth value 3`] = `"nth value must be a positive integer greater than 0"`; + +exports[`nthCalledWith should reject non integer nth value 4`] = `"nth value must be a positive integer greater than 0"`; + +exports[`nthCalledWith should reject nth value smaller than 1 1`] = `"nth value must be a positive integer greater than 0"`; + +exports[`nthCalledWith should reject nth value smaller than 1 2`] = `"nth value must be a positive integer greater than 0"`; + +exports[`nthCalledWith should reject nth value smaller than 1 3`] = `"nth value must be a positive integer greater than 0"`; + +exports[`nthCalledWith should reject nth value smaller than 1 4`] = `"nth value must be a positive integer greater than 0"`; + +exports[`nthCalledWith should replace 1st, 2nd, 3rd with first, second, third 1`] = ` +"expect(jest.fn()).nthCalledWith(expected) + +Expected mock function first call to have been called with: +" +`; + +exports[`nthCalledWith should replace 1st, 2nd, 3rd with first, second, third 2`] = ` +"expect(jest.fn()).not.nthCalledWith(expected) + +Expected mock function first call to not have been called with: + [\\"foo1\\", \\"bar\\"]" +`; + +exports[`nthCalledWith should replace 1st, 2nd, 3rd with first, second, third 3`] = ` +"expect(jest.fn()).nthCalledWith(expected) + +Expected mock function first call to have been called with: +" +`; + +exports[`nthCalledWith should replace 1st, 2nd, 3rd with first, second, third 4`] = ` +"expect(jest.fn()).not.nthCalledWith(expected) + +Expected mock function first call to not have been called with: + [\\"foo1\\", \\"bar\\"]" +`; + +exports[`nthCalledWith should replace 1st, 2nd, 3rd with first, second, third 5`] = ` +"expect(jest.fn()).nthCalledWith(expected) + +Expected mock function first call to have been called with: +" +`; + +exports[`nthCalledWith should replace 1st, 2nd, 3rd with first, second, third 6`] = ` +"expect(jest.fn()).not.nthCalledWith(expected) + +Expected mock function first call to not have been called with: + [\\"foo1\\", \\"bar\\"]" +`; + +exports[`nthCalledWith should replace 1st, 2nd, 3rd with first, second, third 7`] = ` +"expect(jest.fn()).nthCalledWith(expected) + +Expected mock function first call to have been called with: +" +`; + +exports[`nthCalledWith should replace 1st, 2nd, 3rd with first, second, third 8`] = ` +"expect(jest.fn()).not.nthCalledWith(expected) + +Expected mock function first call to not have been called with: + [\\"foo1\\", \\"bar\\"]" +`; + +exports[`nthCalledWith works when not called 1`] = ` +"expect(jest.fn()).nthCalledWith(expected) + +Expected mock function first call to have been called with: + [\\"foo\\", \\"bar\\"] +But it was not called." +`; + +exports[`nthCalledWith works with Immutable.js objects 1`] = ` +"expect(jest.fn()).not.nthCalledWith(expected) + +Expected mock function first call to not have been called with: + [Immutable.Map {\\"a\\": {\\"b\\": \\"c\\"}}, Immutable.Map {\\"a\\": {\\"b\\": \\"c\\"}}]" +`; + +exports[`nthCalledWith works with Map 1`] = ` +"expect(jest.fn()).not.nthCalledWith(expected) + +Expected mock function first call to not have been called with: + [Map {1 => 2, 2 => 1}]" +`; + +exports[`nthCalledWith works with Map 2`] = ` +"expect(jest.fn()).nthCalledWith(expected) + +Expected mock function first call to have been called with: + Map {\\"a\\" => \\"b\\", \\"b\\" => \\"a\\"} as argument 1, but it was called with Map {1 => 2, 2 => 1}." +`; + +exports[`nthCalledWith works with Set 1`] = ` +"expect(jest.fn()).not.nthCalledWith(expected) + +Expected mock function first call to not have been called with: + [Set {1, 2}]" +`; + +exports[`nthCalledWith works with Set 2`] = ` +"expect(jest.fn()).nthCalledWith(expected) + +Expected mock function first call to have been called with: + Set {3, 4} as argument 1, but it was called with Set {1, 2}." +`; + +exports[`nthCalledWith works with arguments that don't match 1`] = ` +"expect(jest.fn()).nthCalledWith(expected) + +Expected mock function first call to have been called with: + \\"bar\\" as argument 2, but it was called with \\"bar1\\"." +`; + +exports[`nthCalledWith works with arguments that match 1`] = ` +"expect(jest.fn()).not.nthCalledWith(expected) + +Expected mock function first call to not have been called with: + [\\"foo\\", \\"bar\\"]" +`; + +exports[`nthCalledWith works with trailing undefined arguments 1`] = ` +"expect(jest.fn()).nthCalledWith(expected) + +Expected mock function first call to have been called with: + Did not expect argument 2 but it was called with undefined." +`; + exports[`toBeCalled works only on spies or jest.fn 1`] = ` "expect(jest.fn())[.not].toBeCalled() @@ -298,6 +504,27 @@ Expected mock function not to have been called with: [\\"foo\\", \\"bar\\"]" `; +exports[`toHaveBeenCalledWith works with many arguments 2`] = ` +"expect(jest.fn()).not.toHaveBeenCalledWith(expected) + +Expected mock function not to have been called with: + [\\"foo\\", \\"bar\\"]" +`; + +exports[`toHaveBeenCalledWith works with many arguments 3`] = ` +"expect(jest.fn()).not.toHaveBeenCalledWith(expected) + +Expected mock function not to have been called with: + [\\"foo\\", \\"bar\\"]" +`; + +exports[`toHaveBeenCalledWith works with many arguments 4`] = ` +"expect(jest.fn()).not.toHaveBeenCalledWith(expected) + +Expected mock function not to have been called with: + [\\"foo\\", \\"bar\\"]" +`; + exports[`toHaveBeenCalledWith works with many arguments that don't match 1`] = ` "expect(jest.fn()).toHaveBeenCalledWith(expected) @@ -309,6 +536,39 @@ Expected mock function to have been called with: \\"bar\\" as argument 2, but it was called with \\"bar1\\"." `; +exports[`toHaveBeenCalledWith works with many arguments that don't match 2`] = ` +"expect(jest.fn()).toHaveBeenCalledWith(expected) + +Expected mock function to have been called with: + \\"bar\\" as argument 2, but it was called with \\"bar3\\". + + \\"bar\\" as argument 2, but it was called with \\"bar2\\". + + \\"bar\\" as argument 2, but it was called with \\"bar1\\"." +`; + +exports[`toHaveBeenCalledWith works with many arguments that don't match 3`] = ` +"expect(jest.fn()).toHaveBeenCalledWith(expected) + +Expected mock function to have been called with: + \\"bar\\" as argument 2, but it was called with \\"bar3\\". + + \\"bar\\" as argument 2, but it was called with \\"bar2\\". + + \\"bar\\" as argument 2, but it was called with \\"bar1\\"." +`; + +exports[`toHaveBeenCalledWith works with many arguments that don't match 4`] = ` +"expect(jest.fn()).toHaveBeenCalledWith(expected) + +Expected mock function to have been called with: + \\"bar\\" as argument 2, but it was called with \\"bar3\\". + + \\"bar\\" as argument 2, but it was called with \\"bar2\\". + + \\"bar\\" as argument 2, but it was called with \\"bar1\\"." +`; + exports[`toHaveBeenCalledWith works with trailing undefined arguments 1`] = ` "expect(jest.fn()).toHaveBeenCalledWith(expected) @@ -388,6 +648,27 @@ Expected mock function to not have been last called with: [\\"foo\\", \\"bar\\"]" `; +exports[`toHaveBeenLastCalledWith works with many arguments 2`] = ` +"expect(jest.fn()).not.toHaveBeenLastCalledWith(expected) + +Expected mock function to not have been last called with: + [\\"foo\\", \\"bar\\"]" +`; + +exports[`toHaveBeenLastCalledWith works with many arguments 3`] = ` +"expect(jest.fn()).not.toHaveBeenLastCalledWith(expected) + +Expected mock function to not have been last called with: + [\\"foo\\", \\"bar\\"]" +`; + +exports[`toHaveBeenLastCalledWith works with many arguments 4`] = ` +"expect(jest.fn()).not.toHaveBeenLastCalledWith(expected) + +Expected mock function to not have been last called with: + [\\"foo\\", \\"bar\\"]" +`; + exports[`toHaveBeenLastCalledWith works with many arguments that don't match 1`] = ` "expect(jest.fn()).toHaveBeenLastCalledWith(expected) @@ -395,6 +676,27 @@ Expected mock function to have been last called with: \\"bar\\" as argument 2, but it was called with \\"bar3\\"." `; +exports[`toHaveBeenLastCalledWith works with many arguments that don't match 2`] = ` +"expect(jest.fn()).toHaveBeenLastCalledWith(expected) + +Expected mock function to have been last called with: + \\"bar\\" as argument 2, but it was called with \\"bar3\\"." +`; + +exports[`toHaveBeenLastCalledWith works with many arguments that don't match 3`] = ` +"expect(jest.fn()).toHaveBeenLastCalledWith(expected) + +Expected mock function to have been last called with: + \\"bar\\" as argument 2, but it was called with \\"bar3\\"." +`; + +exports[`toHaveBeenLastCalledWith works with many arguments that don't match 4`] = ` +"expect(jest.fn()).toHaveBeenLastCalledWith(expected) + +Expected mock function to have been last called with: + \\"bar\\" as argument 2, but it was called with \\"bar3\\"." +`; + exports[`toHaveBeenLastCalledWith works with trailing undefined arguments 1`] = ` "expect(jest.fn()).toHaveBeenLastCalledWith(expected) diff --git a/packages/expect/src/__tests__/spy_matchers.test.js b/packages/expect/src/__tests__/spy_matchers.test.js index 5e200edd834d..81a3bef89668 100644 --- a/packages/expect/src/__tests__/spy_matchers.test.js +++ b/packages/expect/src/__tests__/spy_matchers.test.js @@ -99,36 +99,131 @@ describe('toHaveBeenCalledTimes', () => { }); }); -['lastCalledWith', 'toHaveBeenCalledWith', 'toHaveBeenLastCalledWith'].forEach( - calledWith => { - test(`${calledWith} works when not called`, () => { - const fn = jest.fn(); - jestExpect(fn).not[calledWith]('foo', 'bar'); +[ + 'lastCalledWith', + 'nthCalledWith', + 'toHaveBeenCalledWith', + 'toHaveBeenLastCalledWith', +].forEach(calledWith => { + const caller = function(callee, ...args) { + if (calledWith == 'nthCalledWith') { + callee.apply(null, [1, ...args]); + } else { + callee.apply(null, args); + } + }; + test(`${calledWith} works when not called`, () => { + const fn = jest.fn(); + caller(jestExpect(fn).not[calledWith], 'foo', 'bar'); - expect(() => - jestExpect(fn)[calledWith]('foo', 'bar'), - ).toThrowErrorMatchingSnapshot(); - }); + expect(() => + caller(jestExpect(fn)[calledWith], 'foo', 'bar'), + ).toThrowErrorMatchingSnapshot(); + }); - test(`${calledWith} works with no arguments`, () => { - const fn = jest.fn(); - fn(); - jestExpect(fn)[calledWith](); - }); + test(`${calledWith} works with no arguments`, () => { + const fn = jest.fn(); + fn(); + caller(jestExpect(fn)[calledWith]); + }); - test(`${calledWith} works with arguments that don't match`, () => { - const fn = jest.fn(); - fn('foo', 'bar1'); + test(`${calledWith} works with arguments that don't match`, () => { + const fn = jest.fn(); + fn('foo', 'bar1'); - jestExpect(fn).not[calledWith]('foo', 'bar'); + caller(jestExpect(fn).not[calledWith], 'foo', 'bar'); - expect(() => - jestExpect(fn)[calledWith]('foo', 'bar'), - ).toThrowErrorMatchingSnapshot(); - }); + expect(() => + caller(jestExpect(fn)[calledWith], 'foo', 'bar'), + ).toThrowErrorMatchingSnapshot(); + }); + + test(`${calledWith} works with arguments that match`, () => { + const fn = jest.fn(); + fn('foo', 'bar'); + + caller(jestExpect(fn)[calledWith], 'foo', 'bar'); + + expect(() => + caller(jestExpect(fn).not[calledWith], 'foo', 'bar'), + ).toThrowErrorMatchingSnapshot(); + }); + + test(`${calledWith} works with trailing undefined arguments`, () => { + const fn = jest.fn(); + fn('foo', undefined); + + expect(() => + caller(jestExpect(fn)[calledWith], 'foo'), + ).toThrowErrorMatchingSnapshot(); + }); + + test(`${calledWith} works with Map`, () => { + const fn = jest.fn(); + + const m1 = new Map([[1, 2], [2, 1]]); + const m2 = new Map([[1, 2], [2, 1]]); + const m3 = new Map([['a', 'b'], ['b', 'a']]); + + fn(m1); + + caller(jestExpect(fn)[calledWith], m2); + caller(jestExpect(fn).not[calledWith], m3); + + expect(() => + caller(jestExpect(fn).not[calledWith], m2), + ).toThrowErrorMatchingSnapshot(); + expect(() => + caller(jestExpect(fn)[calledWith], m3), + ).toThrowErrorMatchingSnapshot(); + }); + + test(`${calledWith} works with Set`, () => { + const fn = jest.fn(); + + const s1 = new Set([1, 2]); + const s2 = new Set([1, 2]); + const s3 = new Set([3, 4]); + + fn(s1); + + caller(jestExpect(fn)[calledWith], s2); + caller(jestExpect(fn).not[calledWith], s3); + + expect(() => + caller(jestExpect(fn).not[calledWith], s2), + ).toThrowErrorMatchingSnapshot(); + expect(() => + caller(jestExpect(fn)[calledWith], s3), + ).toThrowErrorMatchingSnapshot(); + }); + + test(`${calledWith} works with Immutable.js objects`, () => { + const fn = jest.fn(); + const directlyCreated = new Immutable.Map([['a', {b: 'c'}]]); + const indirectlyCreated = new Immutable.Map().set('a', {b: 'c'}); + fn(directlyCreated, indirectlyCreated); + + caller(jestExpect(fn)[calledWith], indirectlyCreated, directlyCreated); + + expect(() => + caller( + jestExpect(fn).not[calledWith], + indirectlyCreated, + directlyCreated, + ), + ).toThrowErrorMatchingSnapshot(); + }); - test(`${calledWith} works with arguments that match`, () => { + [ + 'lastCalledWith', + 'toHaveBeenCalledWith', + 'toHaveBeenLastCalledWith', + ].forEach(calledWith => { + test(`${calledWith} works with many arguments`, () => { const fn = jest.fn(); + fn('foo1', 'bar'); + fn('foo', 'bar1'); fn('foo', 'bar'); jestExpect(fn)[calledWith]('foo', 'bar'); @@ -150,80 +245,61 @@ describe('toHaveBeenCalledTimes', () => { jestExpect(fn)[calledWith]('foo', 'bar'), ).toThrowErrorMatchingSnapshot(); }); + }); - test(`${calledWith} works with many arguments`, () => { + describe('nthCalledWith', () => { + test(`nthCalledWith`, () => { const fn = jest.fn(); fn('foo1', 'bar'); fn('foo', 'bar1'); fn('foo', 'bar'); - jestExpect(fn)[calledWith]('foo', 'bar'); - - expect(() => - jestExpect(fn).not[calledWith]('foo', 'bar'), - ).toThrowErrorMatchingSnapshot(); - }); - - test(`${calledWith} works with trailing undefined arguments`, () => { - const fn = jest.fn(); - fn('foo', undefined); + jestExpect(fn).nthCalledWith(1, 'foo1', 'bar'); + jestExpect(fn).nthCalledWith(2, 'foo', 'bar1'); + jestExpect(fn).nthCalledWith(3, 'foo', 'bar'); - expect(() => - jestExpect(fn)[calledWith]('foo'), - ).toThrowErrorMatchingSnapshot(); + expect(() => { + jestExpect(fn).not.nthCalledWith(1, 'foo1', 'bar'), + jestExpect(fn).not.nthCalledWith(2, 'foo', 'bar1'), + jestExpect(fn).not.nthCalledWith(3, 'foo', 'bar'); + }).toThrowErrorMatchingSnapshot(); }); - test(`${calledWith} works with Map`, () => { + it('should replace 1st, 2nd, 3rd with first, second, third', async () => { const fn = jest.fn(); + fn('foo1', 'bar'); + fn('foo', 'bar1'); + fn('foo', 'bar'); - const m1 = new Map([[1, 2], [2, 1]]); - const m2 = new Map([[1, 2], [2, 1]]); - const m3 = new Map([['a', 'b'], ['b', 'a']]); - - fn(m1); - - jestExpect(fn)[calledWith](m2); - jestExpect(fn).not[calledWith](m3); - - expect(() => - jestExpect(fn).not[calledWith](m2), - ).toThrowErrorMatchingSnapshot(); - expect(() => - jestExpect(fn)[calledWith](m3), - ).toThrowErrorMatchingSnapshot(); + expect(() => { + jestExpect(fn).nthCalledWith(1, 'foo', 'bar'); + jestExpect(fn).nthCalledWith(2, 'foo', 'bar'); + jestExpect(fn).nthCalledWith(3, 'foo1', 'bar'); + }).toThrowErrorMatchingSnapshot(); + + expect(() => { + jestExpect(fn).not.nthCalledWith(1, 'foo1', 'bar'), + jestExpect(fn).not.nthCalledWith(2, 'foo', 'bar1'), + jestExpect(fn).not.nthCalledWith(3, 'foo', 'bar'); + }).toThrowErrorMatchingSnapshot(); }); - test(`${calledWith} works with Set`, () => { + it('should reject nth value smaller than 1', async () => { const fn = jest.fn(); + fn('foo1', 'bar'); - const s1 = new Set([1, 2]); - const s2 = new Set([1, 2]); - const s3 = new Set([3, 4]); - - fn(s1); - - jestExpect(fn)[calledWith](s2); - jestExpect(fn).not[calledWith](s3); - - expect(() => - jestExpect(fn).not[calledWith](s2), - ).toThrowErrorMatchingSnapshot(); - expect(() => - jestExpect(fn)[calledWith](s3), - ).toThrowErrorMatchingSnapshot(); + expect(() => { + jestExpect(fn).nthCalledWith(0, 'foo1', 'bar'); + }).toThrowErrorMatchingSnapshot(); }); - test(`${calledWith} works with Immutable.js objects`, () => { + it('should reject non integer nth value', async () => { const fn = jest.fn(); - const directlyCreated = new Immutable.Map([['a', {b: 'c'}]]); - const indirectlyCreated = new Immutable.Map().set('a', {b: 'c'}); - fn(directlyCreated, indirectlyCreated); - - jestExpect(fn)[calledWith](indirectlyCreated, directlyCreated); + fn('foo1', 'bar'); - expect(() => - jestExpect(fn).not[calledWith](indirectlyCreated, directlyCreated), - ).toThrowErrorMatchingSnapshot(); + expect(() => { + jestExpect(fn).nthCalledWith(0.1, 'foo1', 'bar'); + }).toThrowErrorMatchingSnapshot(); }); - }, -); + }); +}); diff --git a/packages/expect/src/spy_matchers.js b/packages/expect/src/spy_matchers.js index aaab97821d89..210563a82cf4 100644 --- a/packages/expect/src/spy_matchers.js +++ b/packages/expect/src/spy_matchers.js @@ -117,6 +117,44 @@ const createLastCalledWithMatcher = matcherName => ( const spyMatchers: MatchersObject = { lastCalledWith: createLastCalledWithMatcher('.lastCalledWith'), + nthCalledWith(received: any, nth: number, ...expected: any) { + const matcherName = '.nthCalledWith'; + ensureMock(received, matcherName); + + const receivedIsSpy = isSpy(received); + const type = receivedIsSpy ? 'spy' : 'mock function'; + + if (!isPositiveInteger(nth) || nth < 1) { + const message = () => + `nth value must be a positive integer greater than 0`; + const pass = false; + return {message, pass}; + } + + const receivedName = receivedIsSpy ? 'spy' : received.getMockName(); + const calls = receivedIsSpy + ? received.calls.all().map(x => x.args) + : received.mock.calls; + const pass = equals(calls[nth - 1], expected, [iterableEquality]); + + const message = pass + ? () => + matcherHint('.not' + matcherName, receivedName) + + '\n\n' + + `Expected ${type} ${nthToString( + nth, + )} call to not have been called with:\n` + + ` ${printExpected(expected)}` + : () => + matcherHint(matcherName, receivedName) + + '\n\n' + + `Expected ${type} ${nthToString( + nth, + )} call to have been called with:\n` + + formatMismatchedCalls(calls, expected, LAST_CALL_PRINT_LIMIT); + + return {message, pass}; + }, toBeCalled: createToBeCalledMatcher('.toBeCalled'), toBeCalledWith: createToBeCalledWithMatcher('.toBeCalledWith'), toHaveBeenCalled: createToBeCalledMatcher('.toHaveBeenCalled'), @@ -230,4 +268,20 @@ const formatMismatchedArgs = (expected, received) => { return printedArgs.join('\n'); }; +const isPositiveInteger = (integer: number) => { + return typeof integer === 'number' && parseInt(integer) === integer; +}; + +const nthToString = (nth: number) => { + switch (nth) { + case 1: + return 'first'; + case 2: + return 'second'; + case 3: + return 'third'; + } + return `${nth}th`; +}; + export default spyMatchers; From de8ca5d319bb6ac2082b1147b1d1343c42033bbc Mon Sep 17 00:00:00 2001 From: elad_yosifon Date: Mon, 26 Feb 2018 11:19:10 +0200 Subject: [PATCH 2/2] CR changes --- .../__snapshots__/spy_matchers.test.js.snap | 16 ++++++++-------- packages/expect/src/spy_matchers.js | 10 ++++------ 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/packages/expect/src/__tests__/__snapshots__/spy_matchers.test.js.snap b/packages/expect/src/__tests__/__snapshots__/spy_matchers.test.js.snap index 1b85c3521e01..650e63d8826f 100644 --- a/packages/expect/src/__tests__/__snapshots__/spy_matchers.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/spy_matchers.test.js.snap @@ -156,21 +156,21 @@ Expected mock function first call to not have been called with: [\\"foo1\\", \\"bar\\"]" `; -exports[`nthCalledWith should reject non integer nth value 1`] = `"nth value must be a positive integer greater than 0"`; +exports[`nthCalledWith should reject non integer nth value 1`] = `"nth value 0.1 must be a positive integer greater than 0"`; -exports[`nthCalledWith should reject non integer nth value 2`] = `"nth value must be a positive integer greater than 0"`; +exports[`nthCalledWith should reject non integer nth value 2`] = `"nth value 0.1 must be a positive integer greater than 0"`; -exports[`nthCalledWith should reject non integer nth value 3`] = `"nth value must be a positive integer greater than 0"`; +exports[`nthCalledWith should reject non integer nth value 3`] = `"nth value 0.1 must be a positive integer greater than 0"`; -exports[`nthCalledWith should reject non integer nth value 4`] = `"nth value must be a positive integer greater than 0"`; +exports[`nthCalledWith should reject non integer nth value 4`] = `"nth value 0.1 must be a positive integer greater than 0"`; -exports[`nthCalledWith should reject nth value smaller than 1 1`] = `"nth value must be a positive integer greater than 0"`; +exports[`nthCalledWith should reject nth value smaller than 1 1`] = `"nth value 0 must be a positive integer greater than 0"`; -exports[`nthCalledWith should reject nth value smaller than 1 2`] = `"nth value must be a positive integer greater than 0"`; +exports[`nthCalledWith should reject nth value smaller than 1 2`] = `"nth value 0 must be a positive integer greater than 0"`; -exports[`nthCalledWith should reject nth value smaller than 1 3`] = `"nth value must be a positive integer greater than 0"`; +exports[`nthCalledWith should reject nth value smaller than 1 3`] = `"nth value 0 must be a positive integer greater than 0"`; -exports[`nthCalledWith should reject nth value smaller than 1 4`] = `"nth value must be a positive integer greater than 0"`; +exports[`nthCalledWith should reject nth value smaller than 1 4`] = `"nth value 0 must be a positive integer greater than 0"`; exports[`nthCalledWith should replace 1st, 2nd, 3rd with first, second, third 1`] = ` "expect(jest.fn()).nthCalledWith(expected) diff --git a/packages/expect/src/spy_matchers.js b/packages/expect/src/spy_matchers.js index 210563a82cf4..ae084e55087c 100644 --- a/packages/expect/src/spy_matchers.js +++ b/packages/expect/src/spy_matchers.js @@ -124,9 +124,11 @@ const spyMatchers: MatchersObject = { const receivedIsSpy = isSpy(received); const type = receivedIsSpy ? 'spy' : 'mock function'; - if (!isPositiveInteger(nth) || nth < 1) { + if (typeof nth !== 'number' || parseInt(nth) !== nth || nth < 1) { const message = () => - `nth value must be a positive integer greater than 0`; + `nth value ${printReceived( + nth, + )} must be a positive integer greater than ${printExpected(0)}`; const pass = false; return {message, pass}; } @@ -268,10 +270,6 @@ const formatMismatchedArgs = (expected, received) => { return printedArgs.join('\n'); }; -const isPositiveInteger = (integer: number) => { - return typeof integer === 'number' && parseInt(integer) === integer; -}; - const nthToString = (nth: number) => { switch (nth) { case 1: