]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
refactor: use jest-mock-warn
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 5 Feb 2020 16:42:11 +0000 (17:42 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 5 Feb 2020 16:42:11 +0000 (17:42 +0100)
__tests__/mockWarn.ts [deleted file]
__tests__/parseQuery.spec.ts
__tests__/stringifyQuery.spec.ts
package.json
yarn.lock

diff --git a/__tests__/mockWarn.ts b/__tests__/mockWarn.ts
deleted file mode 100644 (file)
index e080d95..0000000
+++ /dev/null
@@ -1,103 +0,0 @@
-declare global {
-  namespace jest {
-    interface Matchers<R, T> {
-      toHaveBeenWarned(): R
-      toHaveBeenWarnedLast(): R
-      toHaveBeenWarnedTimes(n: number): R
-    }
-  }
-}
-
-export function mockWarn() {
-  expect.extend({
-    toHaveBeenWarned(received: string) {
-      asserted.add(received)
-      const passed = warn.mock.calls.some(
-        args => args[0].indexOf(received) > -1
-      )
-      if (passed) {
-        return {
-          pass: true,
-          message: () => `expected "${received}" not to have been warned.`,
-        }
-      } else {
-        const msgs = warn.mock.calls.map(args => args[0]).join('\n - ')
-        return {
-          pass: false,
-          message: () =>
-            `expected "${received}" to have been warned.\n\nActual messages:\n\n - ${msgs}`,
-        }
-      }
-    },
-
-    toHaveBeenWarnedLast(received: string) {
-      asserted.add(received)
-      const passed =
-        warn.mock.calls[warn.mock.calls.length - 1][0].indexOf(received) > -1
-      if (passed) {
-        return {
-          pass: true,
-          message: () => `expected "${received}" not to have been warned last.`,
-        }
-      } else {
-        const msgs = warn.mock.calls.map(args => args[0]).join('\n - ')
-        return {
-          pass: false,
-          message: () =>
-            `expected "${received}" to have been warned last.\n\nActual messages:\n\n - ${msgs}`,
-        }
-      }
-    },
-
-    toHaveBeenWarnedTimes(received: string, n: number) {
-      asserted.add(received)
-      let found = 0
-      warn.mock.calls.forEach(args => {
-        if (args[0].indexOf(received) > -1) {
-          found++
-        }
-      })
-
-      if (found === n) {
-        return {
-          pass: true,
-          message: () =>
-            `expected "${received}" to have been warned ${n} times.`,
-        }
-      } else {
-        return {
-          pass: false,
-          message: () =>
-            `expected "${received}" to have been warned ${n} times but got ${found}.`,
-        }
-      }
-    },
-  })
-
-  let warn: jest.SpyInstance
-  const asserted: Set<string> = new Set()
-
-  beforeEach(() => {
-    asserted.clear()
-    warn = jest.spyOn(console, 'warn')
-    warn.mockImplementation(() => {})
-  })
-
-  afterEach(() => {
-    const assertedArray = Array.from(asserted)
-    const nonAssertedWarnings = warn.mock.calls
-      .map(args => args[0])
-      .filter(received => {
-        return !assertedArray.some(assertedMsg => {
-          return received.indexOf(assertedMsg) > -1
-        })
-      })
-    warn.mockRestore()
-    if (nonAssertedWarnings.length) {
-      nonAssertedWarnings.forEach(warning => {
-        console.warn(warning)
-      })
-      throw new Error(`test case threw unexpected warnings.`)
-    }
-  })
-}
index ee017eeb0cbcd4108ad4691138b062ff676ed62a..5fc772c405de39ab32580e20cda62370ca18832b 100644 (file)
@@ -1,5 +1,5 @@
 import { parseQuery } from '../src/utils/query'
-import { mockWarn } from './mockWarn'
+import { mockWarn } from 'jest-mock-warn'
 
 describe('parseQuery', () => {
   mockWarn()
index 61b436becb254d7db8f16a6a06aa7b5967b47303..647db4dd6475ec354ce4d4f8d57e494560f62a20 100644 (file)
@@ -1,5 +1,5 @@
 import { stringifyQuery } from '../src/utils/query'
-import { mockWarn } from './mockWarn'
+import { mockWarn } from 'jest-mock-warn'
 
 describe('stringifyQuery', () => {
   mockWarn()
index 309345c1d6d841c19f82c4f04e6381b9c6c04710..0a61680b563b172279803b452d8faa9f618f218c 100644 (file)
@@ -44,6 +44,7 @@
     "faked-promise": "^2.2.2",
     "html-webpack-plugin": "^3.2.0",
     "jest": "^25.1.0",
+    "jest-mock-warn": "^1.0.1",
     "nightwatch": "^1.3.4",
     "nightwatch-helpers": "^1.2.0",
     "prettier": "^1.19.1",
index 33266b854020541acd2a1dbd97af8868001ff020..a6595d5deec3a363f6cf46e5dc0d96fb187370b8 100644 (file)
--- a/yarn.lock
+++ b/yarn.lock
@@ -4251,6 +4251,11 @@ jest-message-util@^25.1.0:
     slash "^3.0.0"
     stack-utils "^1.0.1"
 
+jest-mock-warn@^1.0.1:
+  version "1.0.1"
+  resolved "https://registry.yarnpkg.com/jest-mock-warn/-/jest-mock-warn-1.0.1.tgz#f72337c2ae4f94e4b9515690b670d3005c605018"
+  integrity sha512-j5Fyx1B2MoBieO40wSW4cdz8xt1OqX9lMNdiM4WPXUKB9dz2JqBzSFfqvIyPXrAjfkd60WCpfIL5F7P/Z2R7DA==
+
 jest-mock@^25.1.0:
   version "25.1.0"
   resolved "https://registry.yarnpkg.com/jest-mock/-/jest-mock-25.1.0.tgz#411d549e1b326b7350b2e97303a64715c28615fd"