]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
test: refactor mock-warn
authorEduardo San Martin Morote <posva13@gmail.com>
Tue, 26 Mar 2024 08:50:11 +0000 (09:50 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Tue, 26 Mar 2024 08:50:21 +0000 (09:50 +0100)
packages/pinia/__tests__/vitest-mock-warn.ts

index 948e5de9f424b4f1cb6893162fa2b6cee6efba1c..7afde1d461b6954289ad82905c3d6cf092a36e67 100644 (file)
@@ -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' {
@@ -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) {
@@ -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<string, string | RegExp>()
-
   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<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 {}
 }