diff --git a/docs/config/index.md b/docs/config/index.md index f9321433d458..9755350a05bf 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -1640,6 +1640,14 @@ Run the browser in a `headless` mode. If you are running Vitest in CI, it will b Run every test in a separate iframe. +#### browser.testerHtmlPath + +- **Type:** `string` +- **Default:** `@vitest/browser/tester.html` +- **Version:** Since Vitest 2.1.4 + +A path to the HTML entry point. Can be relative to the root of the project. This file will be processed with [`transformIndexHtml`](https://vite.dev/guide/api-plugin#transformindexhtml) hook. + #### browser.api - **Type:** `number | { port?, strictPort?, host? }` diff --git a/packages/browser/rollup.config.js b/packages/browser/rollup.config.js index 46687ba7507c..7b00fc5bfa04 100644 --- a/packages/browser/rollup.config.js +++ b/packages/browser/rollup.config.js @@ -107,7 +107,7 @@ export default () => input: './src/client/tester/state.ts', output: { file: 'dist/state.js', - format: 'esm', + format: 'iife', }, plugins: [ esbuild({ diff --git a/packages/browser/src/client/public/esm-client-injector.js b/packages/browser/src/client/public/esm-client-injector.js index 27afb1bb9b06..fe4fef717e92 100644 --- a/packages/browser/src/client/public/esm-client-injector.js +++ b/packages/browser/src/client/public/esm-client-injector.js @@ -1,50 +1,52 @@ -const moduleCache = new Map(); - -function wrapModule(module) { - if (typeof module === "function") { - const promise = new Promise((resolve, reject) => { - if (typeof __vitest_mocker__ === "undefined") - return module().then(resolve, reject); - __vitest_mocker__.prepare().finally(() => { - module().then(resolve, reject); +(() => { + const moduleCache = new Map(); + + function wrapModule(module) { + if (typeof module === "function") { + const promise = new Promise((resolve, reject) => { + if (typeof __vitest_mocker__ === "undefined") + return module().then(resolve, reject); + __vitest_mocker__.prepare().finally(() => { + module().then(resolve, reject); + }); }); - }); - moduleCache.set(promise, { promise, evaluated: false }); - return promise.finally(() => moduleCache.delete(promise)); + moduleCache.set(promise, { promise, evaluated: false }); + return promise.finally(() => moduleCache.delete(promise)); + } + return module; } - return module; -} - -window.__vitest_browser_runner__ = { - wrapModule, - wrapDynamicImport: wrapModule, - moduleCache, - config: { __VITEST_CONFIG__ }, - viteConfig: { __VITEST_VITE_CONFIG__ }, - files: { __VITEST_FILES__ }, - type: { __VITEST_TYPE__ }, - contextId: { __VITEST_CONTEXT_ID__ }, - testerId: { __VITEST_TESTER_ID__ }, - provider: { __VITEST_PROVIDER__ }, - providedContext: { __VITEST_PROVIDED_CONTEXT__ }, -}; - -const config = __vitest_browser_runner__.config; - -if (config.testNamePattern) - config.testNamePattern = parseRegexp(config.testNamePattern); - -function parseRegexp(input) { - // Parse input - const m = input.match(/(\/?)(.+)\1([a-z]*)/i); - - // match nothing - if (!m) return /$^/; - - // Invalid flags - if (m[3] && !/^(?!.*?(.).*?\1)[gmixXsuUAJ]+$/.test(m[3])) - return RegExp(input); - - // Create the regular expression - return new RegExp(m[2], m[3]); -} + + window.__vitest_browser_runner__ = { + wrapModule, + wrapDynamicImport: wrapModule, + moduleCache, + config: { __VITEST_CONFIG__ }, + viteConfig: { __VITEST_VITE_CONFIG__ }, + files: { __VITEST_FILES__ }, + type: { __VITEST_TYPE__ }, + contextId: { __VITEST_CONTEXT_ID__ }, + testerId: { __VITEST_TESTER_ID__ }, + provider: { __VITEST_PROVIDER__ }, + providedContext: { __VITEST_PROVIDED_CONTEXT__ }, + }; + + const config = __vitest_browser_runner__.config; + + if (config.testNamePattern) + config.testNamePattern = parseRegexp(config.testNamePattern); + + function parseRegexp(input) { + // Parse input + const m = input.match(/(\/?)(.+)\1([a-z]*)/i); + + // match nothing + if (!m) return /$^/; + + // Invalid flags + if (m[3] && !/^(?!.*?(.).*?\1)[gmixXsuUAJ]+$/.test(m[3])) + return RegExp(input); + + // Create the regular expression + return new RegExp(m[2], m[3]); + } +})(); diff --git a/packages/browser/src/client/tester/tester.html b/packages/browser/src/client/tester/tester.html index 1bf9f3ff32f1..8586e2166f22 100644 --- a/packages/browser/src/client/tester/tester.html +++ b/packages/browser/src/client/tester/tester.html @@ -4,7 +4,7 @@ -