/
config.spec.ts
166 lines (145 loc) · 3.55 KB
/
config.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
import { InlineConfig } from '..'
import {
mergeConfig,
resolveConfig,
UserConfigExport,
resolveEnvPrefix,
UserConfig
} from '../config'
describe('mergeConfig', () => {
test('handles configs with different alias schemas', () => {
const baseConfig: UserConfigExport = {
resolve: {
alias: [
{
find: 'foo',
replacement: 'foo-value'
}
]
}
}
const newConfig: UserConfigExport = {
resolve: {
alias: {
bar: 'bar-value',
baz: 'baz-value'
}
}
}
const mergedConfig: UserConfigExport = {
resolve: {
alias: [
{
find: 'foo',
replacement: 'foo-value'
},
{
find: 'bar',
replacement: 'bar-value'
},
{
find: 'baz',
replacement: 'baz-value'
}
]
}
}
expect(mergeConfig(baseConfig, newConfig)).toEqual(mergedConfig)
})
test('handles assetsInclude', () => {
const baseConfig: UserConfigExport = {
assetsInclude: 'some-string'
}
const newConfig: UserConfigExport = {
assetsInclude: ['some-other-string', /regexp?/]
}
const mergedConfig: UserConfigExport = {
assetsInclude: ['some-string', 'some-other-string', /regexp?/]
}
expect(mergeConfig(baseConfig, newConfig)).toEqual(mergedConfig)
})
test('not handles alias not under `resolve`', () => {
const baseConfig = {
custom: {
alias: {
bar: 'bar-value',
baz: 'baz-value'
}
}
}
const newConfig = {
custom: {
alias: {
bar: 'bar-value-2',
foo: 'foo-value'
}
}
}
const mergedConfig = {
custom: {
alias: {
bar: 'bar-value-2',
baz: 'baz-value',
foo: 'foo-value'
}
}
}
expect(mergeConfig(baseConfig, newConfig)).toEqual(mergedConfig)
})
})
describe('resolveConfig', () => {
beforeAll(() => {
// silence deprecation warning
jest.spyOn(console, 'warn').mockImplementation(() => {})
})
afterAll(() => {
jest.clearAllMocks()
})
test('copies optimizeDeps.keepNames to esbuildOptions.keepNames', async () => {
const config: InlineConfig = {
optimizeDeps: {
keepNames: false
}
}
expect(await resolveConfig(config, 'serve')).toMatchObject({
optimizeDeps: {
esbuildOptions: {
keepNames: false
}
}
})
})
test('uses esbuildOptions.keepNames if set', async () => {
const config: InlineConfig = {
optimizeDeps: {
keepNames: true,
esbuildOptions: {
keepNames: false
}
}
}
expect(await resolveConfig(config, 'serve')).toMatchObject({
optimizeDeps: {
esbuildOptions: {
keepNames: false
}
}
})
})
})
describe('resolveEnvPrefix', () => {
test(`use 'VITE_' as default value`, () => {
const config: UserConfig = {}
expect(resolveEnvPrefix(config)).toMatchObject(['VITE_'])
})
test(`throw error if envPrefix contains ''`, () => {
let config: UserConfig = { envPrefix: '' }
expect(() => resolveEnvPrefix(config)).toThrow()
config = { envPrefix: ['', 'CUSTOM_'] }
expect(() => resolveEnvPrefix(config)).toThrow()
})
test('should work correctly for valid envPrefix value', () => {
const config: UserConfig = { envPrefix: [' ', 'CUSTOM_'] }
expect(resolveEnvPrefix(config)).toMatchObject([' ', 'CUSTOM_'])
})
})