|
| 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 | +consttempDirs: string[]=[] |
| 10 | + |
| 11 | +asyncfunctioncreateProject(packageName?: string,source='export function runTests() {}'){ |
| 12 | +constrootDir=join(tmpdir(),`nuxt-test-command-${crypto.randomUUID()}`) |
| 13 | +tempDirs.push(rootDir) |
| 14 | +awaitmkdir(rootDir,{recursive: true}) |
| 15 | + |
| 16 | +if(packageName){ |
| 17 | +constpackageDir=join(rootDir,'node_modules', ...packageName.split('/')) |
| 18 | +awaitmkdir(packageDir,{recursive: true}) |
| 19 | +awaitwriteFile(join(packageDir,'package.json'),JSON.stringify({name: packageName,type: 'module',exports: './index.js'})) |
| 20 | +awaitwriteFile(join(packageDir,'index.js'),source) |
| 21 | +} |
| 22 | + |
| 23 | +returnrootDir |
| 24 | +} |
| 25 | + |
| 26 | +afterEach(async()=>{ |
| 27 | +awaitPromise.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 | +constrootDir=awaitcreateProject('@nuxt/test-utils') |
| 33 | + |
| 34 | +expect((awaitimportTestUtils(rootDir)).runTests).toBeTypeOf('function') |
| 35 | +}) |
| 36 | + |
| 37 | +it('prefers nightly when multiple variants are installed',async()=>{ |
| 38 | +constrootDir=awaitcreateProject('@nuxt/test-utils-nightly','export function runTests() {}; export const channel = "nightly"') |
| 39 | +constpackageDir=join(rootDir,'node_modules','@nuxt','test-utils') |
| 40 | +awaitmkdir(packageDir,{recursive: true}) |
| 41 | +awaitwriteFile(join(packageDir,'package.json'),JSON.stringify({name: '@nuxt/test-utils',type: 'module',exports: './index.js'})) |
| 42 | +awaitwriteFile(join(packageDir,'index.js'),'export function runTests() {}; export const channel = "stable"') |
| 43 | + |
| 44 | +expect((awaitimportTestUtils(rootDir)asany).channel).toBe('nightly') |
| 45 | +}) |
| 46 | + |
| 47 | +it('does not fall back when an installed variant fails to load',async()=>{ |
| 48 | +constrootDir=awaitcreateProject('@nuxt/test-utils-nightly','import "missing-dependency"; export function runTests() {}') |
| 49 | + |
| 50 | +awaitexpect(importTestUtils(rootDir)).rejects.toThrow(/missing-dependency/) |
| 51 | +}) |
| 52 | + |
| 53 | +it('reports an incompatible installed version',async()=>{ |
| 54 | +constrootDir=awaitcreateProject('@nuxt/test-utils','export const setup = true') |
| 55 | + |
| 56 | +awaitexpect(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 | +constrootDir=awaitcreateProject() |
| 61 | + |
| 62 | +awaitexpect(importTestUtils(rootDir)).rejects.toThrow('`@nuxt/test-utils` is not installed in this project.') |
| 63 | +}) |
| 64 | +}) |
0 commit comments