diff --git a/.eslintrc.js b/.eslintrc.js index b5205817a4c92..13746fb3c672d 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -434,7 +434,6 @@ module.exports = { es6: true, node: true, jest: true, - jasmine: true, }, globals: { diff --git a/package.json b/package.json index b10894522b309..a5df2c31b4eb7 100644 --- a/package.json +++ b/package.json @@ -69,12 +69,10 @@ "gzip-size": "^5.1.1", "hermes-eslint": "^0.9.0", "hermes-parser": "^0.9.0", - "jasmine-check": "^1.0.0-rc.0", "jest": "^29.4.1", "jest-cli": "^29.4.1", "jest-diff": "^29.4.1", "jest-environment-jsdom": "^29.4.1", - "jest-jasmine2": "^29.4.1", "jest-snapshot-serializer-raw": "^1.1.0", "minimatch": "^3.0.4", "minimist": "^1.2.3", diff --git a/packages/react-client/src/__tests__/ReactFlight-test.js b/packages/react-client/src/__tests__/ReactFlight-test.js index 02d95592a12a5..d7cec67f5bfa9 100644 --- a/packages/react-client/src/__tests__/ReactFlight-test.js +++ b/packages/react-client/src/__tests__/ReactFlight-test.js @@ -91,6 +91,10 @@ describe('ReactFlight', () => { }; }); + afterEach(() => { + jest.restoreAllMocks(); + }); + function clientReference(value) { return Object.defineProperties( function () { @@ -240,7 +244,7 @@ describe('ReactFlight', () => { ReactNoop.render(rootModel); }); expect(ReactNoop).toMatchRenderedOutput('Loading...'); - spyOnDevAndProd(console, 'error'); + spyOnDevAndProd(console, 'error').mockImplementation(() => {}); await load(); expect(console.error).toHaveBeenCalledTimes(1); }); @@ -322,7 +326,7 @@ describe('ReactFlight', () => { ReactNoop.render(rootModel); }); expect(ReactNoop).toMatchRenderedOutput('Loading...'); - spyOnDevAndProd(console, 'error'); + spyOnDevAndProd(console, 'error').mockImplementation(() => {}); await load(); expect(console.error).toHaveBeenCalledTimes(1); }); diff --git a/packages/react-devtools-extensions/flow-typed/jest.js b/packages/react-devtools-extensions/flow-typed/jest.js index 3cad1447b12cb..5ed91242791a2 100644 --- a/packages/react-devtools-extensions/flow-typed/jest.js +++ b/packages/react-devtools-extensions/flow-typed/jest.js @@ -1172,28 +1172,5 @@ declare var expect: { }, }; -// TODO handle return type -// https://jasmine.github.io/2.4/introduction.html#section-Spies -declare function spyOn(value: mixed, method: string): Object; - /** Holds all functions related to manipulating test runner */ declare var jest: JestObjectType; - -/** - * The global Jasmine object, this is generally not exposed as the public API, - * using features inside here could break in later versions of Jest. - */ -declare var jasmine: { - DEFAULT_TIMEOUT_INTERVAL: number, - any(value: mixed): JestAsymmetricEqualityType, - anything(): any, - arrayContaining(value: Array): Array, - clock(): JestClockType, - createSpy(name: string): JestSpyType, - createSpyObj( - baseName: string, - methodNames: Array - ): {[methodName: string]: JestSpyType}, - objectContaining(value: Object): Object, - stringMatching(value: string): string, -}; diff --git a/packages/react-devtools-shared/src/__tests__/TimelineProfiler-test.js b/packages/react-devtools-shared/src/__tests__/TimelineProfiler-test.js index 27188c5f41903..72f112cf5a5af 100644 --- a/packages/react-devtools-shared/src/__tests__/TimelineProfiler-test.js +++ b/packages/react-devtools-shared/src/__tests__/TimelineProfiler-test.js @@ -47,6 +47,10 @@ describe('Timeline profiler', () => { store = global.store; }); + afterEach(() => { + jest.restoreAllMocks(); + }); + describe('User Timing API', () => { let clearedMarks; let featureDetectionMarkName = null; @@ -517,7 +521,7 @@ describe('Timeline profiler', () => { clearPendingMarks(); let errorMessage; - spyOn(console, 'error').and.callFake(message => { + jest.spyOn(console, 'error').mockImplementation(message => { errorMessage = message; }); @@ -571,7 +575,7 @@ describe('Timeline profiler', () => { clearPendingMarks(); let errorMessage; - spyOn(console, 'error').and.callFake(message => { + jest.spyOn(console, 'error').mockImplementation(message => { errorMessage = message; }); @@ -740,7 +744,7 @@ describe('Timeline profiler', () => { }); it('should mark sync render that throws', async () => { - spyOn(console, 'error'); + jest.spyOn(console, 'error').mockImplementation(() => {}); class ErrorBoundary extends React.Component { state = {error: null}; @@ -802,7 +806,7 @@ describe('Timeline profiler', () => { }); it('should mark concurrent render that throws', async () => { - spyOn(console, 'error'); + jest.spyOn(console, 'error').mockImplementation(() => {}); class ErrorBoundary extends React.Component { state = {error: null}; @@ -1697,7 +1701,7 @@ describe('Timeline profiler', () => { renderRootHelper(); let errorMessage; - spyOn(console, 'error').and.callFake(message => { + jest.spyOn(console, 'error').mockImplementation(message => { errorMessage = message; }); @@ -1766,7 +1770,7 @@ describe('Timeline profiler', () => { renderRootHelper(); let errorMessage; - spyOn(console, 'error').and.callFake(message => { + jest.spyOn(console, 'error').mockImplementation(message => { errorMessage = message; }); @@ -1993,7 +1997,7 @@ describe('Timeline profiler', () => { }); it('should mark sync render that throws', async () => { - spyOn(console, 'error'); + jest.spyOn(console, 'error').mockImplementation(() => {}); class ErrorBoundary extends React.Component { state = {error: null}; @@ -2088,7 +2092,7 @@ describe('Timeline profiler', () => { }); it('should mark concurrent render that throws', async () => { - spyOn(console, 'error'); + jest.spyOn(console, 'error').mockImplementation(() => {}); class ErrorBoundary extends React.Component { state = {error: null}; diff --git a/packages/react-devtools-shared/src/__tests__/bridge-test.js b/packages/react-devtools-shared/src/__tests__/bridge-test.js index 67c1615c2b43c..d38c0fe66a8b9 100644 --- a/packages/react-devtools-shared/src/__tests__/bridge-test.js +++ b/packages/react-devtools-shared/src/__tests__/bridge-test.js @@ -38,7 +38,7 @@ describe('Bridge', () => { expect(wall.send).toHaveBeenCalledWith('shutdown'); // Verify that the Bridge doesn't send messages after shutdown. - spyOn(console, 'warn'); + jest.spyOn(console, 'warn').mockImplementation(() => {}); wall.send.mockClear(); bridge.send('should not send'); jest.runAllTimers(); diff --git a/packages/react-devtools-shared/src/__tests__/inspectedElement-test.js b/packages/react-devtools-shared/src/__tests__/inspectedElement-test.js index 3a5a82bf287bc..6e86f42a74a12 100644 --- a/packages/react-devtools-shared/src/__tests__/inspectedElement-test.js +++ b/packages/react-devtools-shared/src/__tests__/inspectedElement-test.js @@ -2121,7 +2121,7 @@ describe('InspectedElement', () => { }); it('should gracefully surface backend errors on the frontend rather than timing out', async () => { - spyOn(console, 'error'); + jest.spyOn(console, 'error').mockImplementation(() => {}); let shouldThrow = false; @@ -2738,7 +2738,7 @@ describe('InspectedElement', () => { it('inspecting nested renderers should not throw', async () => { // Ignoring react art warnings - spyOn(console, 'error'); + jest.spyOn(console, 'error').mockImplementation(() => {}); const ReactArt = require('react-art'); const ArtSVGMode = require('art/modes/svg'); const ARTCurrentMode = require('art/modes/current'); diff --git a/packages/react-devtools-shared/src/__tests__/legacy/inspectElement-test.js b/packages/react-devtools-shared/src/__tests__/legacy/inspectElement-test.js index c6f748a30ed85..ba8064bc3ca3a 100644 --- a/packages/react-devtools-shared/src/__tests__/legacy/inspectElement-test.js +++ b/packages/react-devtools-shared/src/__tests__/legacy/inspectElement-test.js @@ -760,7 +760,7 @@ describe('InspectedElementContext', () => { const rendererID = ((store.getRendererIDForElement(id): any): number); const logSpy = jest.fn(); - spyOn(console, 'log').and.callFake(logSpy); + jest.spyOn(console, 'log').mockImplementation(logSpy); // Should store the whole value (not just the hydrated parts) backendAPI.storeAsGlobal({ diff --git a/packages/react-devtools-shared/src/__tests__/preprocessData-test.js b/packages/react-devtools-shared/src/__tests__/preprocessData-test.js index ff4964927c69d..1464912034efa 100644 --- a/packages/react-devtools-shared/src/__tests__/preprocessData-test.js +++ b/packages/react-devtools-shared/src/__tests__/preprocessData-test.js @@ -1135,7 +1135,7 @@ describe('Timeline profiler', () => { ); const invalidUserTimingData = createUserTimingData(invalidMarks); - const error = spyOn(console, 'error'); + const error = jest.spyOn(console, 'error').mockImplementation(() => {}); preprocessData([ ...createBoilerplateEntries(), ...invalidUserTimingData, @@ -1153,7 +1153,7 @@ describe('Timeline profiler', () => { ); const invalidUserTimingData = createUserTimingData(invalidMarks); - const error = spyOn(console, 'error'); + const error = jest.spyOn(console, 'error').mockImplementation(() => {}); preprocessData([ ...createBoilerplateEntries(), ...invalidUserTimingData, @@ -1748,7 +1748,7 @@ describe('Timeline profiler', () => { describe('errors thrown while rendering', () => { // @reactVersion >= 18.0 it('shoult parse Errors thrown during render', async () => { - spyOn(console, 'error'); + jest.spyOn(console, 'error'); class ErrorBoundary extends React.Component { state = {error: null}; diff --git a/packages/react-devtools-shared/src/__tests__/profilerStore-test.js b/packages/react-devtools-shared/src/__tests__/profilerStore-test.js index 4c6b0ad95440e..fcc4de34eafc7 100644 --- a/packages/react-devtools-shared/src/__tests__/profilerStore-test.js +++ b/packages/react-devtools-shared/src/__tests__/profilerStore-test.js @@ -73,7 +73,7 @@ describe('ProfilerStore', () => { const fauxProfilingData = { dataForRoots: new Map(), }; - spyOn(console, 'warn'); + jest.spyOn(console, 'warn').mockImplementation(() => {}); store.profilerStore.profilingData = fauxProfilingData; expect(store.profilerStore.profilingData).not.toBe(fauxProfilingData); expect(console.warn).toHaveBeenCalledTimes(1); diff --git a/packages/react-devtools-shared/src/__tests__/setupTests.js b/packages/react-devtools-shared/src/__tests__/setupTests.js index 9d68da1dba1e1..9a21dfa4a7b6d 100644 --- a/packages/react-devtools-shared/src/__tests__/setupTests.js +++ b/packages/react-devtools-shared/src/__tests__/setupTests.js @@ -32,8 +32,7 @@ if (compactConsole) { global.console = new CustomConsole(process.stdout, process.stderr, formatter); } -const env = jasmine.getEnv(); -env.beforeEach(() => { +beforeEach(() => { global.mockClipboardCopy = jest.fn(); // Test environment doesn't support document methods like execCommand() @@ -169,7 +168,7 @@ env.beforeEach(() => { } global.fetch = mockFetch; }); -env.afterEach(() => { +afterEach(() => { delete global.__REACT_DEVTOOLS_GLOBAL_HOOK__; // It's important to reset modules between test runs; diff --git a/packages/react-dom/src/__tests__/InvalidEventListeners-test.js b/packages/react-dom/src/__tests__/InvalidEventListeners-test.js index aeeed4bceb876..c5df3e1196ff5 100644 --- a/packages/react-dom/src/__tests__/InvalidEventListeners-test.js +++ b/packages/react-dom/src/__tests__/InvalidEventListeners-test.js @@ -65,7 +65,7 @@ describe('InvalidEventListeners', () => { if (!__DEV__) { expect(console.error).toHaveBeenCalledTimes(1); - expect(console.error.calls.argsFor(0)[0]).toEqual( + expect(console.error.mock.calls[0][0]).toEqual( expect.objectContaining({ detail: expect.objectContaining({ message: diff --git a/packages/react-dom/src/__tests__/ReactDOMComponent-test.js b/packages/react-dom/src/__tests__/ReactDOMComponent-test.js index b474103f97ad3..e0fe178d2fe8c 100644 --- a/packages/react-dom/src/__tests__/ReactDOMComponent-test.js +++ b/packages/react-dom/src/__tests__/ReactDOMComponent-test.js @@ -24,6 +24,10 @@ describe('ReactDOMComponent', () => { ReactTestUtils = require('react-dom/test-utils'); }); + afterEach(() => { + jest.restoreAllMocks(); + }); + describe('updateDOM', () => { it('should handle className', () => { const container = document.createElement('div'); @@ -1239,7 +1243,7 @@ describe('ReactDOMComponent', () => { if (__DEV__) { expect(console.log).toHaveBeenCalledTimes(1); - expect(console.log.calls.argsFor(0)[0]).toContain('onError called'); + expect(console.log.mock.calls[0][0]).toContain('onError called'); } }); @@ -1464,7 +1468,7 @@ describe('ReactDOMComponent', () => { it('should support custom elements which extend native elements', () => { const container = document.createElement('div'); - spyOnDevAndProd(document, 'createElement').and.callThrough(); + spyOnDevAndProd(document, 'createElement'); ReactDOM.render(
, container); expect(document.createElement).toHaveBeenCalledWith('div', { is: 'custom-div', @@ -1496,8 +1500,8 @@ describe('ReactDOMComponent', () => { if (__DEV__) { expect(console.log).toHaveBeenCalledTimes(2); - expect(console.log.calls.argsFor(0)[0]).toContain('onError called'); - expect(console.log.calls.argsFor(1)[0]).toContain('onLoad called'); + expect(console.log.mock.calls[0][0]).toContain('onError called'); + expect(console.log.mock.calls[1][0]).toContain('onLoad called'); } }); diff --git a/packages/react-dom/src/__tests__/ReactDOMConsoleErrorReporting-test.js b/packages/react-dom/src/__tests__/ReactDOMConsoleErrorReporting-test.js index 7e1a06b9de496..03c4f97c4946a 100644 --- a/packages/react-dom/src/__tests__/ReactDOMConsoleErrorReporting-test.js +++ b/packages/react-dom/src/__tests__/ReactDOMConsoleErrorReporting-test.js @@ -49,6 +49,7 @@ describe('ReactDOMConsoleErrorReporting', () => { afterEach(() => { document.body.removeChild(container); window.removeEventListener('error', windowOnError); + jest.restoreAllMocks(); }); describe('ReactDOMClient.createRoot', () => { @@ -95,7 +96,7 @@ describe('ReactDOMConsoleErrorReporting', () => { }), ], ]); - expect(console.error.calls.all().map(c => c.args)).toEqual([ + expect(console.error.mock.calls).toEqual([ [ // Reported because we're in a browser click event: expect.objectContaining({ @@ -125,7 +126,7 @@ describe('ReactDOMConsoleErrorReporting', () => { }), ], ]); - expect(console.error.calls.all().map(c => c.args)).toEqual([ + expect(console.error.mock.calls).toEqual([ [ // Reported because we're in a browser click event: expect.objectContaining({ @@ -140,14 +141,14 @@ describe('ReactDOMConsoleErrorReporting', () => { // Check next render doesn't throw. windowOnError.mockReset(); - console.error.calls.reset(); + console.error.mockReset(); act(() => { root.render(); }); expect(container.textContent).toBe('OK'); expect(windowOnError.mock.calls).toEqual([]); if (__DEV__) { - expect(console.error.calls.all().map(c => c.args)).toEqual([]); + expect(console.error.mock.calls).toEqual([]); } }); @@ -181,7 +182,7 @@ describe('ReactDOMConsoleErrorReporting', () => { }), ], ]); - expect(console.error.calls.all().map(c => c.args)).toEqual([ + expect(console.error.mock.calls).toEqual([ [ // Reported due to the guarded callback: expect.objectContaining({ @@ -212,7 +213,7 @@ describe('ReactDOMConsoleErrorReporting', () => { // The top-level error was caught with try/catch, and there's no guarded callback, // so in production we don't see an error event. expect(windowOnError.mock.calls).toEqual([]); - expect(console.error.calls.all().map(c => c.args)).toEqual([ + expect(console.error.mock.calls).toEqual([ [ // Reported by React with no extra message: expect.objectContaining({ @@ -224,14 +225,14 @@ describe('ReactDOMConsoleErrorReporting', () => { // Check next render doesn't throw. windowOnError.mockReset(); - console.error.calls.reset(); + console.error.mockReset(); act(() => { root.render(); }); expect(container.textContent).toBe('OK'); expect(windowOnError.mock.calls).toEqual([]); if (__DEV__) { - expect(console.error.calls.all().map(c => c.args)).toEqual([]); + expect(console.error.mock.calls).toEqual([]); } }); @@ -267,7 +268,7 @@ describe('ReactDOMConsoleErrorReporting', () => { }), ], ]); - expect(console.error.calls.all().map(c => c.args)).toEqual([ + expect(console.error.mock.calls).toEqual([ [ // Reported by jsdom due to the guarded callback: expect.objectContaining({ @@ -298,7 +299,7 @@ describe('ReactDOMConsoleErrorReporting', () => { // The top-level error was caught with try/catch, and there's no guarded callback, // so in production we don't see an error event. expect(windowOnError.mock.calls).toEqual([]); - expect(console.error.calls.all().map(c => c.args)).toEqual([ + expect(console.error.mock.calls).toEqual([ [ // Reported by React with no extra message: expect.objectContaining({ @@ -310,14 +311,14 @@ describe('ReactDOMConsoleErrorReporting', () => { // Check next render doesn't throw. windowOnError.mockReset(); - console.error.calls.reset(); + console.error.mockReset(); act(() => { root.render(); }); expect(container.textContent).toBe('OK'); expect(windowOnError.mock.calls).toEqual([]); if (__DEV__) { - expect(console.error.calls.all().map(c => c.args)).toEqual([]); + expect(console.error.mock.calls).toEqual([]); } }); @@ -347,7 +348,7 @@ describe('ReactDOMConsoleErrorReporting', () => { }), ], ]); - expect(console.error.calls.all().map(c => c.args)).toEqual([ + expect(console.error.mock.calls).toEqual([ [ // Reported due to the guarded callback: expect.objectContaining({ @@ -368,7 +369,7 @@ describe('ReactDOMConsoleErrorReporting', () => { // The top-level error was caught with try/catch, and there's no guarded callback, // so in production we don't see an error event. expect(windowOnError.mock.calls).toEqual([]); - expect(console.error.calls.all().map(c => c.args)).toEqual([ + expect(console.error.mock.calls).toEqual([ [ // Reported by React with no extra message: expect.objectContaining({ @@ -380,14 +381,14 @@ describe('ReactDOMConsoleErrorReporting', () => { // Check next render doesn't throw. windowOnError.mockReset(); - console.error.calls.reset(); + console.error.mockReset(); act(() => { root.render(); }); expect(container.textContent).toBe('OK'); expect(windowOnError.mock.calls).toEqual([]); if (__DEV__) { - expect(console.error.calls.all().map(c => c.args)).toEqual([]); + expect(console.error.mock.calls).toEqual([]); } }); @@ -419,7 +420,7 @@ describe('ReactDOMConsoleErrorReporting', () => { }), ], ]); - expect(console.error.calls.all().map(c => c.args)).toEqual([ + expect(console.error.mock.calls).toEqual([ [ // Reported by jsdom due to the guarded callback: expect.objectContaining({ @@ -440,7 +441,7 @@ describe('ReactDOMConsoleErrorReporting', () => { // The top-level error was caught with try/catch, and there's no guarded callback, // so in production we don't see an error event. expect(windowOnError.mock.calls).toEqual([]); - expect(console.error.calls.all().map(c => c.args)).toEqual([ + expect(console.error.mock.calls).toEqual([ [ // Reported by React with no extra message: expect.objectContaining({ @@ -452,14 +453,14 @@ describe('ReactDOMConsoleErrorReporting', () => { // Check next render doesn't throw. windowOnError.mockReset(); - console.error.calls.reset(); + console.error.mockReset(); act(() => { root.render(); }); expect(container.textContent).toBe('OK'); expect(windowOnError.mock.calls).toEqual([]); if (__DEV__) { - expect(console.error.calls.all().map(c => c.args)).toEqual([]); + expect(console.error.mock.calls).toEqual([]); } }); @@ -489,7 +490,7 @@ describe('ReactDOMConsoleErrorReporting', () => { }), ], ]); - expect(console.error.calls.all().map(c => c.args)).toEqual([ + expect(console.error.mock.calls).toEqual([ [ // Reported due to the guarded callback: expect.objectContaining({ @@ -510,7 +511,7 @@ describe('ReactDOMConsoleErrorReporting', () => { // The top-level error was caught with try/catch, and there's no guarded callback, // so in production we don't see an error event. expect(windowOnError.mock.calls).toEqual([]); - expect(console.error.calls.all().map(c => c.args)).toEqual([ + expect(console.error.mock.calls).toEqual([ [ // Reported by React with no extra message: expect.objectContaining({ @@ -522,14 +523,14 @@ describe('ReactDOMConsoleErrorReporting', () => { // Check next render doesn't throw. windowOnError.mockReset(); - console.error.calls.reset(); + console.error.mockReset(); act(() => { root.render(); }); expect(container.textContent).toBe('OK'); expect(windowOnError.mock.calls).toEqual([]); if (__DEV__) { - expect(console.error.calls.all().map(c => c.args)).toEqual([]); + expect(console.error.mock.calls).toEqual([]); } }); @@ -561,7 +562,7 @@ describe('ReactDOMConsoleErrorReporting', () => { }), ], ]); - expect(console.error.calls.all().map(c => c.args)).toEqual([ + expect(console.error.mock.calls).toEqual([ [ // Reported by jsdom due to the guarded callback: expect.objectContaining({ @@ -582,7 +583,7 @@ describe('ReactDOMConsoleErrorReporting', () => { // The top-level error was caught with try/catch, and there's no guarded callback, // so in production we don't see an error event. expect(windowOnError.mock.calls).toEqual([]); - expect(console.error.calls.all().map(c => c.args)).toEqual([ + expect(console.error.mock.calls).toEqual([ [ // Reported by React with no extra message: expect.objectContaining({ @@ -594,14 +595,14 @@ describe('ReactDOMConsoleErrorReporting', () => { // Check next render doesn't throw. windowOnError.mockReset(); - console.error.calls.reset(); + console.error.mockReset(); act(() => { root.render(); }); expect(container.textContent).toBe('OK'); expect(windowOnError.mock.calls).toEqual([]); if (__DEV__) { - expect(console.error.calls.all().map(c => c.args)).toEqual([]); + expect(console.error.mock.calls).toEqual([]); } }); }); @@ -649,7 +650,7 @@ describe('ReactDOMConsoleErrorReporting', () => { }), ], ]); - expect(console.error.calls.all().map(c => c.args)).toEqual([ + expect(console.error.mock.calls).toEqual([ [expect.stringContaining('ReactDOM.render is no longer supported')], [ // Reported because we're in a browser click event: @@ -680,7 +681,7 @@ describe('ReactDOMConsoleErrorReporting', () => { }), ], ]); - expect(console.error.calls.all().map(c => c.args)).toEqual([ + expect(console.error.mock.calls).toEqual([ [ // Reported because we're in a browser click event: expect.objectContaining({ @@ -695,14 +696,14 @@ describe('ReactDOMConsoleErrorReporting', () => { // Check next render doesn't throw. windowOnError.mockReset(); - console.error.calls.reset(); + console.error.mockReset(); act(() => { ReactDOM.render(, container); }); expect(container.textContent).toBe('OK'); expect(windowOnError.mock.calls).toEqual([]); if (__DEV__) { - expect(console.error.calls.all().map(c => c.args)).toEqual([ + expect(console.error.mock.calls).toEqual([ [expect.stringContaining('ReactDOM.render is no longer supported')], ]); } @@ -730,7 +731,7 @@ describe('ReactDOMConsoleErrorReporting', () => { }), ], ]); - expect(console.error.calls.all().map(c => c.args)).toEqual([ + expect(console.error.mock.calls).toEqual([ [expect.stringContaining('ReactDOM.render is no longer supported')], [ // Reported due to the guarded callback: @@ -752,7 +753,7 @@ describe('ReactDOMConsoleErrorReporting', () => { // The top-level error was caught with try/catch, and there's no guarded callback, // so in production we don't see an error event. expect(windowOnError.mock.calls).toEqual([]); - expect(console.error.calls.all().map(c => c.args)).toEqual([ + expect(console.error.mock.calls).toEqual([ [ // Reported by React with no extra message: expect.objectContaining({ @@ -764,14 +765,14 @@ describe('ReactDOMConsoleErrorReporting', () => { // Check next render doesn't throw. windowOnError.mockReset(); - console.error.calls.reset(); + console.error.mockReset(); act(() => { ReactDOM.render(, container); }); expect(container.textContent).toBe('OK'); expect(windowOnError.mock.calls).toEqual([]); if (__DEV__) { - expect(console.error.calls.all().map(c => c.args)).toEqual([ + expect(console.error.mock.calls).toEqual([ [expect.stringContaining('ReactDOM.render is no longer supported')], ]); } @@ -802,7 +803,7 @@ describe('ReactDOMConsoleErrorReporting', () => { }), ], ]); - expect(console.error.calls.all().map(c => c.args)).toEqual([ + expect(console.error.mock.calls).toEqual([ [expect.stringContaining('ReactDOM.render is no longer supported')], [ // Reported by jsdom due to the guarded callback: @@ -824,7 +825,7 @@ describe('ReactDOMConsoleErrorReporting', () => { // The top-level error was caught with try/catch, and there's no guarded callback, // so in production we don't see an error event. expect(windowOnError.mock.calls).toEqual([]); - expect(console.error.calls.all().map(c => c.args)).toEqual([ + expect(console.error.mock.calls).toEqual([ [ // Reported by React with no extra message: expect.objectContaining({ @@ -836,14 +837,14 @@ describe('ReactDOMConsoleErrorReporting', () => { // Check next render doesn't throw. windowOnError.mockReset(); - console.error.calls.reset(); + console.error.mockReset(); act(() => { ReactDOM.render(, container); }); expect(container.textContent).toBe('OK'); expect(windowOnError.mock.calls).toEqual([]); if (__DEV__) { - expect(console.error.calls.all().map(c => c.args)).toEqual([ + expect(console.error.mock.calls).toEqual([ [expect.stringContaining('ReactDOM.render is no longer supported')], ]); } @@ -874,7 +875,7 @@ describe('ReactDOMConsoleErrorReporting', () => { }), ], ]); - expect(console.error.calls.all().map(c => c.args)).toEqual([ + expect(console.error.mock.calls).toEqual([ [expect.stringContaining('ReactDOM.render is no longer supported')], [ // Reported due to the guarded callback: @@ -896,7 +897,7 @@ describe('ReactDOMConsoleErrorReporting', () => { // The top-level error was caught with try/catch, and there's no guarded callback, // so in production we don't see an error event. expect(windowOnError.mock.calls).toEqual([]); - expect(console.error.calls.all().map(c => c.args)).toEqual([ + expect(console.error.mock.calls).toEqual([ [ // Reported by React with no extra message: expect.objectContaining({ @@ -908,14 +909,14 @@ describe('ReactDOMConsoleErrorReporting', () => { // Check next render doesn't throw. windowOnError.mockReset(); - console.error.calls.reset(); + console.error.mockReset(); act(() => { ReactDOM.render(, container); }); expect(container.textContent).toBe('OK'); expect(windowOnError.mock.calls).toEqual([]); if (__DEV__) { - expect(console.error.calls.all().map(c => c.args)).toEqual([ + expect(console.error.mock.calls).toEqual([ [expect.stringContaining('ReactDOM.render is no longer supported')], ]); } @@ -949,7 +950,7 @@ describe('ReactDOMConsoleErrorReporting', () => { }), ], ]); - expect(console.error.calls.all().map(c => c.args)).toEqual([ + expect(console.error.mock.calls).toEqual([ [expect.stringContaining('ReactDOM.render is no longer supported')], [ // Reported by jsdom due to the guarded callback: @@ -971,7 +972,7 @@ describe('ReactDOMConsoleErrorReporting', () => { // The top-level error was caught with try/catch, and there's no guarded callback, // so in production we don't see an error event. expect(windowOnError.mock.calls).toEqual([]); - expect(console.error.calls.all().map(c => c.args)).toEqual([ + expect(console.error.mock.calls).toEqual([ [ // Reported by React with no extra message: expect.objectContaining({ @@ -983,14 +984,14 @@ describe('ReactDOMConsoleErrorReporting', () => { // Check next render doesn't throw. windowOnError.mockReset(); - console.error.calls.reset(); + console.error.mockReset(); act(() => { ReactDOM.render(, container); }); expect(container.textContent).toBe('OK'); expect(windowOnError.mock.calls).toEqual([]); if (__DEV__) { - expect(console.error.calls.all().map(c => c.args)).toEqual([ + expect(console.error.mock.calls).toEqual([ [expect.stringContaining('ReactDOM.render is no longer supported')], ]); } @@ -1021,7 +1022,7 @@ describe('ReactDOMConsoleErrorReporting', () => { }), ], ]); - expect(console.error.calls.all().map(c => c.args)).toEqual([ + expect(console.error.mock.calls).toEqual([ [expect.stringContaining('ReactDOM.render is no longer supported')], [ // Reported due to the guarded callback: @@ -1043,7 +1044,7 @@ describe('ReactDOMConsoleErrorReporting', () => { // The top-level error was caught with try/catch, and there's no guarded callback, // so in production we don't see an error event. expect(windowOnError.mock.calls).toEqual([]); - expect(console.error.calls.all().map(c => c.args)).toEqual([ + expect(console.error.mock.calls).toEqual([ [ // Reported by React with no extra message: expect.objectContaining({ @@ -1055,14 +1056,14 @@ describe('ReactDOMConsoleErrorReporting', () => { // Check next render doesn't throw. windowOnError.mockReset(); - console.error.calls.reset(); + console.error.mockReset(); act(() => { ReactDOM.render(, container); }); expect(container.textContent).toBe('OK'); expect(windowOnError.mock.calls).toEqual([]); if (__DEV__) { - expect(console.error.calls.all().map(c => c.args)).toEqual([ + expect(console.error.mock.calls).toEqual([ [expect.stringContaining('ReactDOM.render is no longer supported')], ]); } @@ -1096,7 +1097,7 @@ describe('ReactDOMConsoleErrorReporting', () => { }), ], ]); - expect(console.error.calls.all().map(c => c.args)).toEqual([ + expect(console.error.mock.calls).toEqual([ [expect.stringContaining('ReactDOM.render is no longer supported')], [ // Reported by jsdom due to the guarded callback: @@ -1118,7 +1119,7 @@ describe('ReactDOMConsoleErrorReporting', () => { // The top-level error was caught with try/catch, and there's no guarded callback, // so in production we don't see an error event. expect(windowOnError.mock.calls).toEqual([]); - expect(console.error.calls.all().map(c => c.args)).toEqual([ + expect(console.error.mock.calls).toEqual([ [ // Reported by React with no extra message: expect.objectContaining({ @@ -1130,14 +1131,14 @@ describe('ReactDOMConsoleErrorReporting', () => { // Check next render doesn't throw. windowOnError.mockReset(); - console.error.calls.reset(); + console.error.mockReset(); act(() => { ReactDOM.render(, container); }); expect(container.textContent).toBe('OK'); expect(windowOnError.mock.calls).toEqual([]); if (__DEV__) { - expect(console.error.calls.all().map(c => c.args)).toEqual([ + expect(console.error.mock.calls).toEqual([ [expect.stringContaining('ReactDOM.render is no longer supported')], ]); } diff --git a/packages/react-dom/src/__tests__/ReactDOMFiber-test.js b/packages/react-dom/src/__tests__/ReactDOMFiber-test.js index b342cc9461522..865337e8b5470 100644 --- a/packages/react-dom/src/__tests__/ReactDOMFiber-test.js +++ b/packages/react-dom/src/__tests__/ReactDOMFiber-test.js @@ -24,6 +24,7 @@ describe('ReactDOMFiber', () => { afterEach(() => { document.body.removeChild(container); container = null; + jest.restoreAllMocks(); }); it('should render strings as children', () => { @@ -1154,11 +1155,11 @@ describe('ReactDOMFiber', () => { expect(ops).toEqual(['A']); if (__DEV__) { - expect(console.error.calls.count()).toBe(2); - expect(console.error.calls.argsFor(0)[0]).toMatch( + expect(console.error).toHaveBeenCalledTimes(2); + expect(console.error.mock.calls[0][0]).toMatch( 'ReactDOM.render is no longer supported in React 18', ); - expect(console.error.calls.argsFor(1)[0]).toMatch( + expect(console.error.mock.calls[1][0]).toMatch( 'ReactDOM.render is no longer supported in React 18', ); } @@ -1257,7 +1258,7 @@ describe('ReactDOMFiber', () => { let actualDocument; let textNode; - spyOnDevAndProd(iframeContainer, 'appendChild').and.callFake(node => { + spyOnDevAndProd(iframeContainer, 'appendChild').mockImplementation(node => { actualDocument = node.ownerDocument; textNode = node; }); diff --git a/packages/react-dom/src/__tests__/ReactDOMFizzShellHydration-test.js b/packages/react-dom/src/__tests__/ReactDOMFizzShellHydration-test.js index 6d770d571b83a..a6580d58d9ebd 100644 --- a/packages/react-dom/src/__tests__/ReactDOMFizzShellHydration-test.js +++ b/packages/react-dom/src/__tests__/ReactDOMFizzShellHydration-test.js @@ -63,6 +63,10 @@ describe('ReactDOMFizzShellHydration', () => { }); }); + afterEach(() => { + jest.restoreAllMocks(); + }); + async function serverAct(callback) { await callback(); // Await one turn around the event loop. @@ -286,7 +290,7 @@ describe('ReactDOMFizzShellHydration', () => { }); test('TODO: A large component stack causes SSR to stack overflow', async () => { - spyOnDevAndProd(console, 'error'); + spyOnDevAndProd(console, 'error').mockImplementation(() => {}); function NestedComponent({depth}: {depth: number}) { if (depth <= 0) { @@ -302,7 +306,7 @@ describe('ReactDOMFizzShellHydration', () => { ); }); expect(console.error).toHaveBeenCalledTimes(1); - expect(console.error.calls.argsFor(0)[0].toString()).toBe( + expect(console.error.mock.calls[0][0].toString()).toBe( 'RangeError: Maximum call stack size exceeded', ); }); diff --git a/packages/react-dom/src/__tests__/ReactDOMInput-test.js b/packages/react-dom/src/__tests__/ReactDOMInput-test.js index d75c4ded9e458..7fe9bb1728de1 100644 --- a/packages/react-dom/src/__tests__/ReactDOMInput-test.js +++ b/packages/react-dom/src/__tests__/ReactDOMInput-test.js @@ -48,6 +48,7 @@ describe('ReactDOMInput', () => { afterEach(() => { document.body.removeChild(container); + jest.restoreAllMocks(); }); it('should warn for controlled value of 0 with missing onChange', () => { @@ -1670,7 +1671,9 @@ describe('ReactDOMInput', () => { it('sets type, step, min, max before value always', () => { const log = []; const originalCreateElement = document.createElement; - spyOnDevAndProd(document, 'createElement').and.callFake(function (type) { + spyOnDevAndProd(document, 'createElement').mockImplementation(function ( + type, + ) { const el = originalCreateElement.apply(this, arguments); let value = ''; @@ -1684,7 +1687,7 @@ describe('ReactDOMInput', () => { log.push('set property value'); }, }); - spyOnDevAndProd(el, 'setAttribute').and.callFake(function (name) { + spyOnDevAndProd(el, 'setAttribute').mockImplementation(function (name) { log.push('set attribute ' + name); }); } @@ -1743,7 +1746,9 @@ describe('ReactDOMInput', () => { const log = []; const originalCreateElement = document.createElement; - spyOnDevAndProd(document, 'createElement').and.callFake(function (type) { + spyOnDevAndProd(document, 'createElement').mockImplementation(function ( + type, + ) { const el = originalCreateElement.apply(this, arguments); const getDefaultValue = Object.getOwnPropertyDescriptor( HTMLInputElement.prototype, @@ -1780,7 +1785,10 @@ describe('ReactDOMInput', () => { setValue.call(this, val); }, }); - spyOnDevAndProd(el, 'setAttribute').and.callFake(function (name, val) { + spyOnDevAndProd(el, 'setAttribute').mockImplementation(function ( + name, + val, + ) { log.push(`node.setAttribute(${strify(name)}, ${strify(val)})`); }); } diff --git a/packages/react-dom/src/__tests__/ReactDOMTextarea-test.js b/packages/react-dom/src/__tests__/ReactDOMTextarea-test.js index 7dcb706993803..c926224afbc02 100644 --- a/packages/react-dom/src/__tests__/ReactDOMTextarea-test.js +++ b/packages/react-dom/src/__tests__/ReactDOMTextarea-test.js @@ -42,6 +42,10 @@ describe('ReactDOMTextarea', () => { }; }); + afterEach(() => { + jest.restoreAllMocks(); + }); + it('should allow setting `defaultValue`', () => { const container = document.createElement('div'); const node = renderTextarea(