From c46f41cb94f80700d2e7cf0168f4ff94bda3f3b1 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Tue, 26 Mar 2024 09:50:11 +0100 Subject: [PATCH] test: refactor mock-warn --- packages/pinia/__tests__/vitest-mock-warn.ts | 42 ++++++++++---------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/packages/pinia/__tests__/vitest-mock-warn.ts b/packages/pinia/__tests__/vitest-mock-warn.ts index 948e5de9f4..7afde1d461 100644 --- a/packages/pinia/__tests__/vitest-mock-warn.ts +++ b/packages/pinia/__tests__/vitest-mock-warn.ts @@ -1,11 +1,12 @@ // https://github.com/posva/jest-mock-warn/blob/master/src/index.js -import { afterEach, beforeEach, expect, SpyInstance, vi } from 'vitest' +import type { MockInstance } from 'vitest' +import { afterEach, beforeEach, expect, vi } from 'vitest' interface CustomMatchers { - toHaveBeenWarned(): R - toHaveBeenWarnedLast(): R - toHaveBeenWarnedTimes(n: number): R + toHaveBeenWarned: () => R + toHaveBeenWarnedLast: () => R + toHaveBeenWarnedTimes: (n: number) => R } declare module 'vitest' { @@ -14,12 +15,15 @@ declare module 'vitest' { } export function mockWarn() { + let warn: MockInstance, void> + const asserted = new Map() + expect.extend({ toHaveBeenWarned(received: string | RegExp) { asserted.set(received.toString(), received) const passed = warn.mock.calls.some((args) => typeof received === 'string' - ? args[0].indexOf(received) > -1 + ? args[0].includes(received) : received.test(args[0]) ) if (passed) { @@ -42,7 +46,7 @@ export function mockWarn() { const lastCall = warn.mock.calls[warn.mock.calls.length - 1][0] const passed = typeof received === 'string' - ? lastCall.indexOf(received) > -1 + ? lastCall.includes(received) : received.test(lastCall) if (passed) { return { @@ -65,7 +69,7 @@ export function mockWarn() { warn.mock.calls.forEach((args) => { const isFound = typeof received === 'string' - ? args[0].indexOf(received) > -1 + ? args[0].includes(received) : received.test(args[0]) if (isFound) { found++ @@ -88,9 +92,6 @@ export function mockWarn() { }, }) - let warn: SpyInstance - const asserted = new Map() - beforeEach(() => { asserted.clear() warn = vi.spyOn(console, 'warn') @@ -102,9 +103,9 @@ export function mockWarn() { const nonAssertedWarnings = warn.mock.calls .map((args) => args[0]) .filter((received) => { - return !assertedArray.some(([key, assertedMsg]) => { + return !assertedArray.some(([_key, assertedMsg]) => { return typeof assertedMsg === 'string' - ? received.indexOf(assertedMsg) > -1 + ? received.includes(assertedMsg) : assertedMsg.test(received) }) }) @@ -118,12 +119,13 @@ export function mockWarn() { }) } -declare global { - namespace Vi { - interface JestAssertion { - toHaveBeenWarned(): void - toHaveBeenWarnedLast(): void - toHaveBeenWarnedTimes(n: number): void - } - } +interface CustomMatchers { + toHaveBeenWarned: () => R + toHaveBeenWarnedLast: () => R + toHaveBeenWarnedTimes: (n: number) => R +} + +declare module 'vitest' { + interface Assertion extends CustomMatchers {} + interface AsymmetricMatchersContaining extends CustomMatchers {} }