/
utils.spec.ts
184 lines (159 loc) · 4.6 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { describe, expect, test } from 'vitest'
import {
asyncFlatten,
getHash,
getLocalhostAddressIfDiffersFromDNS,
getPotentialTsSrcPaths,
injectQuery,
isWindows,
resolveHostname
} from '../utils'
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 localhost', async () => {
const resolved = await getLocalhostAddressIfDiffersFromDNS()
expect(await resolveHostname(undefined)).toEqual({
host: 'localhost',
name: resolved ?? 'localhost',
implicit: true
})
})
test('accepts localhost', async () => {
const resolved = await getLocalhostAddressIfDiffersFromDNS()
expect(await resolveHostname('localhost')).toEqual({
host: 'localhost',
name: resolved ?? 'localhost',
implicit: false
})
})
test('accepts 0.0.0.0', async () => {
expect(await resolveHostname('0.0.0.0')).toEqual({
host: '0.0.0.0',
name: 'localhost',
implicit: false
})
})
test('accepts ::', async () => {
expect(await resolveHostname('::')).toEqual({
host: '::',
name: 'localhost',
implicit: false
})
})
test('accepts 0000:0000:0000:0000:0000:0000:0000:0000', async () => {
expect(
await resolveHostname('0000:0000:0000:0000:0000:0000:0000:0000')
).toEqual({
host: '0000:0000:0000:0000:0000:0000:0000:0000',
name: 'localhost',
implicit: false
})
})
})
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'
])
})
describe('getHash', () => {
test('8-digit hex', () => {
const hash = getHash(Buffer.alloc(0))
expect(hash).toMatch(/^[\da-f]{8}$/)
})
})
describe('asyncFlatten', () => {
test('plain array', async () => {
const arr = await asyncFlatten([1, 2, 3])
expect(arr).toEqual([1, 2, 3])
})
test('nested array', async () => {
const arr = await asyncFlatten([1, 2, 3, [4, 5, 6]])
expect(arr).toEqual([1, 2, 3, 4, 5, 6])
})
test('nested falsy array', async () => {
const arr = await asyncFlatten([1, 2, false, [4, null, undefined]])
expect(arr).toEqual([1, 2, false, 4, null, undefined])
})
test('plain promise array', async () => {
const arr = await asyncFlatten([1, 2, Promise.resolve(3)])
expect(arr).toEqual([1, 2, 3])
})
test('nested promise array', async () => {
const arr = await asyncFlatten([
1,
2,
Promise.resolve(3),
Promise.resolve([4, 5, 6])
])
expect(arr).toEqual([1, 2, 3, 4, 5, 6])
})
test('2x nested promise array', async () => {
const arr = await asyncFlatten([
1,
2,
Promise.resolve(3),
Promise.resolve([4, 5, Promise.resolve(6), Promise.resolve([7, 8, 9])])
])
expect(arr).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9])
})
})