From 771ceb62238d1940349476df969731a880981db9 Mon Sep 17 00:00:00 2001 From: azaleta <24407500@qq.com> Date: Mon, 17 Oct 2022 17:35:09 +0900 Subject: [PATCH 1/7] fix(toHaveReturnedWith): correct return info --- .../src/integrations/chai/jest-expect.ts | 12 +++- test/core/test/mocked.test.ts | 58 +++++++++++++++++++ 2 files changed, 67 insertions(+), 3 deletions(-) diff --git a/packages/vitest/src/integrations/chai/jest-expect.ts b/packages/vitest/src/integrations/chai/jest-expect.ts index 77a5fe0d1caf..55a149fa5711 100644 --- a/packages/vitest/src/integrations/chai/jest-expect.ts +++ b/packages/vitest/src/integrations/chai/jest-expect.ts @@ -589,11 +589,13 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => { const spy = getSpy(this) const spyName = spy.getMockName() const pass = spy.mock.results.some(({ type, value: result }) => type === 'return' && jestEquals(value, result)) + const returns = spy.mock.results.filter(({ type }) => type === 'return').map(({ value: result }) => result).toString() this.assert( pass, - `expected "${spyName}" to be successfully called with #{exp}`, - `expected "${spyName}" to not be successfully called with #{exp}`, - value, + `expected "${spyName}" to be successfully called having return #{exp} at least once`, + `expected "${spyName}" to not be successfully called having return #{exp}`, + `executions have a successful return value: ${value}`, + `executions returns: [${truncateString(returns)}]`, ) }) def(['toHaveLastReturnedWith', 'lastReturnedWith'], function (value: any) { @@ -713,3 +715,7 @@ function toString(value: any) { return 'unknown' } } + +function truncateString(str: string, length = 30) { + return str.length > length ? `${str.substring(0, length)}...` : str +} diff --git a/test/core/test/mocked.test.ts b/test/core/test/mocked.test.ts index a329cdf08ff8..070d8a69ccc4 100644 --- a/test/core/test/mocked.test.ts +++ b/test/core/test/mocked.test.ts @@ -1,3 +1,4 @@ +import type { AssertionError } from 'assert' import { assert, describe, expect, test, vi, vitest } from 'vitest' // @ts-expect-error not typed module import { value as virtualValue } from 'virtual-module' @@ -134,6 +135,63 @@ test('async functions should be mocked', () => { expect(asyncFunc()).resolves.toBe('foo') }) +describe('mocked function which fails on toReturnWith', () => { + test('zero call', () => { + const mock = vi.fn(() => 1) + try { + expect(mock).toReturnWith(2) + } + catch (e) { + const throwObj = e as AssertionError + expect(throwObj.expected).toMatchInlineSnapshot('"executions have a successful return value: 2"') + expect(throwObj.actual).toMatchInlineSnapshot('"executions returns: []"') + } + }) + + test('just one call', () => { + const mock = vi.fn(() => 1) + mock() + try { + expect(mock).toReturnWith(2) + } + catch (e) { + const throwObj = e as AssertionError + expect(throwObj.expected).toMatchInlineSnapshot('"executions have a successful return value: 2"') + expect(throwObj.actual).toMatchInlineSnapshot('"executions returns: [1]"') + } + }) + + test('multi calls', () => { + const mock = vi.fn(() => 1) + mock() + mock() + mock() + try { + expect(mock).toReturnWith(2) + } + catch (e) { + const throwObj = e as AssertionError + expect(throwObj.expected).toMatchInlineSnapshot('"executions have a successful return value: 2"') + expect(throwObj.actual).toMatchInlineSnapshot('"executions returns: [1,1,1]"') + } + }) + + test('all execution returns too long', () => { + const mock = vi.fn(() => 'AAAAAAAAAAAAAAAAAAAA') + mock() + mock() + mock() + try { + expect(mock).toReturnWith(2) + } + catch (e) { + const throwObj = e as AssertionError + expect(throwObj.expected).toMatchInlineSnapshot('"executions have a successful return value: 2"') + expect(throwObj.actual).toMatchInlineSnapshot('"executions returns: [AAAAAAAAAAAAAAAAAAAA,AAAAAAAAA...]"') + } + }) +}) + // This is here because mocking streams previously caused some problems (#1671). test('streams', () => { expect(exportedStream).toBeDefined() From 6572a49eb564670ef0f1246dc8c080bc9967074f Mon Sep 17 00:00:00 2001 From: azaleta <24407500@qq.com> Date: Mon, 17 Oct 2022 21:38:24 +0900 Subject: [PATCH 2/7] chore: use chai untis instead --- .../src/integrations/chai/jest-expect.ts | 14 +++++--------- test/core/test/mocked.test.ts | 18 +++++++++++------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/packages/vitest/src/integrations/chai/jest-expect.ts b/packages/vitest/src/integrations/chai/jest-expect.ts index 55a149fa5711..3944474a6b7b 100644 --- a/packages/vitest/src/integrations/chai/jest-expect.ts +++ b/packages/vitest/src/integrations/chai/jest-expect.ts @@ -589,13 +589,13 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => { const spy = getSpy(this) const spyName = spy.getMockName() const pass = spy.mock.results.some(({ type, value: result }) => type === 'return' && jestEquals(value, result)) - const returns = spy.mock.results.filter(({ type }) => type === 'return').map(({ value: result }) => result).toString() + const returns = utils.inspect(spy.mock.results.filter(({ type }) => type === 'return').map(({ value: result }) => result), true) this.assert( pass, - `expected "${spyName}" to be successfully called having return #{exp} at least once`, - `expected "${spyName}" to not be successfully called having return #{exp}`, - `executions have a successful return value: ${value}`, - `executions returns: [${truncateString(returns)}]`, + `expected "${spyName}" to be successfully called having return ${utils.inspect(value)} at least once`, + `expected "${spyName}" to not be successfully called having return ${utils.inspect(value)}`, + `executions have a successful return value: ${utils.inspect(value)}`, + `executions returns: ${returns}`, ) }) def(['toHaveLastReturnedWith', 'lastReturnedWith'], function (value: any) { @@ -715,7 +715,3 @@ function toString(value: any) { return 'unknown' } } - -function truncateString(str: string, length = 30) { - return str.length > length ? `${str.substring(0, length)}...` : str -} diff --git a/test/core/test/mocked.test.ts b/test/core/test/mocked.test.ts index 070d8a69ccc4..64470b53d0bc 100644 --- a/test/core/test/mocked.test.ts +++ b/test/core/test/mocked.test.ts @@ -156,8 +156,9 @@ describe('mocked function which fails on toReturnWith', () => { } catch (e) { const throwObj = e as AssertionError + expect(throwObj.message).toMatchInlineSnapshot('"expected \\"spy\\" to be successfully called having return 2 at least once"') expect(throwObj.expected).toMatchInlineSnapshot('"executions have a successful return value: 2"') - expect(throwObj.actual).toMatchInlineSnapshot('"executions returns: [1]"') + expect(throwObj.actual).toMatchInlineSnapshot('"executions returns: [ 1 ]"') } }) @@ -171,23 +172,26 @@ describe('mocked function which fails on toReturnWith', () => { } catch (e) { const throwObj = e as AssertionError + expect(throwObj.message).toMatchInlineSnapshot('"expected \\"spy\\" to be successfully called having return 2 at least once"') expect(throwObj.expected).toMatchInlineSnapshot('"executions have a successful return value: 2"') - expect(throwObj.actual).toMatchInlineSnapshot('"executions returns: [1,1,1]"') + expect(throwObj.actual).toMatchInlineSnapshot('"executions returns: [ 1, 1, 1 ]"') } }) - test('all execution returns too long', () => { - const mock = vi.fn(() => 'AAAAAAAAAAAAAAAAAAAA') + test('oject type', () => { + const mock = vi.fn(() => { return { a: '1' } }) mock() mock() mock() + try { - expect(mock).toReturnWith(2) + expect(mock).toReturnWith({ a: '4' }) } catch (e) { const throwObj = e as AssertionError - expect(throwObj.expected).toMatchInlineSnapshot('"executions have a successful return value: 2"') - expect(throwObj.actual).toMatchInlineSnapshot('"executions returns: [AAAAAAAAAAAAAAAAAAAA,AAAAAAAAA...]"') + expect(throwObj.message).toMatchInlineSnapshot('"expected \\"spy\\" to be successfully called having return { a: \'4\' } at least once"') + expect(throwObj.expected).toMatchInlineSnapshot('"executions have a successful return value: { a: \'4\' }"') + expect(throwObj.actual).toMatchInlineSnapshot('"executions returns: [ { a: \'1\' }, { a: \'1\' }, { a: \'1\' } ]"') } }) }) From eb3c2c1d1d1b39a97707ced0f3347c457246d625 Mon Sep 17 00:00:00 2001 From: azaleta <24407500@qq.com> Date: Mon, 17 Oct 2022 22:48:39 +0800 Subject: [PATCH 3/7] chore: follow vitest error format method --- .../src/integrations/chai/jest-expect.ts | 38 +++++++-- test/core/test/mocked.test.ts | 85 ++++++++++++++++--- 2 files changed, 105 insertions(+), 18 deletions(-) diff --git a/packages/vitest/src/integrations/chai/jest-expect.ts b/packages/vitest/src/integrations/chai/jest-expect.ts index 3944474a6b7b..ec4d7ef64ac7 100644 --- a/packages/vitest/src/integrations/chai/jest-expect.ts +++ b/packages/vitest/src/integrations/chai/jest-expect.ts @@ -394,6 +394,20 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => { msg += c.gray(`\n\nNumber of calls: ${c.bold(spy.mock.calls.length)}\n`) return msg } + const formatReturns = (spy: EnhancedSpy, msg: string, actualReturn?: any) => { + msg += c.gray(`\n\nReceived: \n${spy.mock.results.map((callReturn, i) => { + let methodCall = c.bold(` ${ordinalOf(i + 1)} ${spy.getMockName()} call return:\n\n`) + if (actualReturn) + methodCall += unifiedDiff(stringify(callReturn.value), stringify(actualReturn), { showLegend: false }) + else + methodCall += stringify(callReturn).split('\n').map(line => ` ${line}`).join('\n') + + methodCall += '\n' + return methodCall + }).join('\n')}`) + msg += c.gray(`\n\nNumber of calls: ${c.bold(spy.mock.calls.length)}\n`) + return msg + } def(['toHaveBeenCalledTimes', 'toBeCalledTimes'], function (number: number) { const spy = getSpy(this) const spyName = spy.getMockName() @@ -589,14 +603,24 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => { const spy = getSpy(this) const spyName = spy.getMockName() const pass = spy.mock.results.some(({ type, value: result }) => type === 'return' && jestEquals(value, result)) - const returns = utils.inspect(spy.mock.results.filter(({ type }) => type === 'return').map(({ value: result }) => result), true) - this.assert( - pass, - `expected "${spyName}" to be successfully called having return ${utils.inspect(value)} at least once`, - `expected "${spyName}" to not be successfully called having return ${utils.inspect(value)}`, - `executions have a successful return value: ${utils.inspect(value)}`, - `executions returns: ${returns}`, + const isNot = utils.flag(this, 'negate') as boolean + + let msg = utils.getMessage( + this, + [ + pass, + `expected "${spyName}" to be called with arguments: #{exp}`, + `expected "${spyName}" to not be called with arguments: #{exp}`, + value, + ], ) + + if ((pass && isNot) || (!pass && !isNot)) { + msg = formatReturns(spy, msg, value) + const err = new Error(msg) + err.name = 'AssertionError' + throw err + } }) def(['toHaveLastReturnedWith', 'lastReturnedWith'], function (value: any) { const spy = getSpy(this) diff --git a/test/core/test/mocked.test.ts b/test/core/test/mocked.test.ts index 64470b53d0bc..7657e8060a5d 100644 --- a/test/core/test/mocked.test.ts +++ b/test/core/test/mocked.test.ts @@ -143,8 +143,15 @@ describe('mocked function which fails on toReturnWith', () => { } catch (e) { const throwObj = e as AssertionError - expect(throwObj.expected).toMatchInlineSnapshot('"executions have a successful return value: 2"') - expect(throwObj.actual).toMatchInlineSnapshot('"executions returns: []"') + expect(throwObj.message).toMatchInlineSnapshot(` + "expected \\"spy\\" to be called with arguments: 2 + + Received: +  + + Number of calls: 0 + " + `) } }) @@ -156,9 +163,19 @@ describe('mocked function which fails on toReturnWith', () => { } catch (e) { const throwObj = e as AssertionError - expect(throwObj.message).toMatchInlineSnapshot('"expected \\"spy\\" to be successfully called having return 2 at least once"') - expect(throwObj.expected).toMatchInlineSnapshot('"executions have a successful return value: 2"') - expect(throwObj.actual).toMatchInlineSnapshot('"executions returns: [ 1 ]"') + expect(throwObj.message).toMatchInlineSnapshot(` + "expected \\"spy\\" to be called with arguments: 2 + + Received: +  1st spy call return: + +  2 + 1 +  + + Number of calls: 1 + " + `) } }) @@ -172,9 +189,29 @@ describe('mocked function which fails on toReturnWith', () => { } catch (e) { const throwObj = e as AssertionError - expect(throwObj.message).toMatchInlineSnapshot('"expected \\"spy\\" to be successfully called having return 2 at least once"') - expect(throwObj.expected).toMatchInlineSnapshot('"executions have a successful return value: 2"') - expect(throwObj.actual).toMatchInlineSnapshot('"executions returns: [ 1, 1, 1 ]"') + expect(throwObj.message).toMatchInlineSnapshot(` + "expected \\"spy\\" to be called with arguments: 2 + + Received: +  1st spy call return: + +  2 + 1 + +  2nd spy call return: + +  2 + 1 + +  3rd spy call return: + +  2 + 1 +  + + Number of calls: 3 + " + `) } }) @@ -189,9 +226,35 @@ describe('mocked function which fails on toReturnWith', () => { } catch (e) { const throwObj = e as AssertionError - expect(throwObj.message).toMatchInlineSnapshot('"expected \\"spy\\" to be successfully called having return { a: \'4\' } at least once"') - expect(throwObj.expected).toMatchInlineSnapshot('"executions have a successful return value: { a: \'4\' }"') - expect(throwObj.actual).toMatchInlineSnapshot('"executions returns: [ { a: \'1\' }, { a: \'1\' }, { a: \'1\' } ]"') + expect(throwObj.message).toMatchInlineSnapshot(` + "expected \\"spy\\" to be called with arguments: { a: '4' } + + Received: +  1st spy call return: + +  Object { + - \\"a\\": \\"4\\", + + \\"a\\": \\"1\\", + } + +  2nd spy call return: + +  Object { + - \\"a\\": \\"4\\", + + \\"a\\": \\"1\\", + } + +  3rd spy call return: + +  Object { + - \\"a\\": \\"4\\", + + \\"a\\": \\"1\\", + } +  + + Number of calls: 3 + " + `) } }) }) From 233bee273d97d2a5742e1f18d3751973b51ce772 Mon Sep 17 00:00:00 2001 From: azaleta <24407500@qq.com> Date: Mon, 17 Oct 2022 23:45:57 +0800 Subject: [PATCH 4/7] chore: correct message --- packages/vitest/src/integrations/chai/jest-expect.ts | 4 ++-- test/core/test/mocked.test.ts | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/vitest/src/integrations/chai/jest-expect.ts b/packages/vitest/src/integrations/chai/jest-expect.ts index ec4d7ef64ac7..8eb7f0d60b61 100644 --- a/packages/vitest/src/integrations/chai/jest-expect.ts +++ b/packages/vitest/src/integrations/chai/jest-expect.ts @@ -609,8 +609,8 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => { this, [ pass, - `expected "${spyName}" to be called with arguments: #{exp}`, - `expected "${spyName}" to not be called with arguments: #{exp}`, + `expected "${spyName}" to return with: #{exp} at least once`, + `expected "${spyName}" to not return with: #{exp}`, value, ], ) diff --git a/test/core/test/mocked.test.ts b/test/core/test/mocked.test.ts index 7657e8060a5d..debf2f616c0e 100644 --- a/test/core/test/mocked.test.ts +++ b/test/core/test/mocked.test.ts @@ -144,7 +144,7 @@ describe('mocked function which fails on toReturnWith', () => { catch (e) { const throwObj = e as AssertionError expect(throwObj.message).toMatchInlineSnapshot(` - "expected \\"spy\\" to be called with arguments: 2 + "expected \\"spy\\" to return with: 2 at least once Received:  @@ -164,7 +164,7 @@ describe('mocked function which fails on toReturnWith', () => { catch (e) { const throwObj = e as AssertionError expect(throwObj.message).toMatchInlineSnapshot(` - "expected \\"spy\\" to be called with arguments: 2 + "expected \\"spy\\" to return with: 2 at least once Received:  1st spy call return: @@ -190,7 +190,7 @@ describe('mocked function which fails on toReturnWith', () => { catch (e) { const throwObj = e as AssertionError expect(throwObj.message).toMatchInlineSnapshot(` - "expected \\"spy\\" to be called with arguments: 2 + "expected \\"spy\\" to return with: 2 at least once Received:  1st spy call return: @@ -227,7 +227,7 @@ describe('mocked function which fails on toReturnWith', () => { catch (e) { const throwObj = e as AssertionError expect(throwObj.message).toMatchInlineSnapshot(` - "expected \\"spy\\" to be called with arguments: { a: '4' } + "expected \\"spy\\" to return with: { a: '4' } at least once Received:  1st spy call return: From 3dfd388b3709441f7a94bcd0d375b6f963410e5c Mon Sep 17 00:00:00 2001 From: azaleta <24407500@qq.com> Date: Wed, 19 Oct 2022 19:49:42 +0900 Subject: [PATCH 5/7] chore: rewrite test use toThrow --- test/core/test/mocked.test.ts | 104 ++-------------------------------- 1 file changed, 4 insertions(+), 100 deletions(-) diff --git a/test/core/test/mocked.test.ts b/test/core/test/mocked.test.ts index debf2f616c0e..f00acf1475bd 100644 --- a/test/core/test/mocked.test.ts +++ b/test/core/test/mocked.test.ts @@ -1,4 +1,3 @@ -import type { AssertionError } from 'assert' import { assert, describe, expect, test, vi, vitest } from 'vitest' // @ts-expect-error not typed module import { value as virtualValue } from 'virtual-module' @@ -138,45 +137,13 @@ test('async functions should be mocked', () => { describe('mocked function which fails on toReturnWith', () => { test('zero call', () => { const mock = vi.fn(() => 1) - try { - expect(mock).toReturnWith(2) - } - catch (e) { - const throwObj = e as AssertionError - expect(throwObj.message).toMatchInlineSnapshot(` - "expected \\"spy\\" to return with: 2 at least once - - Received: -  - - Number of calls: 0 - " - `) - } + expect(() => expect(mock).toReturnWith(2)).toThrowErrorMatchingInlineSnapshot('"expected \\"spy\\" to be successfully called having return 2 at least once"') }) test('just one call', () => { const mock = vi.fn(() => 1) mock() - try { - expect(mock).toReturnWith(2) - } - catch (e) { - const throwObj = e as AssertionError - expect(throwObj.message).toMatchInlineSnapshot(` - "expected \\"spy\\" to return with: 2 at least once - - Received: -  1st spy call return: - -  2 - 1 -  - - Number of calls: 1 - " - `) - } + expect(() => expect(mock).toReturnWith(2)).toThrowErrorMatchingInlineSnapshot('"expected \\"spy\\" to be successfully called having return 2 at least once"') }) test('multi calls', () => { @@ -184,35 +151,7 @@ describe('mocked function which fails on toReturnWith', () => { mock() mock() mock() - try { - expect(mock).toReturnWith(2) - } - catch (e) { - const throwObj = e as AssertionError - expect(throwObj.message).toMatchInlineSnapshot(` - "expected \\"spy\\" to return with: 2 at least once - - Received: -  1st spy call return: - -  2 - 1 - -  2nd spy call return: - -  2 - 1 - -  3rd spy call return: - -  2 - 1 -  - - Number of calls: 3 - " - `) - } + expect(() => expect(mock).toReturnWith(2)).toThrowErrorMatchingInlineSnapshot('"expected \\"spy\\" to be successfully called having return 2 at least once"') }) test('oject type', () => { @@ -220,42 +159,7 @@ describe('mocked function which fails on toReturnWith', () => { mock() mock() mock() - - try { - expect(mock).toReturnWith({ a: '4' }) - } - catch (e) { - const throwObj = e as AssertionError - expect(throwObj.message).toMatchInlineSnapshot(` - "expected \\"spy\\" to return with: { a: '4' } at least once - - Received: -  1st spy call return: - -  Object { - - \\"a\\": \\"4\\", - + \\"a\\": \\"1\\", - } - -  2nd spy call return: - -  Object { - - \\"a\\": \\"4\\", - + \\"a\\": \\"1\\", - } - -  3rd spy call return: - -  Object { - - \\"a\\": \\"4\\", - + \\"a\\": \\"1\\", - } -  - - Number of calls: 3 - " - `) - } + expect(() => expect(mock).toReturnWith({ a: '4' })).toThrowErrorMatchingInlineSnapshot('"expected \\"spy\\" to be successfully called having return { a: \'4\' } at least once"') }) }) From 0b55863959e3366959ccc86a955976d43a1861a8 Mon Sep 17 00:00:00 2001 From: azaleta <24407500@qq.com> Date: Wed, 19 Oct 2022 20:17:58 +0800 Subject: [PATCH 6/7] chore: update snapshot --- test/core/test/mocked.test.ts | 78 +++++++++++++++++++++++++++++++++-- 1 file changed, 74 insertions(+), 4 deletions(-) diff --git a/test/core/test/mocked.test.ts b/test/core/test/mocked.test.ts index f00acf1475bd..17de1c67d742 100644 --- a/test/core/test/mocked.test.ts +++ b/test/core/test/mocked.test.ts @@ -137,13 +137,33 @@ test('async functions should be mocked', () => { describe('mocked function which fails on toReturnWith', () => { test('zero call', () => { const mock = vi.fn(() => 1) - expect(() => expect(mock).toReturnWith(2)).toThrowErrorMatchingInlineSnapshot('"expected \\"spy\\" to be successfully called having return 2 at least once"') + expect(() => expect(mock).toReturnWith(2)).toThrowErrorMatchingInlineSnapshot(` + "expected \\"spy\\" to return with: 2 at least once + + Received: +  + + Number of calls: 0 + " + `) }) test('just one call', () => { const mock = vi.fn(() => 1) mock() - expect(() => expect(mock).toReturnWith(2)).toThrowErrorMatchingInlineSnapshot('"expected \\"spy\\" to be successfully called having return 2 at least once"') + expect(() => expect(mock).toReturnWith(2)).toThrowErrorMatchingInlineSnapshot(` + "expected \\"spy\\" to return with: 2 at least once + + Received: +  1st spy call return: + +  2 + 1 +  + + Number of calls: 1 + " + `) }) test('multi calls', () => { @@ -151,7 +171,29 @@ describe('mocked function which fails on toReturnWith', () => { mock() mock() mock() - expect(() => expect(mock).toReturnWith(2)).toThrowErrorMatchingInlineSnapshot('"expected \\"spy\\" to be successfully called having return 2 at least once"') + expect(() => expect(mock).toReturnWith(2)).toThrowErrorMatchingInlineSnapshot(` + "expected \\"spy\\" to return with: 2 at least once + + Received: +  1st spy call return: + +  2 + 1 + +  2nd spy call return: + +  2 + 1 + +  3rd spy call return: + +  2 + 1 +  + + Number of calls: 3 + " + `) }) test('oject type', () => { @@ -159,7 +201,35 @@ describe('mocked function which fails on toReturnWith', () => { mock() mock() mock() - expect(() => expect(mock).toReturnWith({ a: '4' })).toThrowErrorMatchingInlineSnapshot('"expected \\"spy\\" to be successfully called having return { a: \'4\' } at least once"') + expect(() => expect(mock).toReturnWith({ a: '4' })).toThrowErrorMatchingInlineSnapshot(` + "expected \\"spy\\" to return with: { a: '4' } at least once + + Received: +  1st spy call return: + +  Object { + - \\"a\\": \\"4\\", + + \\"a\\": \\"1\\", + } + +  2nd spy call return: + +  Object { + - \\"a\\": \\"4\\", + + \\"a\\": \\"1\\", + } + +  3rd spy call return: + +  Object { + - \\"a\\": \\"4\\", + + \\"a\\": \\"1\\", + } +  + + Number of calls: 3 + " + `) }) }) From 0f71584af6277f0019555820005b20f119f63460 Mon Sep 17 00:00:00 2001 From: azaleta <24407500@qq.com> Date: Wed, 19 Oct 2022 21:10:43 +0800 Subject: [PATCH 7/7] chore: use snapshot instead --- .../test/__snapshots__/mocked.test.ts.snap | 79 +++++++++++++++++++ test/core/test/mocked.test.ts | 78 +----------------- 2 files changed, 83 insertions(+), 74 deletions(-) create mode 100644 test/core/test/__snapshots__/mocked.test.ts.snap diff --git a/test/core/test/__snapshots__/mocked.test.ts.snap b/test/core/test/__snapshots__/mocked.test.ts.snap new file mode 100644 index 000000000000..858f49ee0236 --- /dev/null +++ b/test/core/test/__snapshots__/mocked.test.ts.snap @@ -0,0 +1,79 @@ +// Vitest Snapshot v1 + +exports[`mocked function which fails on toReturnWith > just one call 1`] = ` +"expected \\"spy\\" to return with: 2 at least once + +Received: + 1st spy call return: + + 2 + 1 + + +Number of calls: 1 +" +`; + +exports[`mocked function which fails on toReturnWith > multi calls 1`] = ` +"expected \\"spy\\" to return with: 2 at least once + +Received: + 1st spy call return: + + 2 + 1 + + 2nd spy call return: + + 2 + 1 + + 3rd spy call return: + + 2 + 1 + + +Number of calls: 3 +" +`; + +exports[`mocked function which fails on toReturnWith > oject type 1`] = ` +"expected \\"spy\\" to return with: { a: '4' } at least once + +Received: + 1st spy call return: + + Object { + - \\"a\\": \\"4\\", + + \\"a\\": \\"1\\", + } + + 2nd spy call return: + + Object { + - \\"a\\": \\"4\\", + + \\"a\\": \\"1\\", + } + + 3rd spy call return: + + Object { + - \\"a\\": \\"4\\", + + \\"a\\": \\"1\\", + } + + +Number of calls: 3 +" +`; + +exports[`mocked function which fails on toReturnWith > zero call 1`] = ` +"expected \\"spy\\" to return with: 2 at least once + +Received: + + +Number of calls: 0 +" +`; diff --git a/test/core/test/mocked.test.ts b/test/core/test/mocked.test.ts index 17de1c67d742..c92fe7a8db84 100644 --- a/test/core/test/mocked.test.ts +++ b/test/core/test/mocked.test.ts @@ -137,33 +137,13 @@ test('async functions should be mocked', () => { describe('mocked function which fails on toReturnWith', () => { test('zero call', () => { const mock = vi.fn(() => 1) - expect(() => expect(mock).toReturnWith(2)).toThrowErrorMatchingInlineSnapshot(` - "expected \\"spy\\" to return with: 2 at least once - - Received: -  - - Number of calls: 0 - " - `) + expect(() => expect(mock).toReturnWith(2)).toThrowErrorMatchingSnapshot() }) test('just one call', () => { const mock = vi.fn(() => 1) mock() - expect(() => expect(mock).toReturnWith(2)).toThrowErrorMatchingInlineSnapshot(` - "expected \\"spy\\" to return with: 2 at least once - - Received: -  1st spy call return: - -  2 - 1 -  - - Number of calls: 1 - " - `) + expect(() => expect(mock).toReturnWith(2)).toThrowErrorMatchingSnapshot() }) test('multi calls', () => { @@ -171,29 +151,7 @@ describe('mocked function which fails on toReturnWith', () => { mock() mock() mock() - expect(() => expect(mock).toReturnWith(2)).toThrowErrorMatchingInlineSnapshot(` - "expected \\"spy\\" to return with: 2 at least once - - Received: -  1st spy call return: - -  2 - 1 - -  2nd spy call return: - -  2 - 1 - -  3rd spy call return: - -  2 - 1 -  - - Number of calls: 3 - " - `) + expect(() => expect(mock).toReturnWith(2)).toThrowErrorMatchingSnapshot() }) test('oject type', () => { @@ -201,35 +159,7 @@ describe('mocked function which fails on toReturnWith', () => { mock() mock() mock() - expect(() => expect(mock).toReturnWith({ a: '4' })).toThrowErrorMatchingInlineSnapshot(` - "expected \\"spy\\" to return with: { a: '4' } at least once - - Received: -  1st spy call return: - -  Object { - - \\"a\\": \\"4\\", - + \\"a\\": \\"1\\", - } - -  2nd spy call return: - -  Object { - - \\"a\\": \\"4\\", - + \\"a\\": \\"1\\", - } - -  3rd spy call return: - -  Object { - - \\"a\\": \\"4\\", - + \\"a\\": \\"1\\", - } -  - - Number of calls: 3 - " - `) + expect(() => expect(mock).toReturnWith({ a: '4' })).toThrowErrorMatchingSnapshot() }) })