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