From 1ed750e994d207b8fba3c09f4fc332ae13798852 Mon Sep 17 00:00:00 2001 From: Ben McCormick Date: Tue, 18 Oct 2016 21:40:00 -0400 Subject: [PATCH 1/2] Add failing test for expecting a function to have been called with no arguments This behavior broke in Jest 16, so adding a test to document it for the future. Also renamed another test that appeared to cover this case but did not. --- .../__snapshots__/spyMatchers-test.js.snap | 48 +++++++++++++++++++ .../src/__tests__/spyMatchers-test.js | 9 +++- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/packages/jest-matchers/src/__tests__/__snapshots__/spyMatchers-test.js.snap b/packages/jest-matchers/src/__tests__/__snapshots__/spyMatchers-test.js.snap index b3503b16c195..6bb29eba42d1 100644 --- a/packages/jest-matchers/src/__tests__/__snapshots__/spyMatchers-test.js.snap +++ b/packages/jest-matchers/src/__tests__/__snapshots__/spyMatchers-test.js.snap @@ -42,6 +42,14 @@ and two more calls." exports[`test lastCalledWith works with jest.fn and no arguments 1`] = ` "expect(jest.fn()).lastCalledWith(expected) +Expected mock function to have been last called with: + ["foo", "bar"] +But it was not called." +`; + +exports[`test lastCalledWith works with jest.fn when not called 1`] = ` +"expect(jest.fn()).lastCalledWith(expected) + Expected mock function to have been last called with: [\"foo\", \"bar\"] But it was not called." @@ -140,6 +148,14 @@ But it was called with: exports[`test toBeCalledWith works with jest.fn and no arguments 1`] = ` "expect(jest.fn()).toBeCalledWith(expected) +Expected mock function to have been called with: + ["foo", "bar"] +But it was not called." +`; + +exports[`test toBeCalledWith works with jest.fn when not called 1`] = ` +"expect(jest.fn()).toBeCalledWith(expected) + Expected mock function to have been called with: [\"foo\", \"bar\"] But it was not called." @@ -238,6 +254,14 @@ But it was called with: exports[`test toHaveBeenCalledWith works with jasmine.createSpy and no arguments 1`] = ` "expect(spy).toHaveBeenCalledWith(expected) +Expected spy to have been called with: + ["foo", "bar"] +But it was not called." +`; + +exports[`test toHaveBeenCalledWith works with jasmine.createSpy when not called 1`] = ` +"expect(spy).toHaveBeenCalledWith(expected) + Expected spy to have been called with: [\"foo\", \"bar\"] But it was not called." @@ -278,6 +302,14 @@ But it was called with: exports[`test toHaveBeenCalledWith works with jest.fn and no arguments 1`] = ` "expect(jest.fn()).toHaveBeenCalledWith(expected) +Expected mock function to have been called with: + ["foo", "bar"] +But it was not called." +`; + +exports[`test toHaveBeenCalledWith works with jest.fn when not called 1`] = ` +"expect(jest.fn()).toHaveBeenCalledWith(expected) + Expected mock function to have been called with: [\"foo\", \"bar\"] But it was not called." @@ -327,6 +359,14 @@ and two more calls." exports[`test toHaveBeenLastCalledWith works with jasmine.createSpy and no arguments 1`] = ` "expect(spy).toHaveBeenLastCalledWith(expected) +Expected spy to have been last called with: + ["foo", "bar"] +But it was not called." +`; + +exports[`test toHaveBeenLastCalledWith works with jasmine.createSpy when not called 1`] = ` +"expect(spy).toHaveBeenLastCalledWith(expected) + Expected spy to have been last called with: [\"foo\", \"bar\"] But it was not called." @@ -368,6 +408,14 @@ and two more calls." exports[`test toHaveBeenLastCalledWith works with jest.fn and no arguments 1`] = ` "expect(jest.fn()).toHaveBeenLastCalledWith(expected) +Expected mock function to have been last called with: + ["foo", "bar"] +But it was not called." +`; + +exports[`test toHaveBeenLastCalledWith works with jest.fn when not called 1`] = ` +"expect(jest.fn()).toHaveBeenLastCalledWith(expected) + Expected mock function to have been last called with: [\"foo\", \"bar\"] But it was not called." diff --git a/packages/jest-matchers/src/__tests__/spyMatchers-test.js b/packages/jest-matchers/src/__tests__/spyMatchers-test.js index 85dc3742dd95..1fb2a59ff87f 100644 --- a/packages/jest-matchers/src/__tests__/spyMatchers-test.js +++ b/packages/jest-matchers/src/__tests__/spyMatchers-test.js @@ -126,7 +126,8 @@ describe('toHaveBeenCalledTimes', () => { const getFunction = () => { return mockName === 'jest.fn' ? jest.fn() : jasmine.createSpy('fn'); }; - test(`${calledWith} works with ${mockName} and no arguments`, () => { + + test(`${calledWith} works with ${mockName} when not called`, () => { const fn = getFunction(); jestExpect(fn).not[calledWith]('foo', 'bar'); @@ -134,6 +135,12 @@ describe('toHaveBeenCalledTimes', () => { .toThrowErrorMatchingSnapshot(); }); + test(`${calledWith} works with ${mockName} and no arguments`, () => { + const fn = getFunction(); + fn(); + jestExpect(fn)[calledWith](); + }); + test(`${calledWith} works with ${mockName} and arguments that don't match`, () => { const fn = getFunction(); fn('foo', 'bar1'); From d773e63ec88a6ee01afcb599f7de052b1849c8be Mon Sep 17 00:00:00 2001 From: Ben McCormick Date: Tue, 18 Oct 2016 22:48:30 -0400 Subject: [PATCH 2/2] Allow testing whether a test has been called with 0 arguments Code was previously assuming at least one argument, but that assumption failed when a function was called with no args. --- .../__snapshots__/spyMatchers-test.js.snap | 48 ------------------- packages/jest-matchers/src/index.js | 4 +- 2 files changed, 2 insertions(+), 50 deletions(-) diff --git a/packages/jest-matchers/src/__tests__/__snapshots__/spyMatchers-test.js.snap b/packages/jest-matchers/src/__tests__/__snapshots__/spyMatchers-test.js.snap index 6bb29eba42d1..3b8773714c01 100644 --- a/packages/jest-matchers/src/__tests__/__snapshots__/spyMatchers-test.js.snap +++ b/packages/jest-matchers/src/__tests__/__snapshots__/spyMatchers-test.js.snap @@ -39,14 +39,6 @@ But it was last called with: and two more calls." `; -exports[`test lastCalledWith works with jest.fn and no arguments 1`] = ` -"expect(jest.fn()).lastCalledWith(expected) - -Expected mock function to have been last called with: - ["foo", "bar"] -But it was not called." -`; - exports[`test lastCalledWith works with jest.fn when not called 1`] = ` "expect(jest.fn()).lastCalledWith(expected) @@ -145,14 +137,6 @@ But it was called with: [\"foo\", \"bar3\"], [\"foo\", \"bar2\"], [\"foo\", \"bar1\"]" `; -exports[`test toBeCalledWith works with jest.fn and no arguments 1`] = ` -"expect(jest.fn()).toBeCalledWith(expected) - -Expected mock function to have been called with: - ["foo", "bar"] -But it was not called." -`; - exports[`test toBeCalledWith works with jest.fn when not called 1`] = ` "expect(jest.fn()).toBeCalledWith(expected) @@ -251,14 +235,6 @@ But it was called with: [\"foo\", \"bar3\"], [\"foo\", \"bar2\"], [\"foo\", \"bar1\"]" `; -exports[`test toHaveBeenCalledWith works with jasmine.createSpy and no arguments 1`] = ` -"expect(spy).toHaveBeenCalledWith(expected) - -Expected spy to have been called with: - ["foo", "bar"] -But it was not called." -`; - exports[`test toHaveBeenCalledWith works with jasmine.createSpy when not called 1`] = ` "expect(spy).toHaveBeenCalledWith(expected) @@ -299,14 +275,6 @@ But it was called with: [\"foo\", \"bar3\"], [\"foo\", \"bar2\"], [\"foo\", \"bar1\"]" `; -exports[`test toHaveBeenCalledWith works with jest.fn and no arguments 1`] = ` -"expect(jest.fn()).toHaveBeenCalledWith(expected) - -Expected mock function to have been called with: - ["foo", "bar"] -But it was not called." -`; - exports[`test toHaveBeenCalledWith works with jest.fn when not called 1`] = ` "expect(jest.fn()).toHaveBeenCalledWith(expected) @@ -356,14 +324,6 @@ But it was last called with: and two more calls." `; -exports[`test toHaveBeenLastCalledWith works with jasmine.createSpy and no arguments 1`] = ` -"expect(spy).toHaveBeenLastCalledWith(expected) - -Expected spy to have been last called with: - ["foo", "bar"] -But it was not called." -`; - exports[`test toHaveBeenLastCalledWith works with jasmine.createSpy when not called 1`] = ` "expect(spy).toHaveBeenLastCalledWith(expected) @@ -405,14 +365,6 @@ But it was last called with: and two more calls." `; -exports[`test toHaveBeenLastCalledWith works with jest.fn and no arguments 1`] = ` -"expect(jest.fn()).toHaveBeenLastCalledWith(expected) - -Expected mock function to have been last called with: - ["foo", "bar"] -But it was not called." -`; - exports[`test toHaveBeenLastCalledWith works with jest.fn when not called 1`] = ` "expect(jest.fn()).toHaveBeenLastCalledWith(expected) diff --git a/packages/jest-matchers/src/index.js b/packages/jest-matchers/src/index.js index 26e30382e740..794683ee0404 100644 --- a/packages/jest-matchers/src/index.js +++ b/packages/jest-matchers/src/index.js @@ -57,7 +57,7 @@ const makeThrowingMatcher = ( isNot: boolean, actual: any, ): ThrowingMatcherFn => { - return function throwingMatcher(expected, ...rest) { + return function throwingMatcher(...args) { let throws = true; const matcherContext: MatcherContext = Object.assign( // When throws is disabled, the matcher will not throw errors during test @@ -74,7 +74,7 @@ const makeThrowingMatcher = ( try { result = matcher.apply( matcherContext, - [actual, expected].concat(rest), + [actual].concat(args), ); } catch (error) { // Remove this and deeper functions from the stack trace frame.