/
utils.spec.ts
100 lines (87 loc) · 2.47 KB
/
utils.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
import {
getPotentialTsSrcPaths,
injectQuery,
isWindows,
resolveHostname
} from '../utils'
import { describe, test, expect } from 'vitest'
describe('injectQuery', () => {
if (isWindows) {
// this test will work incorrectly on unix systems
test('normalize windows path', () => {
expect(injectQuery('C:\\User\\Vite\\Project', 'direct')).toEqual(
'C:/User/Vite/Project?direct'
)
})
}
test('path with multiple spaces', () => {
expect(injectQuery('/usr/vite/path with space', 'direct')).toEqual(
'/usr/vite/path with space?direct'
)
})
test('path with multiple % characters', () => {
expect(injectQuery('/usr/vite/not%20a%20space', 'direct')).toEqual(
'/usr/vite/not%20a%20space?direct'
)
})
test('path with %25', () => {
expect(injectQuery('/usr/vite/%25hello%25', 'direct')).toEqual(
'/usr/vite/%25hello%25?direct'
)
})
test('path with unicode', () => {
expect(injectQuery('/usr/vite/東京', 'direct')).toEqual(
'/usr/vite/東京?direct'
)
})
test('path with unicode, space, and %', () => {
expect(injectQuery('/usr/vite/東京 %20 hello', 'direct')).toEqual(
'/usr/vite/東京 %20 hello?direct'
)
})
})
describe('resolveHostname', () => {
test('defaults to 127.0.0.1', () => {
expect(resolveHostname(undefined)).toEqual({
host: '127.0.0.1',
name: 'localhost'
})
})
test('accepts localhost', () => {
expect(resolveHostname('localhost')).toEqual({
host: 'localhost',
name: 'localhost'
})
})
})
test('ts import of file with .js extension', () => {
expect(getPotentialTsSrcPaths('test-file.js')).toEqual([
'test-file.ts',
'test-file.tsx'
])
})
test('ts import of file with .jsx extension', () => {
expect(getPotentialTsSrcPaths('test-file.jsx')).toEqual(['test-file.tsx'])
})
test('ts import of file .mjs,.cjs extension', () => {
expect(getPotentialTsSrcPaths('test-file.cjs')).toEqual([
'test-file.cts',
'test-file.ctsx'
])
expect(getPotentialTsSrcPaths('test-file.mjs')).toEqual([
'test-file.mts',
'test-file.mtsx'
])
})
test('ts import of file with .js before extension', () => {
expect(getPotentialTsSrcPaths('test-file.js.js')).toEqual([
'test-file.js.ts',
'test-file.js.tsx'
])
})
test('ts import of file with .js and query param', () => {
expect(getPotentialTsSrcPaths('test-file.js.js?lee=123')).toEqual([
'test-file.js.ts?lee=123',
'test-file.js.tsx?lee=123'
])
})