Comparing version 2.4.0 to 2.5.0
{ | ||
"name": "jest-when", | ||
"version": "2.4.0", | ||
"version": "2.5.0", | ||
"description": "An extension lib for jest", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -0,1 +1,2 @@ | ||
const assert = require('assert') | ||
const utils = require('expect/build/jasmine_utils') | ||
@@ -6,2 +7,4 @@ const logger = require('./log')('when') | ||
const getCallLine = () => (new Error()).stack.split('\n')[4] | ||
const checkArgumentMatchers = (expectCall, args) => (match, matcher, i) => { | ||
@@ -47,3 +50,3 @@ logger.debug(`matcher check, match: ${match}, index: ${i}`) | ||
.filter((callMock) => once || callMock.once || !utils.equals(callMock.matchers, matchers)) | ||
.concat({ matchers, returnValue, expectCall, once, called: false, id: this.nextCallMockId }) | ||
.concat({ matchers, returnValue, expectCall, once, called: false, id: this.nextCallMockId, callLine: getCallLine() }) | ||
.sort((a, b) => { | ||
@@ -62,15 +65,10 @@ // Reduce their id by 1000 if they are a once mock, to sort them at the front | ||
for (let i = 0; i < this.callMocks.length; i++) { | ||
const { matchers, returnValue, expectCall } = this.callMocks[i] | ||
const match = matchers.reduce(checkArgumentMatchers(expectCall, args), true) | ||
const { matchers, returnValue, expectCall, once, called } = this.callMocks[i] | ||
if (match) { | ||
// Do not let a once mock match more than once | ||
if (once && called) continue | ||
const isMatch = matchers.reduce(checkArgumentMatchers(expectCall, args), true) | ||
if (isMatch) { | ||
this.callMocks[i].called = true | ||
let removedOneItem = false | ||
this.callMocks = this.callMocks.filter(mock => { | ||
if (mock.once && utils.equals(mock.matchers, matchers) && !removedOneItem) { | ||
removedOneItem = true | ||
return false | ||
} | ||
return true | ||
}) | ||
return typeof returnValue === 'function' ? returnValue(...args) : returnValue | ||
@@ -125,15 +123,19 @@ } | ||
const verifyAllWhenMocksCalled = () => { | ||
registry.forEach(fn => { | ||
const uncalledMocks = fn.__whenMock__.callMocks | ||
.filter(mock => !mock.called) | ||
// Map the mock obj to only the fields worth showing in the error diff | ||
.map(({ called, expectCall, matchers, once, returnValue }) => | ||
({ called, expectCall, matchers, once, returnValue })) | ||
const [allMocks, calledMocks, uncalledMocks] = Array.from(registry).reduce((acc, fn) => { | ||
const mocks = fn.__whenMock__.callMocks | ||
const [calledMocks, uncalledMocks] = mocks.reduce((memo, mock) => { | ||
memo[mock.called ? 0 : 1].push(mock) | ||
return memo | ||
}, [[], []]) | ||
return [[...acc[0], ...mocks], [...acc[1], ...calledMocks], [...acc[2], ...uncalledMocks]] | ||
}, [[], [], []]) | ||
const expected = uncalledMocks.map(m => ({ ...m, called: true })) | ||
const actual = uncalledMocks.map(m => ({ ...m, matchers: [] })) | ||
const callLines = uncalledMocks | ||
.filter(m => Boolean(m.callLine)) | ||
.map(m => `\n ${String(m.callLine).trim()}`) | ||
.join('') | ||
const msg = `Failed verifyAllWhenMocksCalled: ${uncalledMocks.length} not called` | ||
expect([msg, actual]).toEqual([msg, expected]) | ||
}) | ||
const msg = `Failed verifyAllWhenMocksCalled: ${uncalledMocks.length} not called at:${callLines}\n\n\n...rest of the stack...` | ||
assert.equal(`called mocks: ${calledMocks.length}`, `called mocks: ${allMocks.length}`, msg) | ||
} | ||
@@ -140,0 +142,0 @@ |
@@ -94,4 +94,30 @@ const { stringContaining } = expect | ||
expect(verifyAllWhenMocksCalled).toThrow(/Failed verifyAllWhenMocksCalled: 2 not called/) | ||
let caughtErr | ||
try { | ||
verifyAllWhenMocksCalled() | ||
} catch (e) { | ||
caughtErr = e | ||
} | ||
expect(caughtErr.expected).toEqual('called mocks: 4') | ||
expect(caughtErr.actual).toEqual('called mocks: 2') | ||
expect(caughtErr.message).toMatch(/Failed verifyAllWhenMocksCalled: 2 not called/) | ||
}) | ||
it('fails verification check if all mocks were not called with line numbers', () => { | ||
const fn1 = jest.fn() | ||
const fn2 = jest.fn() | ||
when(fn1).expectCalledWith(expect.anything()).mockReturnValue('z') | ||
when(fn2).expectCalledWith(expect.anything()).mockReturnValueOnce('x') | ||
when(fn2).expectCalledWith(expect.anything()).mockReturnValueOnce('y') | ||
when(fn2).expectCalledWith(expect.anything()).mockReturnValue('z') | ||
fn1(1) | ||
fn2(1) | ||
// Should be two call lines printed, hence the {2} at the end of the regex | ||
expect(verifyAllWhenMocksCalled).toThrow(/(src\/when\.test\.js:\d{3}(.|\s)*){2}/) | ||
}) | ||
}) | ||
@@ -98,0 +124,0 @@ |
208340
552