Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ export async function getVitestConfigFromNuxt(
})
}

// do not override vitest root
delete options.viteConfig.root

options.viteConfig.plugins = (options.viteConfig.plugins || []).filter(p => !p || !('name' in p) || !excludedPlugins.includes(p.name))

const resolver = createResolver(import.meta.url)
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/basic/test/basic.nuxt.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { it } from 'vitest'

it('basic/nuxt', () => {})
3 changes: 3 additions & 0 deletions test/fixtures/basic/test/basic.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { it } from 'vitest'

it('basic', () => {})
3 changes: 3 additions & 0 deletions test/fixtures/basic/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { defineVitestConfig } from '@nuxt/test-utils/config'

export default defineVitestConfig({})
2 changes: 2 additions & 0 deletions test/fixtures/override/test/app/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export default defineNuxtConfig({
})
3 changes: 3 additions & 0 deletions test/fixtures/override/test/override.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { it } from 'vitest'

it('override', () => {})
52 changes: 52 additions & 0 deletions test/fixtures/override/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { fileURLToPath } from 'node:url'
import { defineConfig } from 'vitest/config'
import { defineVitestProject } from '@nuxt/test-utils/config'

export default defineConfig({
test: {
projects: [
await defineVitestProject({
test: {
name: 'nuxt1',
environment: 'nuxt',
include: ['./test/**.spec.ts'],
environmentOptions: {
nuxt: {
overrides: {
rootDir: fileURLToPath(new URL('test/app/', import.meta.url)),
},
},
},
},
}),
await defineVitestProject({
extends: true,
test: {
name: 'nuxt2',
environment: 'nuxt',
include: ['./test/**.spec.ts'],
environmentOptions: {
nuxt: {
overrides: {
rootDir: fileURLToPath(new URL('test/app/', import.meta.url)),
},
},
},
},
}),
{
test: {
name: 'unit1',
include: ['./test/**.spec.ts'],
},
},
{
extends: true,
test: {
name: 'unit2',
include: ['./test/**.spec.ts'],
},
},
],
},
})
3 changes: 3 additions & 0 deletions test/fixtures/project/nuxt-test/project-nuxt.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { it } from 'vitest'

it('project/nuxt', () => {})
3 changes: 3 additions & 0 deletions test/fixtures/project/unit-test/project-unit.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { it } from 'vitest'

it('project unit', () => {})
21 changes: 21 additions & 0 deletions test/fixtures/project/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { defineConfig } from 'vitest/config'
import { defineVitestProject } from '@nuxt/test-utils/config'

export default defineConfig({
test: {
projects: [
await defineVitestProject({
test: {
name: 'nuxt',
include: ['./nuxt-test/**.spec.ts'],
},
}),
{
test: {
name: 'unit',
include: ['./unit-test/**.spec.ts'],
},
},
],
},
})
136 changes: 136 additions & 0 deletions test/unit/resolve-config.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import { join, relative } from 'pathe'
import { fileURLToPath } from 'node:url'
import { describe, expect, it } from 'vitest'

import { createVitest } from 'vitest/node'
import type { Vitest } from 'vitest/node'

type VitestCliOptions = Parameters<typeof createVitest>[1]

describe('resolve config', () => {
describe('basic', async () => {
const expected = {
node: [
'test/basic.spec.ts',
],
nuxt: [
'test/basic.nuxt.spec.ts',
],
} as const

it('all', async () => {
expect(await globTestSpecifications('../fixtures/basic')).toEqual(expected)
})

it('spcific root', async () => {
expect(await globTestSpecifications('./', {
root: '../fixtures/basic',
})).toEqual(expected)
})
})

describe('project', async () => {
const expected = {
nuxt: [
'nuxt-test/project-nuxt.spec.ts',
],
unit: [
'unit-test/project-unit.spec.ts',
],
} as const

it('all', async () => {
expect(await globTestSpecifications('../fixtures/project')).toEqual(expected)
})

it('only spcific project', async () => {
expect(await globTestSpecifications('../fixtures/project', {
project: 'nuxt',
})).toEqual({ nuxt: expected.nuxt })
})

it('spcific root', async () => {
expect(await globTestSpecifications('./', {
root: '../fixtures/project',
})).toEqual(expected)
})
})

describe('override', () => {
const expected = {
nuxt1: [
'test/override.spec.ts',
],
nuxt2: [
'test/override.spec.ts',
],
unit1: [
'test/override.spec.ts',
],
unit2: [
'test/override.spec.ts',
],
} as const

it('all', async () => {
expect(await globTestSpecifications('../fixtures/override')).toEqual(expected)
})

it('only spcific project', async () => {
expect(await globTestSpecifications('../fixtures/override', {
project: ['nuxt1', 'nuxt2'],
})).toEqual({
nuxt1: expected.nuxt1,
nuxt2: expected.nuxt2,
})
})

it('spcific root', async () => {
expect(await globTestSpecifications('./', {
root: '../fixtures/override',
})).toEqual(expected)
})
})
})

async function globTestSpecifications(path: string, cliOptions?: VitestCliOptions) {
const cwd = process.cwd()
let vitest: Vitest | undefined

try {
process.chdir(fileURLToPath(new URL(path, import.meta.url)))

vitest = await createVitest('test', {
...cliOptions,
filesOnly: true,
run: false,
watch: false,
cache: false,
})

const files = await vitest.globTestSpecifications()
const result: Record<string, string[]> = {}
const projectDir = join(vitest.config.root, vitest.config.dir)

for (const file of files) {
const project = file.project.name
const filename = relative(projectDir, file.moduleId)
result[project] ??= []
result[project].push(filename)
}

for (const key of Object.keys(result)) {
result[key as keyof typeof result]?.sort()
}

return result
}
finally {
try {
await vitest?.close()
}
finally {
process.chdir(cwd)
}
}
}
Loading