Skip to content

Commit

Permalink
test: refactor mock-warn
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Mar 26, 2024
1 parent 0a94c3c commit c46f41c
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions 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<R = unknown> {
toHaveBeenWarned(): R
toHaveBeenWarnedLast(): R
toHaveBeenWarnedTimes(n: number): R
toHaveBeenWarned: () => R
toHaveBeenWarnedLast: () => R
toHaveBeenWarnedTimes: (n: number) => R
}

declare module 'vitest' {
Expand All @@ -14,12 +15,15 @@ declare module 'vitest' {
}

export function mockWarn() {
let warn: MockInstance<Parameters<(typeof console)['warn']>, void>
const asserted = new Map<string, string | RegExp>()

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) {
Expand All @@ -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 {
Expand All @@ -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++
Expand All @@ -88,9 +92,6 @@ export function mockWarn() {
},
})

let warn: SpyInstance
const asserted = new Map<string, string | RegExp>()

beforeEach(() => {
asserted.clear()
warn = vi.spyOn(console, 'warn')
Expand All @@ -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)
})
})
Expand All @@ -118,12 +119,13 @@ export function mockWarn() {
})
}

declare global {
namespace Vi {
interface JestAssertion<T = any> {
toHaveBeenWarned(): void
toHaveBeenWarnedLast(): void
toHaveBeenWarnedTimes(n: number): void
}
}
interface CustomMatchers<R = unknown> {
toHaveBeenWarned: () => R
toHaveBeenWarnedLast: () => R
toHaveBeenWarnedTimes: (n: number) => R
}

declare module 'vitest' {
interface Assertion<T = any> extends CustomMatchers<T> {}
interface AsymmetricMatchersContaining extends CustomMatchers {}
}

0 comments on commit c46f41c

Please sign in to comment.