Skip to content

Commit

Permalink
Ignore infinite timers in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed Mar 5, 2025
1 parent 3b6aa41 commit bc9f075
Show file tree
Hide file tree
Showing 25 changed files with 78 additions and 87 deletions.
3 changes: 2 additions & 1 deletion packages/internal-test-utils/internalAct.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,8 @@ export async function serverAct<T>(scope: () => Thenable<T>): Thenable<T> {

// $FlowFixMe[cannot-resolve-name]: Flow doesn't know about global Jest object
const j = jest;
if (j.getTimerCount() > 0) {
// We have always one pending timer running that resets the Owner Stack limit.
if (j.getTimerCount() > 1) {
// There's a pending timer. Flush it now. We only do this in order to
// force Suspense fallbacks to display; the fact that it's a timer
// is an implementation detail. If there are other timers scheduled,
Expand Down
6 changes: 3 additions & 3 deletions packages/react-devtools-shared/src/__tests__/bridge-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ describe('Bridge', () => {

// Check that we're wired up correctly.
bridge.send('reloadAppForProfiling');
jest.runAllTimers();
jest.runOnlyPendingTimers();
expect(wall.send).toHaveBeenCalledWith('reloadAppForProfiling');

// Should flush pending messages and then shut down.
wall.send.mockClear();
bridge.send('update', '1');
bridge.send('update', '2');
bridge.shutdown();
jest.runAllTimers();
jest.runOnlyPendingTimers();
expect(wall.send).toHaveBeenCalledWith('update', '1');
expect(wall.send).toHaveBeenCalledWith('update', '2');
expect(wall.send).toHaveBeenCalledWith('shutdown');
Expand All @@ -44,7 +44,7 @@ describe('Bridge', () => {
jest.spyOn(console, 'warn').mockImplementation(() => {});
wall.send.mockClear();
bridge.send('should not send');
jest.runAllTimers();
jest.runOnlyPendingTimers();
expect(wall.send).not.toHaveBeenCalled();
expect(console.warn).toHaveBeenCalledWith(
'Cannot send message "should not send" through a Bridge that has been shutdown.',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('editing interface', () => {
const act = (callback: Function) => {
callback();

jest.runAllTimers(); // Flush Bridge operations
jest.runOnlyPendingTimers(); // Flush Bridge operations
};

const flushPendingUpdates = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('InspectedElementContext', () => {
const act = (callback: Function) => {
callback();

jest.runAllTimers(); // Flush Bridge operations
jest.runOnlyPendingTimers(); // Flush Bridge operations
};

async function read(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('Store (legacy)', () => {
let store;
const act = (callback: Function) => {
callback();
jest.runAllTimers(); // Flush Bridge operations
jest.runOnlyPendingTimers(); // Flush Bridge operations
};
beforeEach(() => {
store = global.store;
Expand Down
6 changes: 3 additions & 3 deletions packages/react-devtools-shared/src/__tests__/store-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1978,7 +1978,7 @@ describe('Store', () => {
clearErrorsAndWarnings({bridge, store});

// flush events to the renderer
jest.runAllTimers();
jest.runOnlyPendingTimers();

expect(store).toMatchInlineSnapshot(`
[root]
Expand Down Expand Up @@ -2022,7 +2022,7 @@ describe('Store', () => {
clearWarningsForElement({bridge, id, rendererID});

// Flush events to the renderer.
jest.runAllTimers();
jest.runOnlyPendingTimers();

expect(store).toMatchInlineSnapshot(`
✕ 2, ⚠ 1
Expand Down Expand Up @@ -2067,7 +2067,7 @@ describe('Store', () => {
clearErrorsForElement({bridge, id, rendererID});

// Flush events to the renderer.
jest.runAllTimers();
jest.runOnlyPendingTimers();

expect(store).toMatchInlineSnapshot(`
✕ 1, ⚠ 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1421,21 +1421,21 @@ describe('TreeListContext', () => {
function clearAllErrors() {
utils.act(() => clearErrorsAndWarningsAPI({bridge, store}));
// flush events to the renderer
jest.runAllTimers();
jest.runOnlyPendingTimers();
}

function clearErrorsForElement(id) {
const rendererID = store.getRendererIDForElement(id);
utils.act(() => clearErrorsForElementAPI({bridge, id, rendererID}));
// flush events to the renderer
jest.runAllTimers();
jest.runOnlyPendingTimers();
}

function clearWarningsForElement(id) {
const rendererID = store.getRendererIDForElement(id);
utils.act(() => clearWarningsForElementAPI({bridge, id, rendererID}));
// flush events to the renderer
jest.runAllTimers();
jest.runOnlyPendingTimers();
}

function selectNextErrorOrWarning() {
Expand Down Expand Up @@ -2358,7 +2358,7 @@ describe('TreeListContext', () => {
);
utils.act(() => TestRenderer.create(<Contexts />));

jest.runAllTimers();
jest.runOnlyPendingTimers();

expect(state).toMatchInlineSnapshot(`
[root]
Expand Down
10 changes: 6 additions & 4 deletions packages/react-devtools-shared/src/__tests__/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,11 @@ export function act(

if (recursivelyFlush) {
// Flush Bridge operations
while (jest.getTimerCount() > 0) {
// We have always one pending timer running that resets the Owner Stack limit.
while (jest.getTimerCount() > 1) {
actDOM(() => {
actTestRenderer(() => {
jest.runAllTimers();
jest.runOnlyPendingTimers();
});
});
}
Expand All @@ -107,10 +108,11 @@ export async function actAsync(
});

if (recursivelyFlush) {
while (jest.getTimerCount() > 0) {
// We have always one pending timer running that resets the Owner Stack limit.
while (jest.getTimerCount() > 1) {
await actDOM(async () => {
await actTestRenderer(async () => {
jest.runAllTimers();
jest.runOnlyPendingTimers();
});
});
}
Expand Down
6 changes: 3 additions & 3 deletions packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2881,7 +2881,7 @@ describe('ReactDOMFizzServer', () => {
root.render(<App color="blue" />);
});
await waitForAll([]);
jest.runAllTimers();
jest.runOnlyPendingTimers();
const clientFallback2 = container.getElementsByTagName('p')[0];
expect(clientFallback2).toBe(serverFallback);

Expand Down Expand Up @@ -2987,7 +2987,7 @@ describe('ReactDOMFizzServer', () => {
// actually force it to re-render on the client and throw away the server one.
root.render(<App fallbackText="More loading..." />);
await waitForAll([]);
jest.runAllTimers();
jest.runOnlyPendingTimers();
assertLog([
'onRecoverableError: The server could not finish this Suspense boundary, ' +
'likely due to an error during server rendering. ' +
Expand Down Expand Up @@ -6414,7 +6414,7 @@ describe('ReactDOMFizzServer', () => {
ref.current.dispatchEvent(
new window.MouseEvent('click', {bubbles: true}),
);
await jest.runAllTimers();
await jest.runOnlyPendingTimers();
expect(getVisibleChildren(container)).toEqual(<button>1</button>);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,14 @@ describe('ReactDOMFizzServerNode', () => {
},
},
);
await jest.runAllTimers();
await jest.runOnlyPendingTimers();
expect(output.result).toBe('');
expect(isCompleteCalls).toBe(0);
// Resolve the loading.
hasLoaded = true;
await resolve();

await jest.runAllTimers();
await jest.runOnlyPendingTimers();

expect(output.result).toBe('');
expect(isCompleteCalls).toBe(1);
Expand Down Expand Up @@ -354,7 +354,7 @@ describe('ReactDOMFizzServerNode', () => {
const theReason = new Error('uh oh');
abort(theReason);

jest.runAllTimers();
jest.runOnlyPendingTimers();

await completed;

Expand Down Expand Up @@ -632,7 +632,7 @@ describe('ReactDOMFizzServerNode', () => {

writable.end();

await jest.runAllTimers();
await jest.runOnlyPendingTimers();

hasLoaded = true;
resolve();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ describe('ReactDOMFizzStaticBrowser', () => {
),
);

await jest.runAllTimers();
await jest.runOnlyPendingTimers();

// Resolve the loading.
hasLoaded = true;
Expand Down Expand Up @@ -354,7 +354,7 @@ describe('ReactDOMFizzStaticBrowser', () => {
),
);

await jest.runAllTimers();
await jest.runOnlyPendingTimers();

const theReason = new Error('aborted for reasons');
controller.abort(theReason);
Expand Down Expand Up @@ -387,7 +387,7 @@ describe('ReactDOMFizzStaticBrowser', () => {
),
);

await jest.runAllTimers();
await jest.runOnlyPendingTimers();

const theReason = new Error('aborted for reasons');
controller.abort(theReason);
Expand Down Expand Up @@ -1377,7 +1377,7 @@ describe('ReactDOMFizzStaticBrowser', () => {
),
);

await jest.runAllTimers();
await jest.runOnlyPendingTimers();

expect(getVisibleChildren(container)).toEqual(<div>Loading...</div>);

Expand Down
12 changes: 6 additions & 6 deletions packages/react-dom/src/__tests__/ReactDOMFizzStaticNode-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ describe('ReactDOMFizzStaticNode', () => {
</div>,
);

await jest.runAllTimers();
await jest.runOnlyPendingTimers();

// Resolve the loading.
hasLoaded = true;
Expand Down Expand Up @@ -199,7 +199,7 @@ describe('ReactDOMFizzStaticNode', () => {
},
);

await jest.runAllTimers();
await jest.runOnlyPendingTimers();

controller.abort();

Expand Down Expand Up @@ -228,7 +228,7 @@ describe('ReactDOMFizzStaticNode', () => {
},
);

await jest.runAllTimers();
await jest.runOnlyPendingTimers();

const theReason = new Error('aborted for reasons');
controller.abort(theReason);
Expand Down Expand Up @@ -259,7 +259,7 @@ describe('ReactDOMFizzStaticNode', () => {
},
);

await jest.runAllTimers();
await jest.runOnlyPendingTimers();

const theReason = new Error('aborted for reasons');
controller.abort(theReason);
Expand Down Expand Up @@ -422,7 +422,7 @@ describe('ReactDOMFizzStaticNode', () => {
},
});

await jest.runAllTimers();
await jest.runOnlyPendingTimers();

controller.abort('foobar');

Expand Down Expand Up @@ -464,7 +464,7 @@ describe('ReactDOMFizzStaticNode', () => {
},
});

await jest.runAllTimers();
await jest.runOnlyPendingTimers();

controller.abort(new Error('uh oh'));

Expand Down
Loading

0 comments on commit bc9f075

Please sign in to comment.