Skip to content

Commit 23dda73

Browse files
authored
fix: share the server on self-referencing extends (#11034)
1 parent 221aaef commit 23dda73

3 files changed

Lines changed: 59 additions & 2 deletions

File tree

docs/config/sharedviteserver.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Inline [projects](/guide/projects) that don't modify the Vite config reuse the V
1313

1414
This option _only_ applies to inline projects. Projects referenced as config files or directories always resolve their own Vite config and create their own server.
1515

16-
A project still gets its own Vite server when it defines Vite-level options that change the server (`plugins`, `resolve`, and so on), when its `extends` doesn't point to the declaring config, or when it defines test options that affect the Vite config:
16+
A project still gets its own Vite server when it defines Vite-level options that change the server (`plugins`, `resolve`, and so on), when its `extends` doesn't point to the declaring config (`extends: true` and a path that resolves to the declaring config file are equivalent), or when it defines test options that affect the Vite config:
1717

1818
- [`alias`](/config/alias)
1919
- [`browser`](/config/browser/enabled)

packages/vitest/src/node/projects/resolveProjects.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,13 @@ function getOwnServerReason(
516516
return 'the raw `test` options of the declaring config are not available'
517517
}
518518
if (options.extends !== undefined && options.extends !== true) {
519-
return '`extends` doesn\'t point to the declaring config'
519+
// a path back to the declaring config means the same as `extends: true`
520+
const extendsDeclaringConfig = typeof options.extends === 'string'
521+
&& context.parentViteConfig.configFile !== undefined
522+
&& resolve(context.parentConfig.root, options.extends) === context.parentViteConfig.configFile
523+
if (!extendsDeclaringConfig) {
524+
return '`extends` doesn\'t point to the declaring config'
525+
}
520526
}
521527
for (const key in options) {
522528
if (key === 'test' || key === 'extends') {

test/e2e/test/projects.test.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,57 @@ describe('sharedViteServer', () => {
981981
expect(e2e.sharedViteServer).toBe(true)
982982
})
983983

984+
it('a string `extends` pointing at the declaring config keeps the server shared', async () => {
985+
const { stderr, ctx } = await runInlineTests({
986+
'vitest.config.js': {
987+
test: {
988+
testTimeout: 4321,
989+
projects: [
990+
{ extends: './vitest.config.js', test: { name: 'self' } },
991+
{ extends: './base.config.js', test: { name: 'other' } },
992+
],
993+
},
994+
},
995+
'base.config.js': {},
996+
'basic.test.js': basicTest,
997+
})
998+
expect(stderr).toBe('')
999+
const root = ctx!.getRootProject()
1000+
const shared = Object.fromEntries(
1001+
ctx!.projects.map(project => [project.name, project.vite === root.vite]),
1002+
)
1003+
expect(shared).toEqual({
1004+
self: true,
1005+
other: false,
1006+
})
1007+
const self = ctx!.projects.find(project => project.name === 'self')!
1008+
expect(self.config.testTimeout).toBe(4321)
1009+
})
1010+
1011+
it('a string `extends` pointing at the container config shares the container server', async () => {
1012+
const { stderr, ctx } = await runInlineTests({
1013+
'vitest.config.js': {
1014+
test: {
1015+
projects: ['./app/vitest.config.js'],
1016+
},
1017+
},
1018+
'app/vitest.config.js': {
1019+
test: {
1020+
name: 'app',
1021+
projects: [
1022+
{ extends: './vitest.config.js', test: { name: 'unit' } },
1023+
],
1024+
},
1025+
},
1026+
'app/basic.test.js': basicTest,
1027+
})
1028+
expect(stderr).toBe('')
1029+
const [unit] = ctx!.projects
1030+
expect(unit.name).toBe('app (unit)')
1031+
expect(unit.sharedViteServer).toBe(true)
1032+
expect(unit.vite).not.toBe(ctx!.getRootProject().vite)
1033+
})
1034+
9841035
it('vite options that don\'t change the server don\'t prevent sharing', async () => {
9851036
const { stderr, ctx } = await runInlineTests({
9861037
'vitest.config.js': ts`

0 commit comments

Comments
 (0)