-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathindex.ts
84 lines (69 loc) · 1.9 KB
/
index.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { createTestContext, setTestContext } from '../context'
import { buildFixture, loadFixture } from '../nuxt'
import { reuseExistingServer, startServer, stopServer } from '../server'
import { createBrowser } from '../browser'
import type { TestHooks, TestOptions } from '../types'
import setupCucumber from './cucumber'
import setupJest from './jest'
import setupVitest from './vitest'
export const setupMaps = {
cucumber: setupCucumber,
jest: setupJest,
vitest: setupVitest,
}
export function createTest(options: Partial<TestOptions>): TestHooks {
const ctx = createTestContext(options)
const beforeEach = () => {
setTestContext(ctx)
}
const afterEach = () => {
setTestContext(undefined)
}
const afterAll = async () => {
if (ctx.serverProcess) {
setTestContext(ctx)
await stopServer()
setTestContext(undefined)
}
if (ctx.nuxt && ctx.nuxt.options.dev) {
await ctx.nuxt.close()
}
if (ctx.browser) {
await ctx.browser.close()
}
// clear side effects
await Promise.all(!ctx.teardown ? [] : ctx.teardown.map(fn => fn()))
}
const setup = async () => {
if (ctx.options.reuseExistingServer) {
await reuseExistingServer()
}
if (ctx.options.fixture) {
await loadFixture()
}
if (ctx.options.build && !ctx.options.reuseExistingServer) {
await buildFixture()
}
if (ctx.options.server && !ctx.options.reuseExistingServer) {
await startServer()
}
if (ctx.options.waitFor) {
await (new Promise(resolve => setTimeout(resolve, ctx.options.waitFor)))
}
if (ctx.options.browser) {
await createBrowser()
}
}
return {
beforeEach,
afterEach,
afterAll,
setup,
ctx,
}
}
export async function setup(options: Partial<TestOptions> = {}) {
const hooks = createTest(options)
const setupFn = setupMaps[hooks.ctx.options.runner]
await setupFn(hooks)
}