Skip to content

Commit 851b02f

Browse files
committed
test: add inline workspace test
1 parent 6795add commit 851b02f

File tree

4 files changed

+73
-5
lines changed

4 files changed

+73
-5
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,20 @@ export async function resolveWorkspace(
5555
const concurrent = limitConcurrency(os.availableParallelism?.() || os.cpus().length || 5)
5656

5757
projectConfigs.forEach((options, index) => {
58-
const parentConfigPath = workspaceConfigPath || vitest.server.config.configFile
59-
const configDir = parentConfigPath ? dirname(parentConfigPath) : vitest.config.root
58+
const configRoot = workspaceConfigPath ? dirname(workspaceConfigPath) : vitest.config.root
6059
// if extends a config file, resolve the file path
61-
const configFile = typeof options.extends === 'string' && typeof parentConfigPath === 'string'
62-
? resolve(configDir, options.extends)
60+
const configFile = typeof options.extends === 'string'
61+
? resolve(configRoot, options.extends)
6362
: false
6463
// if extends a root config, use the users root options
6564
const rootOptions = options.extends === true
6665
? vitest._options
6766
: {}
6867
// if `root` is configured, resolve it relative to the workespace file or vite root (like other options)
6968
// if `root` is not specified, inline configs use the same root as the root project
70-
const root = options.root ? resolve(configDir) : vitest.config.root
69+
const root = options.root
70+
? resolve(configRoot, options.root)
71+
: vitest.config.root
7172
projectPromises.push(concurrent(() => initializeProject(
7273
index,
7374
vitest,
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { expect, it } from 'vitest';
2+
3+
it('correctly inherits values', ({ task }) => {
4+
const project = task.file.projectName
5+
switch (project) {
6+
case 'project-1': {
7+
expect(process.env.TEST_ROOT).toBe('1')
8+
return
9+
}
10+
case 'project-2': {
11+
expect(process.env.TEST_ROOT).toBe('2')
12+
return
13+
}
14+
case 'project-3': {
15+
// even if not inherited from the config directly, the `env` is always inherited from root
16+
expect(process.env.TEST_ROOT).toBe('1')
17+
expect(process.env.TEST_PROJECT).toBe('project-3')
18+
return
19+
}
20+
default: {
21+
expect.unreachable()
22+
}
23+
}
24+
})
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default {
2+
test: {
3+
env: {
4+
TEST_PROJECT: 'project-3',
5+
},
6+
},
7+
}

test/config/test/workspace.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,39 @@ it('vite import analysis is applied when loading workspace config', async () =>
9090
expect(stderr).toBe('')
9191
expect(stdout).toContain('test - a')
9292
})
93+
94+
it('can define inline workspace config programmatically', async () => {
95+
const { stderr, stdout } = await runVitest({
96+
root: 'fixtures/workspace/api',
97+
env: {
98+
TEST_ROOT: '1',
99+
},
100+
workspace: [
101+
{
102+
extends: true,
103+
test: {
104+
name: 'project-1',
105+
},
106+
},
107+
{
108+
test: {
109+
name: 'project-2',
110+
env: {
111+
TEST_ROOT: '2',
112+
},
113+
},
114+
},
115+
{
116+
extends: './vite.custom.config.js',
117+
test: {
118+
name: 'project-3',
119+
},
120+
},
121+
],
122+
})
123+
expect(stderr).toBe('')
124+
expect(stdout).toContain('project-1')
125+
expect(stdout).toContain('project-2')
126+
expect(stdout).toContain('project-3')
127+
expect(stdout).toContain('3 passed')
128+
})

0 commit comments

Comments
 (0)