Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

Commit

Permalink
feat(nuxi)!: setup nuxt globally with nuxt test (#4578)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu authored Nov 10, 2022
1 parent 3dd9271 commit b4d7d62
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 14 deletions.
2 changes: 1 addition & 1 deletion examples/advanced/testing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"build": "nuxi build",
"dev": "nuxi dev",
"start": "nuxi preview",
"test": "vitest run"
"test": "nuxi test"
},
"devDependencies": {
"@nuxt/test-utils": "^3.0.0-rc.13",
Expand Down
10 changes: 2 additions & 8 deletions examples/advanced/testing/tests/basic.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import { fileURLToPath } from 'node:url'
import { describe, expect, it } from 'vitest'
import { setup, $fetch, isDev } from '@nuxt/test-utils'

describe('example', async () => {
await setup({
rootDir: fileURLToPath(new URL('..', import.meta.url)),
server: true
})
import { $fetch, isDev } from '@nuxt/test-utils'

describe('example', () => {
it('Renders Hello Nuxt', async () => {
expect(await $fetch('/')).toMatch('Hello Nuxt!')
})
Expand Down
4 changes: 3 additions & 1 deletion packages/nuxi/src/commands/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ async function importTestUtils (): Promise<typeof import('@nuxt/test-utils')> {
throw new Error('Invalid version of `@nuxt/test-utils` is installed!')
}
return exports
} catch (_err) { err = _err }
} catch (_err) {
err = _err
}
}
console.error(err)
throw new Error('`@nuxt/test-utils-edge` seems missing. Run `npm i -D @nuxt/test-utils-edge` or `yarn add -D @nuxt/test-utils-edge` to install.')
Expand Down
3 changes: 2 additions & 1 deletion packages/test-utils/build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { defineBuildConfig } from 'unbuild'
export default defineBuildConfig({
declaration: true,
entries: [
'src/index'
'src/index',
{ input: 'src/runtime/', outDir: 'dist/runtime', format: 'esm' }
],
externals: [
'vitest',
Expand Down
3 changes: 2 additions & 1 deletion packages/test-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"execa": "^6.1.0",
"get-port-please": "^2.6.1",
"jiti": "^1.16.0",
"ohmyfetch": "^0.4.21"
"ohmyfetch": "^0.4.21",
"pathe": "^0.3.8"
},
"devDependencies": {
"playwright": "^1.27.1",
Expand Down
12 changes: 12 additions & 0 deletions packages/test-utils/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export function createTestContext (options: Partial<TestOptions>): TestContext {
}

export function useTestContext (): TestContext {
recoverContextFromEnv()
if (!currentContext) {
throw new Error('No context is available. (Forgot calling setup or createContext?)')
}
Expand All @@ -45,3 +46,14 @@ export function isDev () {
const ctx = useTestContext()
return ctx.options.dev
}

export function recoverContextFromEnv () {
if (!currentContext && process.env.NUXT_TEST_CONTEXT) {
setTestContext(JSON.parse(process.env.NUXT_TEST_CONTEXT || '{}'))
}
}

export function exposeContextToEnv () {
const { options, browser, url } = currentContext!
process.env.NUXT_TEST_CONTEXT = JSON.stringify({ options, browser, url })
}
5 changes: 5 additions & 0 deletions packages/test-utils/src/dirs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { fileURLToPath } from 'node:url'
import { dirname, resolve } from 'pathe'

export const distDir = dirname(fileURLToPath(import.meta.url))
export const pkgDir = resolve(distDir, '..')
1 change: 1 addition & 0 deletions packages/test-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './nuxt'
export * from './server'
export * from './setup'
export * from './run'
export * from './types'
24 changes: 23 additions & 1 deletion packages/test-utils/src/run.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { resolve } from 'pathe'
import { distDir } from './dirs'

export interface RunTestOptions {
rootDir: string,
dev?: boolean,
watch?: boolean
runner?: 'vitest'
globalSetup?: boolean
}

const RunTestDefaults: Partial<RunTestOptions> = {
runner: 'vitest'
runner: 'vitest',
globalSetup: true
}

export async function runTests (opts: RunTestOptions) {
Expand All @@ -21,6 +26,9 @@ export async function runTests (opts: RunTestOptions) {
process.env.NUXT_TEST_DEV = 'true'
}

// Consumed by recoverContextFromEnv()
process.env.NUXT_TEST_OPTIONS = JSON.stringify(opts)

const { startVitest } = await import('vitest/node')
const succeeded = await startVitest(
'test',
Expand All @@ -37,6 +45,20 @@ export async function runTests (opts: RunTestOptions) {
{
esbuild: {
tsconfigRaw: '{}'
},
test: {
dir: opts.rootDir,
deps: {
inline: [
distDir,
'@nuxt/test-utils',
'@nuxt/test-utils-edge'
]
},
globals: true,
globalSetup: [
...opts.globalSetup ? [resolve(distDir, './runtime/global-setup')] : []
]
}
}
)
Expand Down
12 changes: 12 additions & 0 deletions packages/test-utils/src/runtime/global-setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { createTest, exposeContextToEnv } from '@nuxt/test-utils'

const hooks = createTest(JSON.parse(process.env.NUXT_TEST_OPTIONS || '{}'))

export const setup = async () => {
await hooks.setup()
exposeContextToEnv()
}

export const teardown = async () => {
await hooks.afterAll()
}
2 changes: 1 addition & 1 deletion packages/test-utils/src/setup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export function createTest (options: Partial<TestOptions>): TestHooks {
}
}

export async function setup (options: Partial<TestOptions>) {
export async function setup (options: Partial<TestOptions> = {}) {
const hooks = createTest(options)

const setupFn = setupMaps[hooks.ctx.options.runner]
Expand Down
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit b4d7d62

Please sign in to comment.