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

feat(nuxi)!: setup nuxt globally with nuxt test #4578

Merged
merged 18 commits into from Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion examples/advanced/testing/package.json
Expand Up @@ -4,9 +4,11 @@
"scripts": {
"build": "nuxi build",
"dev": "nuxi dev",
"start": "nuxi preview"
"start": "nuxi preview",
"test": "nuxi test"
},
"devDependencies": {
"@nuxt/test-utils": "npm:@nuxt/test-utils-edge@latest",
"nuxt": "npm:nuxt3@latest"
}
}
10 changes: 2 additions & 8 deletions examples/advanced/testing/tests/basic.test.ts
@@ -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
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
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' }
],
dependencies: [
],
Expand Down
3 changes: 2 additions & 1 deletion packages/test-utils/package.json
Expand Up @@ -19,7 +19,8 @@
"execa": "^6.1.0",
"get-port-please": "^2.5.0",
"jiti": "^1.13.0",
"ohmyfetch": "^0.4.18"
"ohmyfetch": "^0.4.18",
"pathe": "^0.3.0"
},
"devDependencies": {
"playwright": "^1.22.2",
Expand Down
20 changes: 20 additions & 0 deletions packages/test-utils/src/context.ts
Expand Up @@ -26,6 +26,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 @@ -41,3 +42,22 @@ export function isDev () {
const ctx = useTestContext()
return ctx.options.dev
}

export function recoverContextFromEnv () {
if (process.env.NUXT_TEST_CONTEXT && !currentContext) {
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
@@ -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
Expand Up @@ -4,3 +4,4 @@ export * from './nuxt'
export * from './server'
export * from './setup'
export * from './run'
export * from './types'
26 changes: 24 additions & 2 deletions packages/test-utils/src/run.ts
@@ -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,18 +26,35 @@ export async function runTests (opts: RunTestOptions) {
process.env.NUXT_TEST_DEV = 'true'
}

process.env.NUXT_TEST_OPTIONS = JSON.stringify(opts)
pi0 marked this conversation as resolved.
Show resolved Hide resolved

const { startVitest } = await import('vitest/dist/node.js')
const succeeded = await startVitest(
[] /* argv */,
// Vitest options
{
root: opts.rootDir,
run: !opts.watch
},
// Vite options
{
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.mjs')
: undefined
]
}
}
)
Expand Down
12 changes: 12 additions & 0 deletions packages/test-utils/src/runtime/global-setup.ts
@@ -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
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
8 changes: 8 additions & 0 deletions yarn.lock
Expand Up @@ -1697,6 +1697,12 @@ __metadata:
languageName: node
linkType: hard

"@nuxt/test-utils@link:./packages/test-utils::locator=nuxt-framework%40workspace%3A.":
version: 0.0.0-use.local
resolution: "@nuxt/test-utils@link:./packages/test-utils::locator=nuxt-framework%40workspace%3A."
languageName: node
linkType: soft

"@nuxt/test-utils@workspace:packages/test-utils":
version: 0.0.0-use.local
resolution: "@nuxt/test-utils@workspace:packages/test-utils"
Expand All @@ -1708,6 +1714,7 @@ __metadata:
get-port-please: ^2.5.0
jiti: ^1.13.0
ohmyfetch: ^0.4.18
pathe: ^0.3.0
playwright: ^1.22.2
unbuild: latest
vitest: ^0.14.2
Expand Down Expand Up @@ -6421,6 +6428,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "example-testing@workspace:examples/advanced/testing"
dependencies:
"@nuxt/test-utils": "npm:@nuxt/test-utils-edge@latest"
nuxt: "npm:nuxt3@latest"
languageName: unknown
linkType: soft
Expand Down