Skip to content

Commit

Permalink
test: add inline workspace test
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Nov 18, 2024
1 parent 6795add commit 851b02f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 5 deletions.
11 changes: 6 additions & 5 deletions packages/vitest/src/node/workspace/resolveWorkspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,20 @@ export async function resolveWorkspace(
const concurrent = limitConcurrency(os.availableParallelism?.() || os.cpus().length || 5)

projectConfigs.forEach((options, index) => {
const parentConfigPath = workspaceConfigPath || vitest.server.config.configFile
const configDir = parentConfigPath ? dirname(parentConfigPath) : vitest.config.root
const configRoot = workspaceConfigPath ? dirname(workspaceConfigPath) : vitest.config.root
// if extends a config file, resolve the file path
const configFile = typeof options.extends === 'string' && typeof parentConfigPath === 'string'
? resolve(configDir, options.extends)
const configFile = typeof options.extends === 'string'
? resolve(configRoot, options.extends)
: false
// if extends a root config, use the users root options
const rootOptions = options.extends === true
? vitest._options
: {}
// if `root` is configured, resolve it relative to the workespace file or vite root (like other options)
// if `root` is not specified, inline configs use the same root as the root project
const root = options.root ? resolve(configDir) : vitest.config.root
const root = options.root
? resolve(configRoot, options.root)
: vitest.config.root
projectPromises.push(concurrent(() => initializeProject(
index,
vitest,
Expand Down
24 changes: 24 additions & 0 deletions test/config/fixtures/workspace/api/basic.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { expect, it } from 'vitest';

it('correctly inherits values', ({ task }) => {
const project = task.file.projectName
switch (project) {
case 'project-1': {
expect(process.env.TEST_ROOT).toBe('1')
return
}
case 'project-2': {
expect(process.env.TEST_ROOT).toBe('2')
return
}
case 'project-3': {
// even if not inherited from the config directly, the `env` is always inherited from root
expect(process.env.TEST_ROOT).toBe('1')
expect(process.env.TEST_PROJECT).toBe('project-3')
return
}
default: {
expect.unreachable()
}
}
})
7 changes: 7 additions & 0 deletions test/config/fixtures/workspace/api/vite.custom.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
test: {
env: {
TEST_PROJECT: 'project-3',
},
},
}
36 changes: 36 additions & 0 deletions test/config/test/workspace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,39 @@ it('vite import analysis is applied when loading workspace config', async () =>
expect(stderr).toBe('')
expect(stdout).toContain('test - a')
})

it('can define inline workspace config programmatically', async () => {
const { stderr, stdout } = await runVitest({
root: 'fixtures/workspace/api',
env: {
TEST_ROOT: '1',
},
workspace: [
{
extends: true,
test: {
name: 'project-1',
},
},
{
test: {
name: 'project-2',
env: {
TEST_ROOT: '2',
},
},
},
{
extends: './vite.custom.config.js',
test: {
name: 'project-3',
},
},
],
})
expect(stderr).toBe('')
expect(stdout).toContain('project-1')
expect(stdout).toContain('project-2')
expect(stdout).toContain('project-3')
expect(stdout).toContain('3 passed')
})

0 comments on commit 851b02f

Please sign in to comment.