Skip to content

Commit b4d5ae2

Browse files
authored
fix(vitest): inject "define" in workspaces (#4096)
1 parent f2ba160 commit b4d5ae2

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

packages/vitest/src/node/plugins/workspace.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { configDefaults } from '../../defaults'
44
import { generateScopedClassName } from '../../integrations/css/css-modules'
55
import { deepMerge } from '../../utils/base'
66
import type { WorkspaceProject } from '../workspace'
7-
import type { UserWorkspaceConfig } from '../../types'
7+
import type { ResolvedConfig, UserWorkspaceConfig } from '../../types'
88
import { CoverageTransform } from './coverageTransform'
99
import { CSSEnablerPlugin } from './cssEnabler'
1010
import { SsrReplacerPlugin } from './ssrReplacer'
@@ -28,7 +28,7 @@ export function WorkspaceVitestPlugin(project: WorkspaceProject, options: Worksp
2828
this.meta.watchMode = false
2929
},
3030
config(viteConfig) {
31-
const env: Record<string, any> = deleteDefineConfig(viteConfig)
31+
const defines: Record<string, any> = deleteDefineConfig(viteConfig)
3232

3333
const testConfig = viteConfig.test || {}
3434

@@ -79,11 +79,12 @@ export function WorkspaceVitestPlugin(project: WorkspaceProject, options: Worksp
7979
},
8080
},
8181
test: {
82-
env,
8382
name,
8483
},
8584
}
8685

86+
;(config.test as ResolvedConfig).defines = defines
87+
8788
const classNameStrategy = (typeof testConfig.css !== 'boolean' && testConfig.css?.modules?.classNameStrategy) || 'stable'
8889

8990
if (classNameStrategy !== 'scoped') {

test/workspaces/globalTest.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ export async function teardown() {
66

77
try {
88
assert.ok(results.success)
9-
assert.equal(results.numTotalTestSuites, 6)
10-
assert.equal(results.numTotalTests, 7)
11-
assert.equal(results.numPassedTests, 7)
9+
assert.equal(results.numTotalTestSuites, 7)
10+
assert.equal(results.numTotalTests, 8)
11+
assert.equal(results.numPassedTests, 8)
1212

1313
const shared = results.testResults.filter((r: any) => r.name.includes('space_shared/test.spec.ts'))
1414

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { expect, test } from 'vitest'
2+
3+
declare global {
4+
const __DEV__: boolean
5+
}
6+
7+
test('dev is injected', () => {
8+
expect(__DEV__).toBe(true)
9+
})

test/workspaces/space_1/vite.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { defineProject } from 'vitest/config'
22

33
export default defineProject({
4+
define: {
5+
__DEV__: 'true',
6+
},
47
test: {
58
name: 'space_1',
69
environment: 'happy-dom',

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy