fix(test): resolve test utils from project · nuxt/cli@6ff5c36 · GitHub
Skip to content

Commit 6ff5c36

Browse files
committed
fix(test): resolve test utils from project
1 parent 00b2828 commit 6ff5c36

3 files changed

Lines changed: 83 additions & 28 deletions

File tree

Lines changed: 16 additions & 24 deletions
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { mkdir, rm, writeFile } from 'node:fs/promises'
2+
import { tmpdir } from 'node:os'
3+
import { join } from 'node:path'
4+
5+
import { afterEach, describe, expect, it } from 'vitest'
6+
7+
import { importTestUtils } from '../../../src/commands/test'
8+
9+
const tempDirs: string[] = []
10+
11+
async function createProject(packageName?: string, source = 'export function runTests() {}') {
12+
const rootDir = join(tmpdir(), `nuxt-test-command-${crypto.randomUUID()}`)
13+
tempDirs.push(rootDir)
14+
await mkdir(rootDir, { recursive: true })
15+
16+
if (packageName) {
17+
const packageDir = join(rootDir, 'node_modules', ...packageName.split('/'))
18+
await mkdir(packageDir, { recursive: true })
19+
await writeFile(join(packageDir, 'package.json'), JSON.stringify({ name: packageName, type: 'module', exports: './index.js' }))
20+
await writeFile(join(packageDir, 'index.js'), source)
21+
}
22+
23+
return rootDir
24+
}
25+
26+
afterEach(async () => {
27+
await Promise.all(tempDirs.splice(0).map(dir => rm(dir, { recursive: true, force: true })))
28+
})
29+
30+
describe('importTestUtils', () => {
31+
it('loads test utils from the project', async () => {
32+
const rootDir = await createProject('@nuxt/test-utils')
33+
34+
expect((await importTestUtils(rootDir)).runTests).toBeTypeOf('function')
35+
})
36+
37+
it('prefers nightly when multiple variants are installed', async () => {
38+
const rootDir = await createProject('@nuxt/test-utils-nightly', 'export function runTests() {}; export const channel = "nightly"')
39+
const packageDir = join(rootDir, 'node_modules', '@nuxt', 'test-utils')
40+
await mkdir(packageDir, { recursive: true })
41+
await writeFile(join(packageDir, 'package.json'), JSON.stringify({ name: '@nuxt/test-utils', type: 'module', exports: './index.js' }))
42+
await writeFile(join(packageDir, 'index.js'), 'export function runTests() {}; export const channel = "stable"')
43+
44+
expect((await importTestUtils(rootDir) as any).channel).toBe('nightly')
45+
})
46+
47+
it('does not fall back when an installed variant fails to load', async () => {
48+
const rootDir = await createProject('@nuxt/test-utils-nightly', 'import "missing-dependency"; export function runTests() {}')
49+
50+
await expect(importTestUtils(rootDir)).rejects.toThrow(/missing-dependency/)
51+
})
52+
53+
it('reports an incompatible installed version', async () => {
54+
const rootDir = await createProject('@nuxt/test-utils', 'export const setup = true')
55+
56+
await expect(importTestUtils(rootDir)).rejects.toThrow('The installed version of `@nuxt/test-utils` does not support `nuxt test`.')
57+
})
58+
59+
it('reports a missing project dependency', async () => {
60+
const rootDir = await createProject()
61+
62+
await expect(importTestUtils(rootDir)).rejects.toThrow('`@nuxt/test-utils` is not installed in this project.')
63+
})
64+
})

‎packages/nuxt-cli/test/unit/help.spec.ts‎

Lines changed: 3 additions & 4 deletions

0 commit comments

Comments
 (0)