Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

test(core): the StyleX rule recorders load their components before any test, not inside a 30 s hook (#10014)

Christopher Chedeau634b649910ab
diff --git a/packages/core/src/AppShell/AppShellSafeArea.test.tsx b/packages/core/src/AppShell/AppShellSafeArea.test.tsx
index 248ef82919..44d10519b5 100644
--- a/packages/core/src/AppShell/AppShellSafeArea.test.tsx
+++ b/packages/core/src/AppShell/AppShellSafeArea.test.tsx
@@ -13,37 +13,28 @@
*/
import {render, screen} from '@testing-library/react';
-import {beforeAll, beforeEach, describe, expect, it, vi} from 'vitest';
-import type {AppShell as AppShellComponent} from './AppShell';
-import type {
- SideNav as SideNavComponent,
- SideNavItem as SideNavItemComponent,
- SideNavSection as SideNavSectionComponent,
-} from '../SideNav';
+import {beforeEach, describe, expect, it, vi} from 'vitest';
/** Every rule StyleX inserted, as the raw text it handed to `insertRule`. */
const rawRules: string[] = [];
-let AppShell: typeof AppShellComponent;
-let SideNav: typeof SideNavComponent;
-let SideNavItem: typeof SideNavItemComponent;
-let SideNavSection: typeof SideNavSectionComponent;
-beforeAll(async () => {
- const insertRule = CSSStyleSheet.prototype.insertRule;
- CSSStyleSheet.prototype.insertRule = function (
- this: CSSStyleSheet,
- rule: string,
- index?: number,
- ) {
- rawRules.push(rule);
- return insertRule.call(this, rule, index);
- };
- // StyleX injects when the styles are created: load AppShell after the
- // recorder is in place.
- vi.resetModules();
- ({AppShell} = await import('./AppShell'));
- ({SideNav, SideNavItem, SideNavSection} = await import('../SideNav'));
-});
+const insertRule = CSSStyleSheet.prototype.insertRule;
+CSSStyleSheet.prototype.insertRule = function (
+ this: CSSStyleSheet,
+ rule: string,
+ index?: number,
+) {
+ rawRules.push(rule);
+ return insertRule.call(this, rule, index);
+};
+// StyleX injects when the styles are created: load AppShell after the
+// recorder is in place. The file loads them before any test runs, so no
+// hook's time limit covers the load.
+vi.resetModules();
+const {AppShell} = await import('./AppShell');
+const {SideNav} = await import('../SideNav/SideNav');
+const {SideNavItem} = await import('../SideNav/SideNavItem');
+const {SideNavSection} = await import('../SideNav/SideNavSection');
function stubMatchMedia(matches: boolean) {
vi.stubGlobal(
diff --git a/packages/core/src/__tests__/modalBackdrop.test.tsx b/packages/core/src/__tests__/modalBackdrop.test.tsx
index 18ec52de71..2a44c5e24a 100644
--- a/packages/core/src/__tests__/modalBackdrop.test.tsx
+++ b/packages/core/src/__tests__/modalBackdrop.test.tsx
@@ -17,33 +17,26 @@
import {render} from '@testing-library/react';
import type {ReactElement} from 'react';
import {beforeAll, describe, expect, it} from 'vitest';
-import type {BottomSheet as BottomSheetComponent} from '../BottomSheet/BottomSheet';
-import type {Dialog as DialogComponent} from '../Dialog/Dialog';
-import type {Drawer as DrawerComponent} from '../Drawer/Drawer';
-import type {MobileNav as MobileNavComponent} from '../MobileNav/MobileNav';
-let BottomSheet: typeof BottomSheetComponent;
-let Dialog: typeof DialogComponent;
-let Drawer: typeof DrawerComponent;
-let MobileNav: typeof MobileNavComponent;
+/** Every rule StyleX inserted, as the raw text it handed to `insertRule`. */
+const rawRules: string[] = [];
// StyleX injects a component's rules when its module loads: record them
-// first, then load the modals.
-beforeAll(async () => {
- const insertRule = CSSStyleSheet.prototype.insertRule;
- CSSStyleSheet.prototype.insertRule = function (
- this: CSSStyleSheet,
- rule: string,
- index?: number,
- ) {
- rawRules.push(rule);
- return insertRule.call(this, rule, index);
- };
- ({BottomSheet} = await import('../BottomSheet/BottomSheet'));
- ({Dialog} = await import('../Dialog/Dialog'));
- ({Drawer} = await import('../Drawer/Drawer'));
- ({MobileNav} = await import('../MobileNav/MobileNav'));
-});
+// first, then load the modals. The file loads them before any test runs, so
+// no hook's time limit covers the load.
+const insertRule = CSSStyleSheet.prototype.insertRule;
+CSSStyleSheet.prototype.insertRule = function (
+ this: CSSStyleSheet,
+ rule: string,
+ index?: number,
+) {
+ rawRules.push(rule);
+ return insertRule.call(this, rule, index);
+};
+const {BottomSheet} = await import('../BottomSheet/BottomSheet');
+const {Dialog} = await import('../Dialog/Dialog');
+const {Drawer} = await import('../Drawer/Drawer');
+const {MobileNav} = await import('../MobileNav/MobileNav');
// jsdom does not implement showModal/close on <dialog>.
beforeAll(() => {
@@ -59,9 +52,6 @@ beforeAll(() => {
};
});
-/** Every rule StyleX inserted, as the raw text it handed to `insertRule`. */
-const rawRules: string[] = [];
-
/**
* The declarations that the element's StyleX classes make on its `::backdrop`,
* by property. With `pseudo: ''`, they are the declarations on the element.