From 1fc443164a1b49a50b1126942c775e1053ac04d6 Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Fri, 2 Oct 2026 11:09:40 +0200 Subject: [PATCH] ESLint: Allow non-module stylesheet imports in unit tests --- .../src/cover/test/edit.browser.test.js | 2 - .../src/card/test/index.browser.test.tsx | 1 - .../test/index.browser.test.tsx | 1 - .../test/index.browser.test.tsx | 4 -- .../menu/test/scroll-lock.browser.test.tsx | 1 - .../test/spacing.browser.test.tsx | 2 - .../tools-panel/test/index.browser.test.tsx | 1 - .../components/test/cropper.browser.test.tsx | 1 - .../use-media-editor-state.browser.test.tsx | 1 - .../src/test/theme-provider.browser.test.tsx | 1 - .../progress/test/progress.browser.test.tsx | 1 - tools/eslint/config.mjs | 2 + tools/eslint/validate-test-config.mjs | 39 +++++++++++++++++++ 13 files changed, 41 insertions(+), 16 deletions(-) diff --git a/packages/block-library/src/cover/test/edit.browser.test.js b/packages/block-library/src/cover/test/edit.browser.test.js index 9f770910181e3b..4ac1b90a0a4587 100644 --- a/packages/block-library/src/cover/test/edit.browser.test.js +++ b/packages/block-library/src/cover/test/edit.browser.test.js @@ -8,9 +8,7 @@ import { } from '@wordpress/integration-tests/helpers/integration-test-editor'; import { registerCoreBlocks } from '@wordpress/block-library'; // Keep the matrix popover above the block toolbar, as in the editor. -// eslint-disable-next-line @wordpress/no-non-module-stylesheet-imports -- Browser fixtures need the styles WordPress normally enqueues. import '../../../../block-editor/src/components/block-popover/style.scss'; -// eslint-disable-next-line @wordpress/no-non-module-stylesheet-imports -- Browser fixtures need the styles WordPress normally enqueues. import '../../../../components/src/popover/style.scss'; const defaultSettings = { __experimentalFeatures: { diff --git a/packages/components/src/card/test/index.browser.test.tsx b/packages/components/src/card/test/index.browser.test.tsx index c1870a27f1090b..883b8c10cd1f13 100644 --- a/packages/components/src/card/test/index.browser.test.tsx +++ b/packages/components/src/card/test/index.browser.test.tsx @@ -2,7 +2,6 @@ import { describe, expect, it } from 'vitest'; import { screen } from '@testing-library/react'; import { render } from 'vitest-browser-react'; // Load the tokens that the production build also supplies as fallbacks. -// eslint-disable-next-line @wordpress/no-non-module-stylesheet-imports import '../../../../theme/prebuilt/css/design-tokens.css'; import { Card, diff --git a/packages/components/src/confirm-dialog/test/index.browser.test.tsx b/packages/components/src/confirm-dialog/test/index.browser.test.tsx index 67fea0d31c7dbd..f56d38207be09e 100644 --- a/packages/components/src/confirm-dialog/test/index.browser.test.tsx +++ b/packages/components/src/confirm-dialog/test/index.browser.test.tsx @@ -6,7 +6,6 @@ import Modal from '../../modal'; import { ConfirmDialog } from '..'; // The z-index contract spans ConfirmDialog's CSS Module and Modal's global // overlay styles, so load the same global stylesheet WordPress enqueues. -// eslint-disable-next-line @wordpress/no-non-module-stylesheet-imports import '../../modal/style.scss'; const noop = () => {}; diff --git a/packages/components/src/custom-gradient-picker/test/index.browser.test.tsx b/packages/components/src/custom-gradient-picker/test/index.browser.test.tsx index 04a14ecd9a74a8..3a39fcc5489177 100644 --- a/packages/components/src/custom-gradient-picker/test/index.browser.test.tsx +++ b/packages/components/src/custom-gradient-picker/test/index.browser.test.tsx @@ -3,13 +3,9 @@ import { userEvent } from 'vitest/browser'; import { screen, waitFor } from '@testing-library/react'; import { render } from 'vitest-browser-react'; import { useState } from '@wordpress/element'; -// eslint-disable-next-line @wordpress/no-non-module-stylesheet-imports import '../style.scss'; -// eslint-disable-next-line @wordpress/no-non-module-stylesheet-imports import '../../button/style.scss'; -// eslint-disable-next-line @wordpress/no-non-module-stylesheet-imports import '../../dropdown/style.scss'; -// eslint-disable-next-line @wordpress/no-non-module-stylesheet-imports import '../../popover/style.scss'; import CustomGradientPicker from '../'; import CustomGradientBar from '../gradient-bar'; diff --git a/packages/components/src/menu/test/scroll-lock.browser.test.tsx b/packages/components/src/menu/test/scroll-lock.browser.test.tsx index 1e211224af0a8a..22eacaf7f05aab 100644 --- a/packages/components/src/menu/test/scroll-lock.browser.test.tsx +++ b/packages/components/src/menu/test/scroll-lock.browser.test.tsx @@ -5,7 +5,6 @@ import { useState } from '@wordpress/element'; import { Menu } from '..'; import Modal from '../../modal'; // Load the body scroll-lock rule that WordPress enqueues for Modal. -// eslint-disable-next-line @wordpress/no-non-module-stylesheet-imports import '../../modal/style.scss'; const MenuWithModal = () => { diff --git a/packages/components/src/palette-edit/test/spacing.browser.test.tsx b/packages/components/src/palette-edit/test/spacing.browser.test.tsx index 8c090cc1025425..11b1e009e8df32 100644 --- a/packages/components/src/palette-edit/test/spacing.browser.test.tsx +++ b/packages/components/src/palette-edit/test/spacing.browser.test.tsx @@ -3,9 +3,7 @@ import { userEvent } from 'vitest/browser'; import { screen } from '@testing-library/react'; import { render } from 'vitest-browser-react'; // Load the same tokens and button styles used by consumers. -// eslint-disable-next-line @wordpress/no-non-module-stylesheet-imports import '../../../../theme/prebuilt/css/design-tokens.css'; -// eslint-disable-next-line @wordpress/no-non-module-stylesheet-imports import '../../button/style.scss'; import PaletteEdit from '..'; diff --git a/packages/components/src/tools-panel/test/index.browser.test.tsx b/packages/components/src/tools-panel/test/index.browser.test.tsx index 55beeff944d9b0..b0f4f6ab36c439 100644 --- a/packages/components/src/tools-panel/test/index.browser.test.tsx +++ b/packages/components/src/tools-panel/test/index.browser.test.tsx @@ -4,7 +4,6 @@ import { render } from 'vitest-browser-react'; import { ContextSystemProvider } from '../../context'; import { ToolsPanel, ToolsPanelItem } from '..'; // The cascade contract includes Button's global icon-button defaults. -// eslint-disable-next-line @wordpress/no-non-module-stylesheet-imports import '../../button/style.scss'; const defaultProps = { diff --git a/packages/media-editor/src/image-editor/react/components/test/cropper.browser.test.tsx b/packages/media-editor/src/image-editor/react/components/test/cropper.browser.test.tsx index 68abd699a4491f..b7e81a8062bf5f 100644 --- a/packages/media-editor/src/image-editor/react/components/test/cropper.browser.test.tsx +++ b/packages/media-editor/src/image-editor/react/components/test/cropper.browser.test.tsx @@ -7,7 +7,6 @@ import type { CropperController } from '../../hooks/use-cropper-reducer'; import { DEFAULT_STATE } from '../../../core/constants'; import { getSourceRegion } from '../../../core/source-region'; // Browser Mode needs the package's real styles for layout and transitions. -// eslint-disable-next-line @wordpress/no-non-module-stylesheet-imports import '../../../style.scss'; const GRID_TEST_ID = 'cropper-grid'; diff --git a/packages/media-editor/src/state/test/use-media-editor-state.browser.test.tsx b/packages/media-editor/src/state/test/use-media-editor-state.browser.test.tsx index 8bf4b336246e65..37972266b607e0 100644 --- a/packages/media-editor/src/state/test/use-media-editor-state.browser.test.tsx +++ b/packages/media-editor/src/state/test/use-media-editor-state.browser.test.tsx @@ -9,7 +9,6 @@ import { type MediaEditorSession, } from '../use-media-editor-state'; // Browser Mode needs the package's real styles for layout and transitions. -// eslint-disable-next-line @wordpress/no-non-module-stylesheet-imports import '../../image-editor/style.scss'; // Shown below 1:1 in the fixture, so pixel snapping stays off until a crop diff --git a/packages/theme/src/test/theme-provider.browser.test.tsx b/packages/theme/src/test/theme-provider.browser.test.tsx index e722e011a41ef4..9e0258931560fc 100644 --- a/packages/theme/src/test/theme-provider.browser.test.tsx +++ b/packages/theme/src/test/theme-provider.browser.test.tsx @@ -6,7 +6,6 @@ import { ThemeProvider } from '../theme-provider'; import type { ThemeProviderColorWarning } from '../theme-provider-color-warnings'; import styles from '../style.module.css'; // Browser Mode verifies the generated design-token stylesheet itself. -// eslint-disable-next-line @wordpress/no-non-module-stylesheet-imports import '../../prebuilt/css/design-tokens.css'; // The "strong" brand background resolves to the `color.primary` seed itself, and diff --git a/packages/ui/src/progress/test/progress.browser.test.tsx b/packages/ui/src/progress/test/progress.browser.test.tsx index 3875f338fcac5c..f40a7e3fd50942 100644 --- a/packages/ui/src/progress/test/progress.browser.test.tsx +++ b/packages/ui/src/progress/test/progress.browser.test.tsx @@ -2,7 +2,6 @@ import { describe, expect, it } from 'vitest'; import { screen } from '@testing-library/react'; import { render } from 'vitest-browser-react'; // Browser Mode loads source CSS without the build's token fallbacks. -// eslint-disable-next-line @wordpress/no-non-module-stylesheet-imports import '@wordpress/theme/design-tokens.css'; import * as Progress from '../index'; diff --git a/tools/eslint/config.mjs b/tools/eslint/config.mjs index c341d3223eb194..b2c8953a032f8e 100644 --- a/tools/eslint/config.mjs +++ b/tools/eslint/config.mjs @@ -549,6 +549,8 @@ export default dedupePlugins( [ `routes/**/*.${ SCRIPT_EXT }`, `widgets/**/*.${ SCRIPT_EXT }`, ], + // Tests load styles directly instead of using WordPress's enqueue path. + ignores: vitestLintFiles, rules: { '@wordpress/no-non-module-stylesheet-imports': 'error', }, diff --git a/tools/eslint/validate-test-config.mjs b/tools/eslint/validate-test-config.mjs index a0e3eea2e40f70..0942693b6f3b6a 100644 --- a/tools/eslint/validate-test-config.mjs +++ b/tools/eslint/validate-test-config.mjs @@ -11,6 +11,45 @@ const browserTest = 'packages/components/src/button/test/index.browser.test.tsx'; const sharedHelper = 'packages/block-serialization-spec-parser/shared-tests.js'; +// Tests load styles directly; production and Storybook keep their import rules. +for ( const [ file, expectedRule ] of [ + [ nodeTest, undefined ], + [ jsdomTest, undefined ], + [ browserTest, undefined ], + [ 'packages/components/src/menu/test/utils.ts', undefined ], + [ 'packages/components/src/menu/__tests__/styles.ts', undefined ], + [ + 'packages/components/src/menu/index.tsx', + '@wordpress/no-non-module-stylesheet-imports', + ], + [ + 'routes/post-list/stage.tsx', + '@wordpress/no-non-module-stylesheet-imports', + ], + [ + 'widgets/hello-dolly/render.tsx', + '@wordpress/no-non-module-stylesheet-imports', + ], + [ + 'packages/components/src/menu/stories/index.story.tsx', + 'gutenberg-storybook/no-non-module-stylesheet-imports', + ], +] ) { + const [ result ] = await eslint.lintText( "import './style.scss';", { + filePath: resolve( rootDir, file ), + } ); + assert.equal( result.fatalErrorCount, 0 ); + assert.deepEqual( + result.messages + .filter( ( { ruleId } ) => + ruleId?.endsWith( '/no-non-module-stylesheet-imports' ) + ) + .map( ( { ruleId } ) => ruleId ), + expectedRule ? [ expectedRule ] : [], + `${ file }: non-module stylesheet imports` + ); +} + // Check the complete repository config, including file discovery and overrides. for ( const file of [ nodeTest, jsdomTest, browserTest, sharedHelper ] ) { const config = await eslint.calculateConfigForFile( file );