Skip to content

Commit

Permalink
chore: use tinyexec to run tests (nuxt-modules#3103)
Browse files Browse the repository at this point in the history
  • Loading branch information
BobbieGoede authored Sep 14, 2024
1 parent c501d0e commit 851b5dc
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 125 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@
"consola": "^3",
"eslint": "^9.5.0",
"eslint-config-prettier": "^9.1.0",
"execa": "^9.3.0",
"get-port-please": "^3.1.1",
"gh-changelogen": "^0.2.8",
"globals": "^15.6.0",
Expand All @@ -131,6 +130,7 @@
"pkg-pr-new": "^0.0.20",
"playwright-core": "^1.45.3",
"prettier": "^3.3.2",
"tinyexec": "^0.3.0",
"ts-essentials": "^9.4.2",
"typescript": "^5.6.2",
"typescript-eslint": "^7.14.0",
Expand Down
99 changes: 4 additions & 95 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 30 additions & 27 deletions specs/utils/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */

import { execa } from 'execa'
import { exec } from 'tinyexec'
import { getRandomPort, waitForPort } from 'get-port-please'
import type { FetchOptions } from 'ofetch'
import { $fetch as _$fetch, fetch as _fetch } from 'ofetch'
Expand All @@ -21,16 +21,18 @@ export async function startServer(env: Record<string, unknown> = {}) {
ctx.url = `http://${host}:${port}`
if (ctx.options.dev) {
const nuxiCLI = await kit.resolvePath('nuxi/cli')
ctx.serverProcess = execa(nuxiCLI, ['_dev'], {
cwd: ctx.nuxt!.options.rootDir,
stdio: 'inherit',
env: {
...process.env,
_PORT: String(port), // Used by internal _dev command
PORT: String(port),
HOST: host,
NODE_ENV: 'development',
...env
ctx.serverProcess = exec(nuxiCLI, ['_dev'], {
nodeOptions: {
cwd: ctx.nuxt!.options.rootDir,
stdio: 'inherit',
env: {
...process.env,
_PORT: String(port), // Used by internal _dev command
PORT: String(port),
HOST: host,
NODE_ENV: 'development',
...env
}
}
})
await waitForPort(port, { retries: 32, host }).catch(() => {})
Expand All @@ -53,27 +55,28 @@ export async function startServer(env: Record<string, unknown> = {}) {
// ; (await import('consola')).consola.restoreConsole()
const [_command, ...commandArgs] = command.split(' ')

ctx.serverProcess = execa(_command, commandArgs, {
//@ts-ignore
env: {
...process.env,
PORT: String(port),
HOST: host,
...env
ctx.serverProcess = exec(_command, commandArgs, {
nodeOptions: {
env: {
...process.env,
PORT: String(port),
HOST: host,
...env
}
}
})

await waitForPort(port, { retries: 32, host, delay: 1000 })
} else {
//@ts-ignore
ctx.serverProcess = execa('node', [resolve(ctx.nuxt!.options.nitro.output!.dir!, 'server/index.mjs')], {
stdio: 'inherit',
env: {
...process.env,
PORT: String(port),
HOST: host,
NODE_ENV: 'test',
...env
ctx.serverProcess = exec('node', [resolve(ctx.nuxt!.options.nitro.output!.dir!, 'server/index.mjs')], {
nodeOptions: {
stdio: 'inherit',
env: {
...process.env,
PORT: String(port),
HOST: host,
...env
}
}
})
await waitForPort(port, { retries: 20, host })
Expand Down
4 changes: 2 additions & 2 deletions specs/utils/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Nuxt, NuxtConfig } from '@nuxt/schema'
import type { Subprocess } from 'execa'
import type { exec } from 'tinyexec'
import type { Browser, LaunchOptions } from 'playwright-core'
import type { NuxtI18nOptions } from '../../src/types'
import type { Suite, File } from 'vitest'
Expand Down Expand Up @@ -34,7 +34,7 @@ export interface TestContext {
nuxt?: Nuxt
browser?: Browser
url?: string
serverProcess?: Subprocess
serverProcess?: ReturnType<typeof exec>
// eslint-disable-next-line @typescript-eslint/ban-types
mockFn?: Function
}
Expand Down

0 comments on commit 851b5dc

Please sign in to comment.