|
| 1 | +declare global { |
| 2 | + namespace jest { |
| 3 | + interface Matchers<R, T> { |
| 4 | + toHaveBeenWarned(): R; |
| 5 | + toHaveBeenWarnedLast(): R; |
| 6 | + toHaveBeenWarnedTimes(n: number): R; |
| 7 | + } |
| 8 | + } |
| 9 | +} |
| 10 | + |
| 11 | +export const mockError = () => mockWarn(true); |
| 12 | + |
| 13 | +export function mockWarn(asError = false) { |
| 14 | + expect.extend({ |
| 15 | + toHaveBeenWarned(received: string) { |
| 16 | + asserted.add(received); |
| 17 | + const passed = warn.mock.calls.some(args => args[0].indexOf(received) > -1); |
| 18 | + if (passed) { |
| 19 | + return { |
| 20 | + pass: true, |
| 21 | + message: () => `expected "${received}" not to have been warned.`, |
| 22 | + }; |
| 23 | + } else { |
| 24 | + const msgs = warn.mock.calls.map(args => args[0]).join('\n - '); |
| 25 | + return { |
| 26 | + pass: false, |
| 27 | + message: () => |
| 28 | + `expected "${received}" to have been warned.\n\nActual messages:\n\n - ${msgs}`, |
| 29 | + }; |
| 30 | + } |
| 31 | + }, |
| 32 | + |
| 33 | + toHaveBeenWarnedLast(received: string) { |
| 34 | + asserted.add(received); |
| 35 | + const passed = warn.mock.calls[warn.mock.calls.length - 1][0].indexOf(received) > -1; |
| 36 | + if (passed) { |
| 37 | + return { |
| 38 | + pass: true, |
| 39 | + message: () => `expected "${received}" not to have been warned last.`, |
| 40 | + }; |
| 41 | + } else { |
| 42 | + const msgs = warn.mock.calls.map(args => args[0]).join('\n - '); |
| 43 | + return { |
| 44 | + pass: false, |
| 45 | + message: () => |
| 46 | + `expected "${received}" to have been warned last.\n\nActual messages:\n\n - ${msgs}`, |
| 47 | + }; |
| 48 | + } |
| 49 | + }, |
| 50 | + |
| 51 | + toHaveBeenWarnedTimes(received: string, n: number) { |
| 52 | + asserted.add(received); |
| 53 | + let found = 0; |
| 54 | + warn.mock.calls.forEach(args => { |
| 55 | + if (args[0].indexOf(received) > -1) { |
| 56 | + found++; |
| 57 | + } |
| 58 | + }); |
| 59 | + |
| 60 | + if (found === n) { |
| 61 | + return { |
| 62 | + pass: true, |
| 63 | + message: () => `expected "${received}" to have been warned ${n} times.`, |
| 64 | + }; |
| 65 | + } else { |
| 66 | + return { |
| 67 | + pass: false, |
| 68 | + message: () => `expected "${received}" to have been warned ${n} times but got ${found}.`, |
| 69 | + }; |
| 70 | + } |
| 71 | + }, |
| 72 | + }); |
| 73 | + |
| 74 | + let warn: jest.SpyInstance; |
| 75 | + const asserted: Set<string> = new Set(); |
| 76 | + |
| 77 | + beforeEach(() => { |
| 78 | + asserted.clear(); |
| 79 | + warn = jest.spyOn(console, asError ? 'error' : 'warn'); |
| 80 | + warn.mockImplementation(() => {}); |
| 81 | + }); |
| 82 | + |
| 83 | + afterEach(() => { |
| 84 | + const assertedArray = Array.from(asserted); |
| 85 | + const nonAssertedWarnings = warn.mock.calls |
| 86 | + .map(args => args[0]) |
| 87 | + .filter(received => { |
| 88 | + return !assertedArray.some(assertedMsg => { |
| 89 | + return received.indexOf(assertedMsg) > -1; |
| 90 | + }); |
| 91 | + }); |
| 92 | + warn.mockRestore(); |
| 93 | + if (nonAssertedWarnings.length) { |
| 94 | + nonAssertedWarnings.forEach(warning => { |
| 95 | + console.warn(warning); |
| 96 | + }); |
| 97 | + throw new Error(`test case threw unexpected warnings.`); |
| 98 | + } |
| 99 | + }); |
| 100 | +} |
0 commit comments