-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
tests.test.ts
96 lines (88 loc) · 2.72 KB
/
tests.test.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
85
86
87
88
89
90
91
92
93
94
95
96
import { describe, test, beforeAll, afterAll, expect } from 'vitest'
import {
build,
createServer,
preview,
PreviewServer,
ViteDevServer
} from 'vite'
import fetch from 'node-fetch'
import { testcases } from './testcases'
import { getConfig, loadFileContent } from './utils'
import type { AddressInfo } from 'node:net'
const fetchFromServer = async (
server: ViteDevServer | PreviewServer,
path: string
) => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const port = (server.httpServer!.address() as AddressInfo).port
const url = `http://localhost:${port}${path}`
const res = await fetch(url)
return res
}
const fetchTextContent = async (
server: ViteDevServer | PreviewServer,
path: string
) => {
const res = await fetchFromServer(server, path)
const content = res.status === 200 ? await res.text() : null
return content
}
describe('serve', () => {
for (const [configFile, tests] of Object.entries(testcases)) {
describe(configFile, () => {
let server: ViteDevServer
beforeAll(async () => {
server = await createServer(getConfig(configFile))
server = await server.listen()
})
afterAll(async () => {
await server.close()
})
for (const { name, src, dest, transformedContent } of tests) {
test.concurrent(name, async () => {
const actual = await fetchTextContent(server, dest)
const expected = await loadFileContent(src)
expect(actual).toBe(transformedContent ?? expected)
})
}
})
}
describe('vite.other.config.ts', () => {
let server: ViteDevServer
beforeAll(async () => {
server = await createServer(getConfig('vite.other.config.ts'))
server = await server.listen()
})
afterAll(async () => {
await server.close()
})
test.concurrent('cors', async () => {
const res = await fetchFromServer(server, '/fixture1/foo.txt')
expect(res.status).toBe(200)
expect(res.headers.get('Access-Control-Allow-Origin')).toBe('*')
})
})
})
describe('build', () => {
for (const [configFile, tests] of Object.entries(testcases)) {
describe(configFile, () => {
let server: PreviewServer
beforeAll(async () => {
await build(getConfig(configFile))
server = await preview(getConfig(configFile))
server.printUrls
})
afterAll(() => {
server.httpServer.close()
})
for (const { name, src, dest, transformedContent } of tests) {
test.concurrent(name, async () => {
const actual = await fetchTextContent(server, dest)
const expected = await loadFileContent(src)
expect(actual).toBe(transformedContent ?? expected)
})
}
})
}
})