File tree Expand file tree Collapse file tree 7 files changed +78
-0
lines changed
Expand file tree Collapse file tree 7 files changed +78
-0
lines changed Original file line number Diff line number Diff line change @@ -49,6 +49,7 @@ export const createBrowserServer: BrowserServerFactory = async (options) => {
4949 let cacheDir : string
5050 const vite = await createViteServer ( {
5151 ...project . options , // spread project config inlined in root workspace config
52+ define : project . config . viteDefine ,
5253 base : '/' ,
5354 root : project . config . root ,
5455 logLevel,
Original file line number Diff line number Diff line change @@ -61,9 +61,11 @@ export async function VitestPlugin(
6161
6262 // store defines for globalThis to make them
6363 // reassignable when running in worker in src/runtime/setup.ts
64+ const originalDefine = { ...viteConfig . define } // stash original defines for browser mode
6465 const defines : Record < string , any > = deleteDefineConfig ( viteConfig )
6566
6667 ; ( options as unknown as ResolvedConfig ) . defines = defines
68+ ; ( options as unknown as ResolvedConfig ) . viteDefine = originalDefine
6769
6870 let open : string | boolean | undefined = false
6971
Original file line number Diff line number Diff line change @@ -125,6 +125,7 @@ export function WorkspaceVitestPlugin(
125125 this . meta . watchMode = false
126126 } ,
127127 config ( viteConfig ) {
128+ const originalDefine = { ...viteConfig . define } // stash original defines for browser mode
128129 const defines : Record < string , any > = deleteDefineConfig ( viteConfig )
129130
130131 const testConfig = viteConfig . test || { }
@@ -197,6 +198,7 @@ export function WorkspaceVitestPlugin(
197198 }
198199
199200 ; ( config . test as ResolvedConfig ) . defines = defines
201+ ; ( config . test as ResolvedConfig ) . viteDefine = originalDefine
200202
201203 const classNameStrategy
202204 = ( typeof testConfig . css !== 'boolean'
Original file line number Diff line number Diff line change @@ -1049,6 +1049,7 @@ export interface ResolvedConfig
10491049 reporters : ( InlineReporter | ReporterWithOptions ) [ ]
10501050
10511051 defines : Record < string , any >
1052+ viteDefine : Record < string , any >
10521053
10531054 api : ApiConfig & { token : string }
10541055 cliExclude ?: string [ ]
Original file line number Diff line number Diff line change 1+ import { test , expect } from 'vitest'
2+
3+ test ( 'passes' , ( ) => {
4+ expect ( process . env . TEST_PROCESS_ENV ) . toBe ( 'PROCESS_OK' )
5+ expect ( import . meta. env . TEST_META_ENV ) . toBe ( 'META_OK' )
6+ } )
Original file line number Diff line number Diff line change 1+ import { playwright } from '@vitest/browser-playwright'
2+ import { defineConfig } from 'vitest/config'
3+
4+ let config = defineConfig ( {
5+ define : {
6+ 'process.env.TEST_PROCESS_ENV' : JSON . stringify ( 'PROCESS_OK' ) ,
7+ 'import.meta.env.TEST_META_ENV' : JSON . stringify ( 'META_OK' ) ,
8+ } ,
9+ test : {
10+ browser : {
11+ enabled : true ,
12+ provider : playwright ( ) ,
13+ instances : [
14+ { browser : 'chromium' } ,
15+ ] ,
16+ } ,
17+ } ,
18+ } )
19+
20+ if ( process . env . BROWSER_DEFINE_TEST_PROEJCT === "true" ) {
21+ config = defineConfig ( {
22+ test : {
23+ projects : [ config ] ,
24+ } ,
25+ } )
26+ }
27+
28+ export default config
Original file line number Diff line number Diff line change 1+ import { expect , test } from 'vitest'
2+ import { runVitest } from '../../test-utils'
3+
4+ test ( 'browser root' , async ( ) => {
5+ process . env . BROWSER_DEFINE_TEST_PROEJCT = 'false'
6+ const { testTree, stderr } = await runVitest ( {
7+ root : './fixtures/browser-define' ,
8+ browser : {
9+ headless : true ,
10+ } ,
11+ } )
12+ expect ( stderr ) . toBe ( '' )
13+ expect ( testTree ( ) ) . toMatchInlineSnapshot ( `
14+ {
15+ "basic.test.ts": {
16+ "passes": "passed",
17+ },
18+ }
19+ ` )
20+ } )
21+
22+ test ( 'browser proejct' , async ( ) => {
23+ process . env . BROWSER_DEFINE_TEST_PROEJCT = 'true'
24+ const { testTree, stderr } = await runVitest ( {
25+ root : './fixtures/browser-define' ,
26+ browser : {
27+ headless : true ,
28+ } ,
29+ } )
30+ expect ( stderr ) . toBe ( '' )
31+ expect ( testTree ( ) ) . toMatchInlineSnapshot ( `
32+ {
33+ "basic.test.ts": {
34+ "passes": "passed",
35+ },
36+ }
37+ ` )
38+ } )
You can’t perform that action at this time.
0 commit comments