This repository has been archived by the owner on Apr 6, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
context.ts
59 lines (51 loc) · 1.74 KB
/
context.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { resolve } from 'node:path'
import defu from 'defu'
import type { TestContext, TestOptions, TestRunner } from './types'
let currentContext: TestContext | undefined
export function createTestContext (options: Partial<TestOptions>): TestContext {
const _options: Partial<TestOptions> = defu(options, {
testDir: resolve(process.cwd(), 'test'),
fixture: 'fixture',
configFile: 'nuxt.config',
setupTimeout: 120 * 1000,
dev: !!JSON.parse(process.env.NUXT_TEST_DEV || 'false'),
logLevel: 1,
server: true,
build: (options.browser !== false) || (options.server !== false),
nuxtConfig: {},
// TODO: auto detect based on process.env
runner: <TestRunner>'vitest',
browserOptions: {
type: 'chromium' as const
}
})
return setTestContext({
options: _options as TestOptions
})
}
export function useTestContext (): TestContext {
recoverContextFromEnv()
if (!currentContext) {
throw new Error('No context is available. (Forgot calling setup or createContext?)')
}
return currentContext
}
export function setTestContext (context: TestContext): TestContext
export function setTestContext (context?: TestContext): TestContext | undefined
export function setTestContext (context?: TestContext): TestContext | undefined {
currentContext = context
return currentContext
}
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 })
}