From 709c1de5071420a6a42c6483dca1ebd4da9390b5 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Wed, 5 Feb 2020 17:42:11 +0100 Subject: [PATCH] refactor: use jest-mock-warn --- __tests__/mockWarn.ts | 103 ------------------------------- __tests__/parseQuery.spec.ts | 2 +- __tests__/stringifyQuery.spec.ts | 2 +- package.json | 1 + yarn.lock | 5 ++ 5 files changed, 8 insertions(+), 105 deletions(-) delete mode 100644 __tests__/mockWarn.ts diff --git a/__tests__/mockWarn.ts b/__tests__/mockWarn.ts deleted file mode 100644 index e080d955..00000000 --- a/__tests__/mockWarn.ts +++ /dev/null @@ -1,103 +0,0 @@ -declare global { - namespace jest { - interface Matchers { - 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 = 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.`) - } - }) -} diff --git a/__tests__/parseQuery.spec.ts b/__tests__/parseQuery.spec.ts index ee017eeb..5fc772c4 100644 --- a/__tests__/parseQuery.spec.ts +++ b/__tests__/parseQuery.spec.ts @@ -1,5 +1,5 @@ import { parseQuery } from '../src/utils/query' -import { mockWarn } from './mockWarn' +import { mockWarn } from 'jest-mock-warn' describe('parseQuery', () => { mockWarn() diff --git a/__tests__/stringifyQuery.spec.ts b/__tests__/stringifyQuery.spec.ts index 61b436be..647db4dd 100644 --- a/__tests__/stringifyQuery.spec.ts +++ b/__tests__/stringifyQuery.spec.ts @@ -1,5 +1,5 @@ import { stringifyQuery } from '../src/utils/query' -import { mockWarn } from './mockWarn' +import { mockWarn } from 'jest-mock-warn' describe('stringifyQuery', () => { mockWarn() diff --git a/package.json b/package.json index 309345c1..0a61680b 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/yarn.lock b/yarn.lock index 33266b85..a6595d5d 100644 --- 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" -- 2.47.3