Skip to content

Commit 8877e22

Browse files
authoredJan 17, 2024
fix(vitest): use development/production conditions when resolving external modules (#4980)
1 parent 7d9f673 commit 8877e22

File tree

14 files changed

+123
-3
lines changed

14 files changed

+123
-3
lines changed
 

‎packages/vitest/src/node/pool.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,16 @@ export function createPool(ctx: Vitest): ProcessPool {
6060
return getDefaultPoolName(project, file)
6161
}
6262

63-
const conditions = ctx.server.config.resolve.conditions?.flatMap(c => ['--conditions', c]) || []
63+
// in addition to resolve.conditions Vite also adds production/development,
64+
// see: https://github.com/vitejs/vite/blob/af2aa09575229462635b7cbb6d248ca853057ba2/packages/vite/src/node/plugins/resolve.ts#L1056-L1080
65+
const potentialConditions = new Set(['production', 'development', ...ctx.server.config.resolve.conditions])
66+
const conditions = [...potentialConditions].filter((condition) => {
67+
if (condition === 'production')
68+
return ctx.server.config.isProduction
69+
if (condition === 'development')
70+
return !ctx.server.config.isProduction
71+
return true
72+
}).flatMap(c => ['--conditions', c])
6473

6574
// Instead of passing whole process.execArgv to the workers, pick allowed options.
6675
// Some options may crash worker, e.g. --prof, --title. nodejs/node#41103

‎pnpm-lock.yaml

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎pnpm-workspace.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ packages:
33
- packages/*
44
- examples/*
55
- test/*
6+
- test/config/fixtures/conditions-pkg
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default as condition } from 'subpackage'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "conditions",
3+
"private": true,
4+
"type": "module",
5+
"main": "index.js",
6+
"dependencies": {
7+
"subpackage": "file:../conditions-subpackage"
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'custom'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
throw new Error('Should not be imported')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'development'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "subpackage",
3+
"private": true,
4+
"type": "module",
5+
"exports": {
6+
".": {
7+
"custom": "./custom.js",
8+
"development": "./development.js",
9+
"production": "./production.js",
10+
"default": "./default.js"
11+
}
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'production'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { test, expect } from 'vitest';
2+
import { condition } from '../conditions-pkg';
3+
4+
test('condition is correct', () => {
5+
expect(condition).toBe(TEST_CONDITION)
6+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { defineConfig } from 'vitest/config'
2+
3+
export default defineConfig({})
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { expect, test } from 'vitest'
2+
import { runVitest } from '../../test-utils'
3+
4+
test('correctly imports external dependencies with a development condition', async () => {
5+
// dev condition is the default
6+
const { stderr } = await runVitest({
7+
root: 'fixtures/conditions-test',
8+
server: {
9+
deps: {
10+
external: [/conditions-pkg/],
11+
},
12+
},
13+
}, [], 'test', {
14+
define: {
15+
TEST_CONDITION: '"development"',
16+
},
17+
})
18+
19+
expect(stderr).toBe('')
20+
})
21+
22+
test('correctly imports external dependencies with a production condition', async () => {
23+
// this is the only check in Vite for "isProduction" value
24+
process.env.NODE_ENV = 'production'
25+
26+
const { stderr } = await runVitest({
27+
root: 'fixtures/conditions-test',
28+
server: {
29+
deps: {
30+
external: [/conditions-pkg/],
31+
},
32+
},
33+
}, [], 'test', {
34+
define: {
35+
TEST_CONDITION: '"production"',
36+
},
37+
})
38+
39+
expect(stderr).toBe('')
40+
})
41+
42+
test('correctly imports external dependencies with a custom condition', async () => {
43+
delete process.env.NODE_ENV
44+
45+
const { stderr } = await runVitest({
46+
root: 'fixtures/conditions-test',
47+
server: {
48+
deps: {
49+
external: [/conditions-pkg/],
50+
},
51+
},
52+
}, [], 'test', {
53+
resolve: {
54+
conditions: ['custom'],
55+
},
56+
define: {
57+
TEST_CONDITION: '"custom"',
58+
},
59+
})
60+
61+
expect(stderr).toBe('')
62+
})

‎test/test-utils/index.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ import { Console } from 'node:console'
22
import { Writable } from 'node:stream'
33
import fs from 'node:fs'
44
import { fileURLToPath } from 'node:url'
5+
import type { UserConfig as ViteUserConfig } from 'vite'
56
import { type UserConfig, type VitestRunMode, afterEach } from 'vitest'
67
import type { Vitest } from 'vitest/node'
78
import { startVitest } from 'vitest/node'
89
import { type Options, execa } from 'execa'
910
import stripAnsi from 'strip-ansi'
1011
import { dirname, resolve } from 'pathe'
1112

12-
export async function runVitest(config: UserConfig, cliFilters: string[] = [], mode: VitestRunMode = 'test') {
13+
export async function runVitest(config: UserConfig, cliFilters: string[] = [], mode: VitestRunMode = 'test', viteOverrides: ViteUserConfig = {}) {
1314
// Reset possible previous runs
1415
process.exitCode = 0
1516
let exitCode = process.exitCode
@@ -26,7 +27,7 @@ export async function runVitest(config: UserConfig, cliFilters: string[] = [], m
2627
watch: false,
2728
reporters: ['verbose'],
2829
...config,
29-
})
30+
}, viteOverrides)
3031
}
3132
catch (e: any) {
3233
return {

0 commit comments

Comments
 (0)
Please sign in to comment.