Skip to content

Commit

Permalink
perf(e2e): use tinyexec to run commands (#939)
Browse files Browse the repository at this point in the history
  • Loading branch information
BobbieGoede committed Sep 10, 2024
1 parent e1a8ef9 commit 4bd549f
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 38 deletions.
3 changes: 2 additions & 1 deletion examples/app-jest/test/browser.e2e.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { fileURLToPath } from 'node:url'
import { createPage, setup } from '@nuxt/test-utils/e2e'
import { isWindows } from 'std-env'

await setup({
rootDir: fileURLToPath(new URL('../', import.meta.url)),
Expand All @@ -12,5 +13,5 @@ describe('browser', () => {
const text = await page.getByRole('heading', { name: 'Welcome to Nuxt!' }).textContent()
expect(text).toContain('Welcome to Nuxt!')
await page.close()
}, 10000)
}, isWindows ? 60000 : 10000)
})
3 changes: 2 additions & 1 deletion examples/app-vitest/test/browser.e2e.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { fileURLToPath } from 'node:url'
import { createPage, setup } from '@nuxt/test-utils/e2e'
import { describe, expect, it } from 'vitest'
import { isWindows } from 'std-env'

await setup({
rootDir: fileURLToPath(new URL('../', import.meta.url)),
Expand All @@ -13,5 +14,5 @@ describe('browser', () => {
const text = await page.getByRole('heading', { name: 'Welcome to Nuxt!' }).textContent()
expect(text).toContain('Welcome to Nuxt!')
await page.close()
}, 10000)
}, isWindows ? 60000 : 10000)
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
"defu": "^6.1.4",
"destr": "^2.0.3",
"estree-walker": "^3.0.3",
"execa": "^8.0.1",
"fake-indexeddb": "^6.0.0",
"get-port-please": "^3.1.2",
"local-pkg": "^0.5.0",
Expand All @@ -59,6 +58,7 @@
"radix3": "^1.1.2",
"scule": "^1.3.0",
"std-env": "^3.7.0",
"tinyexec": "^0.3.0",
"ufo": "^1.5.4",
"unenv": "^1.10.0",
"unplugin": "^1.14.0",
Expand Down
11 changes: 8 additions & 3 deletions pnpm-lock.yaml

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

4 changes: 2 additions & 2 deletions scripts/_utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { promises as fsp } from 'node:fs'
import { resolve } from 'pathe'
import { execaSync } from 'execa'
import { exec } from 'tinyexec'
import { determineSemverChange, getGitDiff, loadChangelogConfig, parseCommits } from 'changelogen'

export interface Dep {
Expand Down Expand Up @@ -105,7 +105,7 @@ export async function determineBumpType() {

export async function getLatestCommits() {
const config = await loadChangelogConfig(process.cwd())
const latestTag = execaSync('git', ['describe', '--tags', '--abbrev=0']).stdout
const { stdout: latestTag } = await exec('git', ['describe', '--tags', '--abbrev=0'])

return parseCommits(await getGitDiff(latestTag), config)
}
64 changes: 36 additions & 28 deletions src/core/server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
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 @@ -18,29 +18,33 @@ export async function startServer(options: StartServerOptions = {}) {
const ctx = useTestContext()
await stopServer()
const host = '127.0.0.1'
const port = ctx.options.port || await getRandomPort(host)
const port = ctx.options.port || (await getRandomPort(host))
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',
...options.env,
...ctx.options.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',
...options.env,
...ctx.options.env,
},
},
})
await waitForPort(port, { retries: 32, host }).catch(() => {})
let lastError
for (let i = 0; i < 150; i++) {
await new Promise(resolve => setTimeout(resolve, 100))
try {
const res = await $fetch<string>(ctx.nuxt!.options.app.baseURL, { responseType: 'text' })
const res = await $fetch<string>(ctx.nuxt!.options.app.baseURL, {
responseType: 'text',
})
if (!res.includes('__NUXT_LOADING__')) {
return
}
Expand All @@ -53,19 +57,23 @@ export async function startServer(options: StartServerOptions = {}) {
throw lastError || new Error('Timeout waiting for dev server!')
}
else {
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',
...options.env,
...ctx.options.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,
NODE_ENV: 'test',
...options.env,
...ctx.options.env,
},
},
},
})
)
await waitForPort(port, { retries: 20, host })
}
}
Expand All @@ -81,9 +89,9 @@ export function fetch(path: string, options?: RequestInit) {
return _fetch(url(path), options)
}

export const $fetch = (function (path: string, options?: FetchOptions) {
export const $fetch = function (path: string, options?: FetchOptions) {
return _$fetch(url(path), options)
}) as typeof globalThis['$fetch']
} as (typeof globalThis)['$fetch']

export function url(path: string) {
const ctx = useTestContext()
Expand Down
4 changes: 2 additions & 2 deletions src/core/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Nuxt, NuxtConfig } from '@nuxt/schema'
import type { ExecaChildProcess } from 'execa'
import type { Browser, LaunchOptions } from 'playwright-core'
import type { exec } from 'tinyexec'
import type { StartServerOptions } from './server'

export type TestRunner = 'vitest' | 'jest' | 'cucumber'
Expand Down Expand Up @@ -72,7 +72,7 @@ export interface TestContext {
nuxt?: Nuxt
browser?: Browser
url?: string
serverProcess?: ExecaChildProcess
serverProcess?: ReturnType<typeof exec>
mockFn?: (...args: unknown[]) => unknown
/**
* Functions to run on the vitest `afterAll` hook.
Expand Down

0 comments on commit 4bd549f

Please sign in to comment.