Skip to content

Commit

Permalink
fix(vitest): inject "define" in workspaces (#4096)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored Sep 18, 2023
1 parent f2ba160 commit b4d5ae2
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
7 changes: 4 additions & 3 deletions packages/vitest/src/node/plugins/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { configDefaults } from '../../defaults'
import { generateScopedClassName } from '../../integrations/css/css-modules'
import { deepMerge } from '../../utils/base'
import type { WorkspaceProject } from '../workspace'
import type { UserWorkspaceConfig } from '../../types'
import type { ResolvedConfig, UserWorkspaceConfig } from '../../types'
import { CoverageTransform } from './coverageTransform'
import { CSSEnablerPlugin } from './cssEnabler'
import { SsrReplacerPlugin } from './ssrReplacer'
Expand All @@ -28,7 +28,7 @@ export function WorkspaceVitestPlugin(project: WorkspaceProject, options: Worksp
this.meta.watchMode = false
},
config(viteConfig) {
const env: Record<string, any> = deleteDefineConfig(viteConfig)
const defines: Record<string, any> = deleteDefineConfig(viteConfig)

const testConfig = viteConfig.test || {}

Expand Down Expand Up @@ -79,11 +79,12 @@ export function WorkspaceVitestPlugin(project: WorkspaceProject, options: Worksp
},
},
test: {
env,
name,
},
}

;(config.test as ResolvedConfig).defines = defines

const classNameStrategy = (typeof testConfig.css !== 'boolean' && testConfig.css?.modules?.classNameStrategy) || 'stable'

if (classNameStrategy !== 'scoped') {
Expand Down
6 changes: 3 additions & 3 deletions test/workspaces/globalTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ export async function teardown() {

try {
assert.ok(results.success)
assert.equal(results.numTotalTestSuites, 6)
assert.equal(results.numTotalTests, 7)
assert.equal(results.numPassedTests, 7)
assert.equal(results.numTotalTestSuites, 7)
assert.equal(results.numTotalTests, 8)
assert.equal(results.numPassedTests, 8)

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

Expand Down
9 changes: 9 additions & 0 deletions test/workspaces/space_1/test/env-injected.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { expect, test } from 'vitest'

declare global {
const __DEV__: boolean
}

test('dev is injected', () => {
expect(__DEV__).toBe(true)
})
3 changes: 3 additions & 0 deletions test/workspaces/space_1/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { defineProject } from 'vitest/config'

export default defineProject({
define: {
__DEV__: 'true',
},
test: {
name: 'space_1',
environment: 'happy-dom',
Expand Down

0 comments on commit b4d5ae2

Please sign in to comment.