mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 05:35:13 +00:00
feat(nuxi)!: setup nuxt globally with nuxt test
(#4578)
This commit is contained in:
parent
3dd9271c09
commit
b4d7d62287
@ -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",
|
||||
|
@ -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!')
|
||||
})
|
||||
|
@ -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.')
|
||||
|
@ -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',
|
||||
|
@ -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",
|
||||
|
@ -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?)')
|
||||
}
|
||||
@ -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
packages/test-utils/src/dirs.ts
Normal file
5
packages/test-utils/src/dirs.ts
Normal file
@ -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, '..')
|
@ -5,3 +5,4 @@ export * from './nuxt'
|
||||
export * from './server'
|
||||
export * from './setup'
|
||||
export * from './run'
|
||||
export * from './types'
|
||||
|
@ -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) {
|
||||
@ -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',
|
||||
@ -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')] : []
|
||||
]
|
||||
}
|
||||
}
|
||||
)
|
||||
|
12
packages/test-utils/src/runtime/global-setup.ts
Normal file
12
packages/test-utils/src/runtime/global-setup.ts
Normal file
@ -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()
|
||||
}
|
@ -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]
|
||||
|
@ -557,6 +557,7 @@ importers:
|
||||
get-port-please: ^2.6.1
|
||||
jiti: ^1.16.0
|
||||
ohmyfetch: ^0.4.21
|
||||
pathe: ^0.3.8
|
||||
playwright: ^1.27.1
|
||||
unbuild: ^0.9.4
|
||||
vitest: ^0.25.1
|
||||
@ -569,6 +570,7 @@ importers:
|
||||
get-port-please: 2.6.1
|
||||
jiti: 1.16.0
|
||||
ohmyfetch: 0.4.21
|
||||
pathe: 0.3.9
|
||||
devDependencies:
|
||||
playwright: 1.27.1
|
||||
unbuild: 0.9.4
|
||||
|
Loading…
Reference in New Issue
Block a user