Skip to content

Commit cf6e816

Browse files
authored
[code-infra] Stabilize fake timers in regression tests (#19719)
1 parent 943e431 commit cf6e816

File tree

3 files changed

+36
-19
lines changed

3 files changed

+36
-19
lines changed

test/regressions/TestViewer.tsx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import * as React from 'react';
22
import { useLocation } from 'react-router';
33
import { styled } from '@mui/material/styles';
44
import GlobalStyles from '@mui/material/GlobalStyles';
5-
import { fakeClock, setupFakeClock } from '../utils/setupFakeClock'; // eslint-disable-line
5+
// eslint-disable-next-line import/no-relative-packages
6+
import { fakeClock, setupFakeClock } from '../utils/setupFakeClock';
67

78
const StyledBox = styled('div', {
89
shouldForwardProp: (prop) => prop !== 'isDataGridTest' && prop !== 'isDataGridPivotTest',
@@ -75,15 +76,16 @@ function TestViewer(props: any) {
7576
}
7677

7778
function MockTime(props: React.PropsWithChildren<{ shouldAdvanceTime: boolean }>) {
78-
const [ready, setReady] = React.useState(false);
79+
const [dispose, setDispose] = React.useState<(() => void) | null>(null);
80+
const [prevShouldAdvanceTime, setPrevShouldAdvanceTime] = React.useState(props.shouldAdvanceTime);
7981

80-
React.useEffect(() => {
81-
const dispose = setupFakeClock(props.shouldAdvanceTime);
82-
setReady(true);
83-
return dispose;
84-
}, [props.shouldAdvanceTime]);
82+
if (!dispose || prevShouldAdvanceTime !== props.shouldAdvanceTime) {
83+
dispose?.();
84+
setDispose(() => setupFakeClock(props.shouldAdvanceTime));
85+
setPrevShouldAdvanceTime(props.shouldAdvanceTime);
86+
}
8587

86-
return ready ? props.children : null;
88+
return props.children;
8789
}
8890

8991
function LoadFont(props: any) {

test/regressions/index.tsx

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import * as React from 'react';
22
import * as ReactDOM from 'react-dom/client';
33
import { createBrowserRouter, RouterProvider, Outlet, NavLink, useNavigate } from 'react-router';
44
import { Globals } from '@react-spring/web';
5-
import { setupFakeClock, restoreFakeClock } from '../utils/setupFakeClock'; // eslint-disable-line
6-
import { generateTestLicenseKey, setupTestLicenseKey } from '../utils/testLicense'; // eslint-disable-line
5+
// eslint-disable-next-line import/no-relative-packages
6+
import '../utils/setupFakeClock';
7+
// eslint-disable-next-line import/no-relative-packages
8+
import { generateTestLicenseKey, setupTestLicenseKey } from '../utils/testLicense';
79
import TestViewer from './TestViewer';
8-
import type { Test } from './testsBySuite';
10+
import { type Test, testsBySuite } from './testsBySuite';
911

1012
setupTestLicenseKey(generateTestLicenseKey(new Date('2099-01-01')));
1113

@@ -29,17 +31,9 @@ window.muiFixture = {
2931
},
3032
};
3133

32-
let testsBySuite: typeof import('./testsBySuite').testsBySuite;
33-
3434
main();
3535

3636
async function main() {
37-
setupFakeClock();
38-
39-
testsBySuite = (await import('./testsBySuite')).testsBySuite;
40-
41-
restoreFakeClock();
42-
4337
ReactDOM.createRoot(document.getElementById('react-root')!).render(<App />);
4438
}
4539

test/regressions/vite.config.mts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import path from 'path';
22
import { defineConfig, transformWithEsbuild } from 'vite';
33
import react from '@vitejs/plugin-react';
4+
import * as fs from 'fs/promises';
45
import { alias } from '../../vitest.shared.mts';
56

67
export default defineConfig({
@@ -20,6 +21,26 @@ export default defineConfig({
2021
worker: {
2122
format: 'es',
2223
},
24+
optimizeDeps: {
25+
esbuildOptions: {
26+
plugins: [
27+
{
28+
name: 'js-as-jsx',
29+
setup(build) {
30+
build.onLoad({ filter: /\.js$/ }, async (args) => {
31+
if (args.path.includes('/node_modules/')) {
32+
return null;
33+
}
34+
35+
const contents = await fs.readFile(args.path, 'utf8');
36+
37+
return { contents, loader: 'jsx' };
38+
});
39+
},
40+
},
41+
],
42+
},
43+
},
2344
plugins: [
2445
react(),
2546
{

0 commit comments

Comments
 (0)