Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(vitest): inject "define" in workspaces #4096

Merged
merged 2 commits into from Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/vitest/src/node/plugins/workspace.ts
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
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
@@ -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
@@ -1,6 +1,9 @@
import { defineProject } from 'vitest/config'

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