From 4e6f08371a3a11bc3c5e5feaa5365a56450c54c6 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Fri, 10 Jan 2025 11:12:10 +0000 Subject: [PATCH 1/5] test: add `init` and `add` e2e tests --- src/commands/init.ts | 2 +- test/e2e/commands.spec.ts | 47 +++++++++++++++++++++++++++++++++------ 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/src/commands/init.ts b/src/commands/init.ts index 5ff915976..8fca9df23 100644 --- a/src/commands/init.ts +++ b/src/commands/init.ts @@ -25,7 +25,7 @@ const pms: Record = { } // this is for type safety to prompt updating code in nuxi when nypm adds a new package manager -const packageManagerOptions = Object.keys(pms) as PackageManagerName[] +export const packageManagerOptions = Object.keys(pms) as PackageManagerName[] export default defineCommand({ meta: { diff --git a/test/e2e/commands.spec.ts b/test/e2e/commands.spec.ts index ce936e1c5..86e9b1d56 100644 --- a/test/e2e/commands.spec.ts +++ b/test/e2e/commands.spec.ts @@ -1,18 +1,35 @@ import type { TestFunction } from 'vitest' import type { commands } from '../../src/commands' +import { existsSync } from 'node:fs' + +import { readdir, rm } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' import { fileURLToPath } from 'node:url' import { isWindows } from 'std-env' - import { x } from 'tinyexec' -import { describe, expect, it } from 'vitest' +import { describe, expect, it, vi } from 'vitest' +import { packageManagerOptions } from '../../src/commands/init' const fixtureDir = fileURLToPath(new URL('../../playground', import.meta.url)) +vi.mock('@nuxt/kit', () => { + return {} +}) + describe('commands', () => { const tests: Record> = { _dev: 'todo', - add: 'todo', + add: async () => { + const file = join(fixtureDir, 'server/api/test.ts') + await rm(file, { force: true }) + await x('nuxi', ['add', 'api', 'test'], { + nodeOptions: { stdio: 'pipe', cwd: fixtureDir }, + }) + expect(existsSync(file)).toBeTruthy() + await rm(file, { force: true }) + }, analyze: 'todo', build: 'todo', cleanup: 'todo', @@ -26,12 +43,28 @@ describe('commands', () => { upgrade: 'todo', dev: 'todo', generate: 'todo', - init: 'todo', + init: async () => { + const dir = tmpdir() + for (const pm of ['pnpm']) { + const installPath = join(dir, pm) + await rm(installPath, { recursive: true, force: true }) + try { + await x('nuxi', ['init', installPath, `--packageManager=${pm}`, '--gitInit=false', '--preferOffline'], { + nodeOptions: { stdio: 'inherit', cwd: fixtureDir }, + }) + const files = await readdir(installPath).catch(() => []) + expect(files).toContain('nuxt.config.ts') + } + finally { + await rm(installPath, { recursive: true, force: true }) + } + } + }, info: 'todo', } it('throws error if no command is provided', async () => { - const res = await x('pnpm', ['nuxi'], { + const res = await x('nuxi', [], { nodeOptions: { stdio: 'pipe', cwd: fixtureDir }, }) expect(res.exitCode).toBe(1) @@ -40,7 +73,7 @@ describe('commands', () => { // TODO: FIXME - windows currently throws 'nuxt-foo' is not recognized as an internal or external command, operable program or batch file. it.skipIf(isWindows)('throws error if wrong command is provided', async () => { - const res = await x('pnpm', ['nuxi', 'foo'], { + const res = await x('nuxi', ['foo'], { nodeOptions: { stdio: 'pipe', cwd: fixtureDir }, }) expect(res.exitCode).toBe(1) @@ -48,7 +81,7 @@ describe('commands', () => { }) const testsToRun = Object.entries(tests).filter(([_, value]) => value !== 'todo') - it.each(testsToRun)(`%s`, (_, test) => (test as () => Promise)()) + it.each(testsToRun)(`%s`, (_, test) => (test as () => Promise)(), { timeout: 50000 }) for (const [command, value] of Object.entries(tests)) { if (value === 'todo') { From 515e88bfa13ed87b7968a8507a8f451067b19e03 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Fri, 10 Jan 2025 11:12:54 +0000 Subject: [PATCH 2/5] chore: cleanup --- test/e2e/commands.spec.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/test/e2e/commands.spec.ts b/test/e2e/commands.spec.ts index 86e9b1d56..aa3d6e6b5 100644 --- a/test/e2e/commands.spec.ts +++ b/test/e2e/commands.spec.ts @@ -9,15 +9,10 @@ import { join } from 'node:path' import { fileURLToPath } from 'node:url' import { isWindows } from 'std-env' import { x } from 'tinyexec' -import { describe, expect, it, vi } from 'vitest' -import { packageManagerOptions } from '../../src/commands/init' +import { describe, expect, it } from 'vitest' const fixtureDir = fileURLToPath(new URL('../../playground', import.meta.url)) -vi.mock('@nuxt/kit', () => { - return {} -}) - describe('commands', () => { const tests: Record> = { _dev: 'todo', From 897e0f827983c8311b58ab877389d73bdce1ee12 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Fri, 10 Jan 2025 11:15:43 +0000 Subject: [PATCH 3/5] =?UTF-8?q?=F0=9F=A4=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- knip.json | 5 ----- 1 file changed, 5 deletions(-) diff --git a/knip.json b/knip.json index 7fa3dd15f..f9f3ab9d2 100644 --- a/knip.json +++ b/knip.json @@ -1,11 +1,6 @@ { "$schema": "https://unpkg.com/knip@5/schema.json", "workspaces": { - ".": { - "ignoreDependencies": [ - "pkg-pr-new" - ] - }, "playground": { "ignoreDependencies": [ "nuxi", From e146d0a57650e5db4fb6cece85bfb5d2ceb6a823 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Fri, 10 Jan 2025 11:16:10 +0000 Subject: [PATCH 4/5] test: more time on windows --- test/e2e/commands.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/e2e/commands.spec.ts b/test/e2e/commands.spec.ts index aa3d6e6b5..9af8adf10 100644 --- a/test/e2e/commands.spec.ts +++ b/test/e2e/commands.spec.ts @@ -76,7 +76,7 @@ describe('commands', () => { }) const testsToRun = Object.entries(tests).filter(([_, value]) => value !== 'todo') - it.each(testsToRun)(`%s`, (_, test) => (test as () => Promise)(), { timeout: 50000 }) + it.each(testsToRun)(`%s`, (_, test) => (test as () => Promise)(), { timeout: isWindows ? 100000 : 50000 }) for (const [command, value] of Object.entries(tests)) { if (value === 'todo') { From ed7c6fe0a12502e7cab02a50c8134fe6e5f82f09 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Fri, 10 Jan 2025 11:17:10 +0000 Subject: [PATCH 5/5] chore: remove export again --- src/commands/init.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/init.ts b/src/commands/init.ts index 8fca9df23..5ff915976 100644 --- a/src/commands/init.ts +++ b/src/commands/init.ts @@ -25,7 +25,7 @@ const pms: Record = { } // this is for type safety to prompt updating code in nuxi when nypm adds a new package manager -export const packageManagerOptions = Object.keys(pms) as PackageManagerName[] +const packageManagerOptions = Object.keys(pms) as PackageManagerName[] export default defineCommand({ meta: {