-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
/
ssr-html.spec.ts
97 lines (85 loc) · 2.77 KB
/
ssr-html.spec.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
97
import { execFile } from 'node:child_process'
import { promisify } from 'node:util'
import path from 'node:path'
import fetch from 'node-fetch'
import { describe, expect, test } from 'vitest'
import { port } from './serve'
import { editFile, isServe, page, untilUpdated } from '~utils'
const url = `http://localhost:${port}`
describe('injected inline scripts', () => {
test('no injected inline scripts are present', async () => {
await page.goto(url)
const inlineScripts = await page.$$eval('script', (nodes) =>
nodes.filter((n) => !n.getAttribute('src') && n.innerHTML),
)
expect(inlineScripts).toHaveLength(0)
})
test('injected script proxied correctly', async () => {
await page.goto(url)
const proxiedScripts = await page.$$eval('script', (nodes) =>
nodes
.filter((n) => {
const src = n.getAttribute('src')
if (!src) return false
return src.includes('?html-proxy&index')
})
.map((n) => n.getAttribute('src')),
)
// assert at least 1 proxied script exists
expect(proxiedScripts).not.toHaveLength(0)
const scriptContents = await Promise.all(
proxiedScripts.map((src) => fetch(url + src).then((res) => res.text())),
)
// all proxied scripts return code
for (const code of scriptContents) {
expect(code).toBeTruthy()
}
})
})
describe.runIf(isServe)('hmr', () => {
test('handle virtual module updates', async () => {
await page.goto(url)
const el = await page.$('.virtual')
expect(await el.textContent()).toBe('[success]')
editFile('src/importedVirtual.js', (code) =>
code.replace('[success]', '[wow]'),
)
await page.waitForNavigation()
await untilUpdated(async () => {
const el = await page.$('.virtual')
return await el.textContent()
}, '[wow]')
})
})
describe.runIf(isServe)('stacktrace', () => {
const execFileAsync = promisify(execFile)
for (const ext of ['js', 'ts']) {
for (const sourcemapsEnabled of [false, true]) {
test(`stacktrace of ${ext} is correct when sourcemaps is${
sourcemapsEnabled ? '' : ' not'
} enabled in Node.js`, async () => {
const testStacktraceFile = path.resolve(
__dirname,
'../test-stacktrace.js',
)
const p = await execFileAsync('node', [
testStacktraceFile,
'' + sourcemapsEnabled,
ext,
])
const lines = p.stdout
.split('\n')
.filter((line) => line.includes('Module.error'))
const reg = new RegExp(
path
.resolve(__dirname, '../src', `error.${ext}`)
.replace(/\\/g, '\\\\') + ':2:9',
'i',
)
lines.forEach((line) => {
expect(line.trim()).toMatch(reg)
})
})
}
}
})