forked from vitejs/vite
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackend-integration.spec.ts
116 lines (105 loc) · 3.8 KB
/
backend-integration.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { describe, expect, test } from 'vitest'
import {
browserErrors,
browserLogs,
editFile,
getColor,
isBuild,
isServe,
listAssets,
page,
readManifest,
serverLogs,
untilBrowserLogAfter,
untilUpdated,
} from '~utils'
const outerAssetMatch = isBuild
? /\/dev\/assets\/logo-[-\w]{8}\.png/
: /\/dev\/@fs\/.+?\/images\/logo\.png/
test('should have no 404s', () => {
browserLogs.forEach((msg) => {
expect(msg).not.toMatch('404')
})
})
describe('asset imports from js', () => {
test('file outside root', async () => {
expect(
await page.textContent('.asset-reference.outside-root .asset-url'),
).toMatch(outerAssetMatch)
})
})
describe.runIf(isBuild)('build', () => {
test('manifest', async () => {
const manifest = readManifest('dev')
const htmlEntry = manifest['index.html']
const mainTsEntry = manifest['main.ts']
const cssAssetEntry = manifest['global.css']
const pcssAssetEntry = manifest['foo.pcss']
const scssAssetEntry = manifest['nested/blue.scss']
const imgAssetEntry = manifest['../images/logo.png']
const dirFooAssetEntry = manifest['../../dir/foo.css']
const iconEntrypointEntry = manifest['icon.png']
expect(htmlEntry.css.length).toEqual(1)
expect(htmlEntry.assets.length).toEqual(1)
expect(mainTsEntry.assets?.length ?? 0).toBeGreaterThanOrEqual(1)
expect(mainTsEntry.assets).toContainEqual(
expect.stringMatching(/assets\/url-[-\w]{8}\.css/),
)
expect(cssAssetEntry?.file).not.toBeUndefined()
expect(cssAssetEntry?.isEntry).toEqual(true)
expect(pcssAssetEntry?.file).not.toBeUndefined()
expect(pcssAssetEntry?.isEntry).toEqual(true)
expect(scssAssetEntry?.file).not.toBeUndefined()
expect(scssAssetEntry?.src).toEqual('nested/blue.scss')
expect(scssAssetEntry?.isEntry).toEqual(true)
expect(imgAssetEntry?.file).not.toBeUndefined()
expect(imgAssetEntry?.isEntry).toBeUndefined()
expect(dirFooAssetEntry).not.toBeUndefined() // '\\' should not be used even on windows
// use the entry name
expect(dirFooAssetEntry.file).toMatch('assets/bar-')
expect(iconEntrypointEntry?.file).not.toBeUndefined()
})
test('CSS imported from JS entry should have a non-nested chunk name', () => {
const manifest = readManifest('dev')
const mainTsEntryCss = manifest['nested/sub.ts'].css
expect(mainTsEntryCss.length).toBe(1)
expect(mainTsEntryCss[0].replace('assets/', '')).not.toContain('/')
})
test('entrypoint assets should not generate empty JS file', () => {
expect(serverLogs).not.toContainEqual(
'Generated an empty chunk: "icon.png".',
)
const assets = listAssets('dev')
expect(assets).not.toContainEqual(
expect.stringMatching(/icon.png-[-\w]{8}\.js$/),
)
})
})
describe.runIf(isServe)('serve', () => {
test('No ReferenceError', async () => {
browserErrors.forEach((error) => {
expect(error.name).not.toBe('ReferenceError')
})
})
test('preserve the base in CSS HMR', async () => {
await untilUpdated(() => getColor('body'), 'black') // sanity check
editFile('frontend/entrypoints/global.css', (code) =>
code.replace('black', 'red'),
)
await untilUpdated(() => getColor('body'), 'red') // successful HMR
// Verify that the base (/dev/) was added during the css-update
const link = await page.$('link[rel="stylesheet"]:last-of-type')
expect(await link.getAttribute('href')).toContain('/dev/global.css?t=')
})
test('CSS dependencies are tracked for HMR', async () => {
const el = await page.$('h1')
await untilBrowserLogAfter(
() =>
editFile('frontend/entrypoints/main.ts', (code) =>
code.replace('text-black', 'text-[rgb(204,0,0)]'),
),
'[vite] css hot updated: /global.css',
)
await untilUpdated(() => getColor(el), 'rgb(204, 0, 0)')
})
})