main
js 63 lines 1.7 KB
Raw
1 /*!
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8 'use strict';
9
10 const {
11 patchConsoleMethods,
12 resetAllUnexpectedConsoleCalls,
13 assertConsoleLogsCleared,
14 } = require('internal-test-utils/consoleMock');
15 const spyOn = jest.spyOn;
16
17 // Spying on console methods in production builds can mask errors.
18 // This is why we added an explicit spyOnDev() helper.
19 // It's too easy to accidentally use the more familiar spyOn() helper though,
20 // So we disable it entirely.
21 // Spying on both dev and prod will require using both spyOnDev() and spyOnProd().
22 global.spyOn = function () {
23 throw new Error(
24 'Do not use spyOn(). ' +
25 'It can accidentally hide unexpected errors in production builds. ' +
26 'Use spyOnDev(), spyOnProd(), or spyOnDevAndProd() instead.'
27 );
28 };
29
30 global.spyOnDev = function (...args) {
31 if (__DEV__) {
32 return spyOn(...args);
33 }
34 };
35
36 global.spyOnDevAndProd = spyOn;
37
38 global.spyOnProd = function (...args) {
39 if (!__DEV__) {
40 return spyOn(...args);
41 }
42 };
43
44 // Patch the console to assert that all console error/warn/log calls assert.
45 patchConsoleMethods({includeLog: !!process.env.CI});
46 beforeEach(resetAllUnexpectedConsoleCalls);
47 afterEach(assertConsoleLogsCleared);
48
49 // TODO: enable this check so we don't forget to reset spyOnX mocks.
50 // afterEach(() => {
51 // if (
52 // console[methodName] !== mockMethod &&
53 // !jest.isMockFunction(console[methodName])
54 // ) {
55 // throw new Error(
56 // `Test did not tear down console.${methodName} mock properly.`
57 // );
58 // }
59 // });
60
61 expect.extend({
62 ...require('../matchers/reactTestMatchers'),
63 });