|
| 1 | +// Fork of https://github.com/facebook/react/blob/513417d6951fa3ff5729302b7990b84604b11afa/scripts/jest/setupTests.js#L71-L161 |
| 2 | +/** |
| 3 | +MIT License |
| 4 | +
|
| 5 | +Copyright (c) Facebook, Inc. and its affiliates. |
| 6 | +
|
| 7 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +of this software and associated documentation files (the "Software"), to deal |
| 9 | +in the Software without restriction, including without limitation the rights |
| 10 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +copies of the Software, and to permit persons to whom the Software is |
| 12 | +furnished to do so, subject to the following conditions: |
| 13 | +
|
| 14 | +The above copyright notice and this permission notice shall be included in all |
| 15 | +copies or substantial portions of the Software. |
| 16 | +
|
| 17 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +SOFTWARE. |
| 24 | + */ |
| 25 | +/* eslint-disable prefer-template */ |
| 26 | +/* eslint-disable func-names */ |
| 27 | +const util = require('util') |
| 28 | +const chalk = require('chalk') |
| 29 | +const shouldIgnoreConsoleError = require('./shouldIgnoreConsoleError') |
| 30 | + |
| 31 | +const patchConsoleMethod = (methodName, unexpectedConsoleCallStacks) => { |
| 32 | + const newMethod = function (format, ...args) { |
| 33 | + // Ignore uncaught errors reported by jsdom |
| 34 | + // and React addendums because they're too noisy. |
| 35 | + if (methodName === 'error' && shouldIgnoreConsoleError(format, args)) { |
| 36 | + return |
| 37 | + } |
| 38 | + |
| 39 | + // Capture the call stack now so we can warn about it later. |
| 40 | + // The call stack has helpful information for the test author. |
| 41 | + // Don't throw yet though b'c it might be accidentally caught and suppressed. |
| 42 | + const stack = new Error().stack |
| 43 | + unexpectedConsoleCallStacks.push([ |
| 44 | + stack.substr(stack.indexOf('\n') + 1), |
| 45 | + util.format(format, ...args), |
| 46 | + ]) |
| 47 | + } |
| 48 | + |
| 49 | + console[methodName] = newMethod |
| 50 | + |
| 51 | + return newMethod |
| 52 | +} |
| 53 | + |
| 54 | +const isSpy = spy => |
| 55 | + (spy.calls && typeof spy.calls.count === 'function') || |
| 56 | + spy._isMockFunction === true |
| 57 | + |
| 58 | +const flushUnexpectedConsoleCalls = ( |
| 59 | + mockMethod, |
| 60 | + methodName, |
| 61 | + expectedMatcher, |
| 62 | + unexpectedConsoleCallStacks, |
| 63 | +) => { |
| 64 | + if (console[methodName] !== mockMethod && !isSpy(console[methodName])) { |
| 65 | + throw new Error( |
| 66 | + `Test did not tear down console.${methodName} mock properly.`, |
| 67 | + ) |
| 68 | + } |
| 69 | + if (unexpectedConsoleCallStacks.length > 0) { |
| 70 | + const messages = unexpectedConsoleCallStacks.map( |
| 71 | + ([stack, message]) => |
| 72 | + `${chalk.red(message)}\n` + |
| 73 | + `${stack |
| 74 | + .split('\n') |
| 75 | + .map(line => chalk.gray(line)) |
| 76 | + .join('\n')}`, |
| 77 | + ) |
| 78 | + |
| 79 | + const message = |
| 80 | + `Expected test not to call ${chalk.bold( |
| 81 | + `console.${methodName}()`, |
| 82 | + )}.\n\n` + |
| 83 | + 'If the warning is expected, test for it explicitly by:\n' + |
| 84 | + `1. Using the ${chalk.bold('.' + expectedMatcher + '()')} ` + |
| 85 | + `matcher, or...\n` + |
| 86 | + `2. Mock it out using ${chalk.bold( |
| 87 | + 'spyOnDev', |
| 88 | + )}(console, '${methodName}') or ${chalk.bold( |
| 89 | + 'spyOnProd', |
| 90 | + )}(console, '${methodName}'), and test that the warning occurs.` |
| 91 | + |
| 92 | + throw new Error(`${message}\n\n${messages.join('\n\n')}`) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +const unexpectedErrorCallStacks = [] |
| 97 | +const unexpectedWarnCallStacks = [] |
| 98 | + |
| 99 | +const errorMethod = patchConsoleMethod('error', unexpectedErrorCallStacks) |
| 100 | +const warnMethod = patchConsoleMethod('warn', unexpectedWarnCallStacks) |
| 101 | + |
| 102 | +const flushAllUnexpectedConsoleCalls = () => { |
| 103 | + flushUnexpectedConsoleCalls( |
| 104 | + errorMethod, |
| 105 | + 'error', |
| 106 | + 'toErrorDev', |
| 107 | + unexpectedErrorCallStacks, |
| 108 | + ) |
| 109 | + flushUnexpectedConsoleCalls( |
| 110 | + warnMethod, |
| 111 | + 'warn', |
| 112 | + 'toWarnDev', |
| 113 | + unexpectedWarnCallStacks, |
| 114 | + ) |
| 115 | + unexpectedErrorCallStacks.length = 0 |
| 116 | + unexpectedWarnCallStacks.length = 0 |
| 117 | +} |
| 118 | + |
| 119 | +const resetAllUnexpectedConsoleCalls = () => { |
| 120 | + unexpectedErrorCallStacks.length = 0 |
| 121 | + unexpectedWarnCallStacks.length = 0 |
| 122 | +} |
| 123 | + |
| 124 | +expect.extend({ |
| 125 | + ...require('./toWarnDev'), |
| 126 | +}) |
| 127 | + |
| 128 | +beforeEach(resetAllUnexpectedConsoleCalls) |
| 129 | +afterEach(flushAllUnexpectedConsoleCalls) |
0 commit comments