Skip to content

Commit

Permalink
fix(vitest): use development/production conditions when resolving ext…
Browse files Browse the repository at this point in the history
…ernal modules (#4980)
  • Loading branch information
sheremet-va authored Jan 17, 2024
1 parent 7d9f673 commit 8877e22
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 3 deletions.
11 changes: 10 additions & 1 deletion packages/vitest/src/node/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,16 @@ export function createPool(ctx: Vitest): ProcessPool {
return getDefaultPoolName(project, file)
}

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

// Instead of passing whole process.execArgv to the workers, pick allowed options.
// Some options may crash worker, e.g. --prof, --title. nodejs/node#41103
Expand Down
11 changes: 11 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ packages:
- packages/*
- examples/*
- test/*
- test/config/fixtures/conditions-pkg
1 change: 1 addition & 0 deletions test/config/fixtures/conditions-pkg/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as condition } from 'subpackage'
9 changes: 9 additions & 0 deletions test/config/fixtures/conditions-pkg/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "conditions",
"private": true,
"type": "module",
"main": "index.js",
"dependencies": {
"subpackage": "file:../conditions-subpackage"
}
}
1 change: 1 addition & 0 deletions test/config/fixtures/conditions-subpackage/custom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'custom'
1 change: 1 addition & 0 deletions test/config/fixtures/conditions-subpackage/default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
throw new Error('Should not be imported')
1 change: 1 addition & 0 deletions test/config/fixtures/conditions-subpackage/development.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'development'
13 changes: 13 additions & 0 deletions test/config/fixtures/conditions-subpackage/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "subpackage",
"private": true,
"type": "module",
"exports": {
".": {
"custom": "./custom.js",
"development": "./development.js",
"production": "./production.js",
"default": "./default.js"
}
}
}
1 change: 1 addition & 0 deletions test/config/fixtures/conditions-subpackage/production.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'production'
6 changes: 6 additions & 0 deletions test/config/fixtures/conditions-test/conditions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { test, expect } from 'vitest';
import { condition } from '../conditions-pkg';

test('condition is correct', () => {
expect(condition).toBe(TEST_CONDITION)
})
3 changes: 3 additions & 0 deletions test/config/fixtures/conditions-test/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({})
62 changes: 62 additions & 0 deletions test/config/test/conditions-cli.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { expect, test } from 'vitest'
import { runVitest } from '../../test-utils'

test('correctly imports external dependencies with a development condition', async () => {
// dev condition is the default
const { stderr } = await runVitest({
root: 'fixtures/conditions-test',
server: {
deps: {
external: [/conditions-pkg/],
},
},
}, [], 'test', {
define: {
TEST_CONDITION: '"development"',
},
})

expect(stderr).toBe('')
})

test('correctly imports external dependencies with a production condition', async () => {
// this is the only check in Vite for "isProduction" value
process.env.NODE_ENV = 'production'

const { stderr } = await runVitest({
root: 'fixtures/conditions-test',
server: {
deps: {
external: [/conditions-pkg/],
},
},
}, [], 'test', {
define: {
TEST_CONDITION: '"production"',
},
})

expect(stderr).toBe('')
})

test('correctly imports external dependencies with a custom condition', async () => {
delete process.env.NODE_ENV

const { stderr } = await runVitest({
root: 'fixtures/conditions-test',
server: {
deps: {
external: [/conditions-pkg/],
},
},
}, [], 'test', {
resolve: {
conditions: ['custom'],
},
define: {
TEST_CONDITION: '"custom"',
},
})

expect(stderr).toBe('')
})
5 changes: 3 additions & 2 deletions test/test-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import { Console } from 'node:console'
import { Writable } from 'node:stream'
import fs from 'node:fs'
import { fileURLToPath } from 'node:url'
import type { UserConfig as ViteUserConfig } from 'vite'
import { type UserConfig, type VitestRunMode, afterEach } from 'vitest'
import type { Vitest } from 'vitest/node'
import { startVitest } from 'vitest/node'
import { type Options, execa } from 'execa'
import stripAnsi from 'strip-ansi'
import { dirname, resolve } from 'pathe'

export async function runVitest(config: UserConfig, cliFilters: string[] = [], mode: VitestRunMode = 'test') {
export async function runVitest(config: UserConfig, cliFilters: string[] = [], mode: VitestRunMode = 'test', viteOverrides: ViteUserConfig = {}) {
// Reset possible previous runs
process.exitCode = 0
let exitCode = process.exitCode
Expand All @@ -26,7 +27,7 @@ export async function runVitest(config: UserConfig, cliFilters: string[] = [], m
watch: false,
reporters: ['verbose'],
...config,
})
}, viteOverrides)
}
catch (e: any) {
return {
Expand Down

0 comments on commit 8877e22

Please sign in to comment.