import { createRequire } from 'node:module'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; import { globSync } from 'glob'; import { defineConfig } from 'vitest/config'; import { createVitePlugins } from './config/vite-plugins.mjs'; import { createPlaywrightProvider } from './config/playwright-provider.mjs'; import { createBrowserTraceArtifacts } from './config/browser-traces.mjs'; import { discoverTestFiles, getVitestTestsByProject, } from './scripts/discover-test-files.mjs'; const CONFIG_DIR = path.dirname( fileURLToPath( import.meta.url ) ); const ROOT_DIR = path.resolve( CONFIG_DIR, '../..' ); const nodeRequire = createRequire( import.meta.url ); const gutenbergEnvSetupFile = path.join( ROOT_DIR, 'test/unit/config/gutenberg-env.js' ); const isolationSetupFile = path.join( ROOT_DIR, 'test/unit/config/isolation.vitest.js' ); const vitestTests = getVitestTestsByProject( discoverTestFiles( ROOT_DIR ) ); const styleMockAlias = { find: /^.*\.(?:css|scss)$/, replacement: path.join( ROOT_DIR, 'test/unit/config/style-mock.vitest.js' ), }; const reporters = [ 'default' ]; const browserTraces = process.env.WP_VITEST_BROWSER_TRACE === '1' ? createBrowserTraceArtifacts( ROOT_DIR ) : undefined; if ( browserTraces ) { reporters.push( browserTraces.reporter ); } if ( process.env.GITHUB_ACTIONS === 'true' ) { reporters.push( 'github-actions' ); } if ( process.env.CI && process.env.GITHUB_REPOSITORY === 'WordPress/gutenberg' ) { reporters.push( [ /* * Resolve to an absolute path so Vitest can load the reporter regardless * of hoisting layout. */ nodeRequire.resolve( '@flakiness/vitest' ), { duplicates: 'rename', flakinessProject: 'WordPress/gutenberg', }, ] ); } // Preserve repository-root configuration discovery and default to UTC while // allowing the date-test matrix to supply another timezone. process.chdir( ROOT_DIR ); process.env.TZ ||= 'UTC'; const transpiledPackageNames = globSync( 'packages/*/src/index.{js,jsx,mjs,cjs,ts,tsx,mts,cts}', { cwd: ROOT_DIR, absolute: true } ) .sort() .map( ( fileName ) => { const relative = path.relative( ROOT_DIR, fileName ); return relative.split( path.sep )[ 1 ]; } ); export default defineConfig( { root: ROOT_DIR, oxc: { jsx: { runtime: 'automatic', }, }, plugins: await createVitePlugins( ROOT_DIR ), resolve: { alias: [ { find: /^yargs$/, replacement: path.join( ROOT_DIR, 'test/unit/config/yargs.vitest.js' ), }, { find: '@wordpress/vips/worker', replacement: path.join( ROOT_DIR, 'test/unit/config/vips-worker-code-stub.vitest.js' ), }, { find: '@wordpress/video-conversion/worker', replacement: path.join( ROOT_DIR, 'test/unit/config/video-conversion-worker-code-stub.vitest.js' ), }, { find: /^@wordpress\/([^/]+)\/src\/(.+)$/, replacement: path.join( ROOT_DIR, 'packages/$1/src/$2' ), }, { find: new RegExp( `^@wordpress/(${ transpiledPackageNames.join( '|' ) })$` ), replacement: path.join( ROOT_DIR, 'packages/$1/src' ), }, { find: '@wordpress/theme/design-tokens.js', replacement: path.join( ROOT_DIR, 'packages/theme/prebuilt/js/design-tokens.mjs' ), }, { find: /^@wordpress\/block-library\/build-module\/(.*)\.mjs$/, replacement: path.join( ROOT_DIR, 'packages/block-library/src/$1' ), }, { find: /.+\.wasm$/, replacement: path.join( ROOT_DIR, 'test/unit/config/wasm-stub.js' ), }, ], }, test: { // Vitest only reads attachmentsDir from the root configuration. attachmentsDir: path.join( ROOT_DIR, 'test-results/vitest-attachments' ), projects: [ { extends: true, resolve: { alias: [ styleMockAlias ], }, test: { name: 'node', environment: 'node', pool: 'threads', include: vitestTests.node, setupFiles: [ gutenbergEnvSetupFile, path.join( ROOT_DIR, 'test/unit/config/console.vitest.js' ), isolationSetupFile, ], }, }, { extends: true, resolve: { alias: [ styleMockAlias ], }, test: { name: 'jsdom', environment: 'jsdom', pool: 'threads', environmentOptions: { jsdom: { url: 'http://localhost/', }, }, include: vitestTests.jsdom, setupFiles: [ path.join( ROOT_DIR, 'test/unit/config/setup-globals.vitest.js' ), path.join( ROOT_DIR, 'test/unit/config/global-mocks.vitest.js' ), gutenbergEnvSetupFile, path.join( ROOT_DIR, 'test/unit/config/console.vitest.js' ), path.join( ROOT_DIR, 'test/unit/config/testing-library.vitest.js' ), isolationSetupFile, ], }, }, { extends: true, /* * Browser mode pre-bundles the Vitest runtime, which Vite resolves * from the project root. Root the project where the test * dependencies are declared so resolution stays layout agnostic. */ root: CONFIG_DIR, optimizeDeps: { entries: vitestTests.browser.map( ( testPath ) => path.join( ROOT_DIR, testPath ) ), }, test: { name: 'browser', dir: ROOT_DIR, include: vitestTests.browser, setupFiles: [ ...( browserTraces ? [ path.join( CONFIG_DIR, 'config/browser-traces.vitest.js' ), ] : [] ), path.join( ROOT_DIR, 'test/unit/config/browser.vitest.js' ), path.join( ROOT_DIR, 'test/unit/config/gutenberg-env.js' ), path.join( ROOT_DIR, 'test/unit/config/console.vitest.js' ), isolationSetupFile, ], browser: { enabled: true, headless: true, instances: [ { browser: 'chromium' } ], provider: createPlaywrightProvider(), commands: { ...browserTraces?.commands, // Vitest advances fake timers during built-in hover commands. // Exact delay tests must retain control of the clock. async hoverWithManualTimers( { frame }, selector ) { const testFrame = await frame(); await testFrame.locator( selector ).hover(); }, }, screenshotDirectory: path.join( ROOT_DIR, 'test-results/vitest-browser-screenshots' ), screenshotFailures: true, }, }, }, ], // mockReset already clears every mock. Keep clearMocks disabled to make // that overlap explicit and avoid a redundant cleanup pass. clearMocks: false, globals: false, // Match native ES modules, which have no `__dirname`, `require`, or // other CommonJS variables. CommonJS files keep them. injectCjsGlobals: false, includeTaskLocation: true, isolate: true, mockReset: true, passWithNoTests: false, reporters, sequence: { hooks: 'list', setupFiles: 'list', }, snapshotFormat: { escapeString: false, printBasicPrototype: false, }, restoreMocks: true, unstubEnvs: true, unstubGlobals: true, }, } );