-
-
Notifications
You must be signed in to change notification settings - Fork 263
Expand file tree
/
Copy pathutils.test.ts
More file actions
61 lines (57 loc) · 1.76 KB
/
Copy pathutils.test.ts
File metadata and controls
61 lines (57 loc) · 1.76 KB
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
import type { DevEnvironment } from 'vite'
import { describe, expect, it } from 'vitest'
import { hashString, normalizeViteImportAnalysisUrl } from './utils'
function createEnvironment(options?: {
consumer?: 'client' | 'server'
timestamp?: number
}) {
return {
config: {
root: '/root',
consumer: options?.consumer ?? 'server',
},
moduleGraph: {
getModuleById: () =>
options?.timestamp
? { lastHMRTimestamp: options.timestamp }
: undefined,
},
} as unknown as DevEnvironment
}
describe(hashString, () => {
it('returns a stable short sha256 hash', () => {
expect(hashString('test')).toBe('9f86d081884c')
expect(hashString('test')).toBe(hashString('test'))
expect(hashString('other')).not.toBe(hashString('test'))
})
})
describe(normalizeViteImportAnalysisUrl, () => {
it('normalizes root files and virtual ids', () => {
const environment = createEnvironment()
expect(
normalizeViteImportAnalysisUrl(environment, '/root/src/action.ts'),
).toBe('/src/action.ts')
expect(normalizeViteImportAnalysisUrl(environment, 'virtual:action')).toBe(
'/@id/virtual:action',
)
})
it('injects HMR timestamps for client consumers or when requested', () => {
const id = '/root/src/action.ts'
expect(
normalizeViteImportAnalysisUrl(
createEnvironment({ consumer: 'client', timestamp: 123 }),
id,
),
).toBe('/src/action.ts?t=123')
expect(
normalizeViteImportAnalysisUrl(
createEnvironment({ timestamp: 456 }),
id,
{ injectHMRTimestamp: true },
),
).toBe('/src/action.ts?t=456')
expect(
normalizeViteImportAnalysisUrl(createEnvironment({ timestamp: 789 }), id),
).toBe('/src/action.ts')
})
})