1 | import tap from 'tap'
|
2 | import path from 'path'
|
3 |
|
4 | import { createConfig, removeConfigValues, getConfigFile, _clearCurrentConfig } from '../lib/config'
|
5 | import { Env } from '../lib/constants'
|
6 |
|
7 | tap.test('config - defaults', async (t) => {
|
8 | _clearCurrentConfig()
|
9 |
|
10 | const config = await createConfig({
|
11 | cli: {
|
12 | files: 'app/*.js',
|
13 | output: 'dist',
|
14 | },
|
15 | })
|
16 |
|
17 | t.equal(config.env, Env.PRODUCTION)
|
18 | t.ok(!!config.configFilepath)
|
19 |
|
20 | t.ok(path.isAbsolute(config.files[0]))
|
21 |
|
22 | t.ok(path.isAbsolute(config.files[0]))
|
23 | t.ok(path.isAbsolute(config.output))
|
24 | t.ok(path.isAbsolute(config.assets))
|
25 |
|
26 | t.ok(config.output.includes('dist'))
|
27 | })
|
28 |
|
29 | tap.test('config - no files', async (t) => {
|
30 | _clearCurrentConfig()
|
31 |
|
32 | const config = await createConfig({
|
33 | cli: {},
|
34 | })
|
35 |
|
36 | t.same(config.files, [])
|
37 | })
|
38 |
|
39 | tap.test('config - output', async (t) => {
|
40 | _clearCurrentConfig()
|
41 |
|
42 | const config = await createConfig({
|
43 | cli: {
|
44 | files: 'app/*.js',
|
45 | output: 'dist',
|
46 | },
|
47 | })
|
48 |
|
49 | t.ok(path.isAbsolute(config.output))
|
50 | t.ok(config.output.includes('dist'))
|
51 | })
|
52 |
|
53 | tap.test('config - assets', async (t) => {
|
54 | _clearCurrentConfig()
|
55 |
|
56 | const config = await createConfig({
|
57 | cli: {
|
58 | files: 'app/*.js',
|
59 | assets: 'assets',
|
60 | },
|
61 | })
|
62 |
|
63 | t.ok(path.isAbsolute(config.assets))
|
64 | t.ok(config.assets.includes('assets'))
|
65 | })
|
66 |
|
67 | tap.test('config - staticOutputDir', async (t) => {
|
68 | _clearCurrentConfig()
|
69 |
|
70 | const config = await createConfig({
|
71 | cli: {
|
72 | files: 'app/*.js',
|
73 | assets: 'assets',
|
74 | },
|
75 | })
|
76 |
|
77 | t.ok(path.isAbsolute(config.staticOutputDir))
|
78 | })
|
79 |
|
80 | tap.test('config - getConfigFile no config, no log or exit', async (t) => {
|
81 | const { getConfigFile } = require('proxyquire')('../lib/config', {
|
82 | './log': {
|
83 | error() {
|
84 | t.fail()
|
85 | },
|
86 | },
|
87 | })
|
88 |
|
89 | const configFile = getConfigFile()
|
90 |
|
91 | t.same(configFile, {})
|
92 | })
|
93 |
|
94 | tap.test('config - getConfigFile exists', async (t) => {
|
95 | t.testdir({
|
96 | 'presta.config.js': `export const files = 'path/to/*.js'`,
|
97 | })
|
98 |
|
99 | const configFilepath = path.join(t.testdirName, './presta.config.js')
|
100 | const configFile = getConfigFile(configFilepath)
|
101 |
|
102 | t.ok(configFile.files)
|
103 | })
|
104 |
|
105 | tap.test('config - getConfigFile throws, syntax error', async (t) => {
|
106 | let called = false
|
107 |
|
108 | const { getConfigFile } = require('proxyquire')('../lib/config', {
|
109 | './log': {
|
110 | error() {
|
111 | called = true
|
112 | },
|
113 | },
|
114 | })
|
115 |
|
116 | t.testdir({
|
117 | 'presta.config.js': `export const files = 'path/to/*.js`,
|
118 | })
|
119 |
|
120 | const configFilepath = path.join(t.testdirName, './presta.config.js')
|
121 | const configFile = getConfigFile(configFilepath)
|
122 |
|
123 | t.same(configFile, {})
|
124 | t.ok(called)
|
125 | })
|
126 |
|
127 | tap.test('config - getConfigFile throws and exits', (t) => {
|
128 | t.testdir({
|
129 | 'presta.config.js': `export const files = 'path/to/*.js`,
|
130 | })
|
131 |
|
132 | const exit = process.exit
|
133 |
|
134 |
|
135 | process.exit = (code: int) => {
|
136 | t.equal(code, 1)
|
137 | t.pass()
|
138 | t.end()
|
139 | }
|
140 |
|
141 | const configFilepath = path.join(t.testdirName, './presta.config.js')
|
142 | getConfigFile(configFilepath, true)
|
143 |
|
144 |
|
145 | process.exit = exit
|
146 | })
|
147 |
|
148 | tap.test('config - getConfigFile throws and does not exit with no file', async (t) => {
|
149 | const configFile = getConfigFile(undefined, true)
|
150 | t.same(configFile, {})
|
151 | })
|
152 |
|
153 | tap.test('config - picks up default file if present', async (t) => {
|
154 | _clearCurrentConfig()
|
155 |
|
156 | const file = 'file.js'
|
157 | const output = 'output'
|
158 |
|
159 | t.testdir({
|
160 | 'presta.config.js': `const files = '${file}'; const output = '${output}'; module.exports = { files, output }`,
|
161 | })
|
162 |
|
163 | const configFilepath = path.join(t.testdirName, './presta.config.js')
|
164 | const configFile = getConfigFile(configFilepath)
|
165 | const config = await createConfig({
|
166 | config: configFile,
|
167 | cli: {},
|
168 | })
|
169 |
|
170 | t.ok(config.files[0].includes(file))
|
171 | t.ok(config.output.includes(output))
|
172 | })
|
173 |
|
174 | tap.test('config - overriden by CLI args', async (t) => {
|
175 | _clearCurrentConfig()
|
176 |
|
177 | t.testdir({
|
178 | 'presta.config.js': `const files = 'file.js'; const output = 'output'; module.exports = { files, output }`,
|
179 | })
|
180 |
|
181 | const configFilepath = path.join(t.testdirName, './presta.config.js')
|
182 | const configFile = getConfigFile(configFilepath)
|
183 | const config = await createConfig({
|
184 | config: configFile,
|
185 | cli: {
|
186 | files: 'foo.js',
|
187 | output: 'out',
|
188 | },
|
189 | })
|
190 |
|
191 | t.ok(config.files[0].includes('foo.js'))
|
192 | t.ok(config.output.includes('out'))
|
193 | })
|
194 |
|
195 | tap.test('config - file is merged with internal config', async (t) => {
|
196 | _clearCurrentConfig()
|
197 |
|
198 | const config = await createConfig({
|
199 | config: {
|
200 | output: 'out',
|
201 | },
|
202 | })
|
203 |
|
204 | t.ok(config.output.includes('out'))
|
205 | })
|
206 |
|
207 | tap.test('config - merging updates', async (t) => {
|
208 | _clearCurrentConfig()
|
209 |
|
210 | const config = await createConfig({
|
211 | config: {
|
212 | output: 'output',
|
213 | },
|
214 | })
|
215 |
|
216 | t.ok(config.output.includes('output'))
|
217 |
|
218 | const merged = await createConfig({
|
219 | config: {
|
220 | output: 'output',
|
221 | assets: 'assets',
|
222 | },
|
223 | })
|
224 |
|
225 | t.ok(merged.assets.includes('assets'))
|
226 | })
|
227 |
|
228 | tap.test('config - removeConfigValues', async (t) => {
|
229 | _clearCurrentConfig()
|
230 |
|
231 | const config = await createConfig({
|
232 | config: {
|
233 | output: 'output',
|
234 | },
|
235 | })
|
236 |
|
237 | t.ok(config.output.includes('output'))
|
238 |
|
239 | const unmerged = await removeConfigValues()
|
240 |
|
241 | t.ok(unmerged.output.includes('build'))
|
242 | })
|
243 |
|
244 | tap.test('config - hooks', async (t) => {
|
245 | _clearCurrentConfig()
|
246 |
|
247 | const config = await createConfig({})
|
248 |
|
249 | config.hooks.onPostBuild(() => {
|
250 | t.pass()
|
251 | t.end()
|
252 | })
|
253 |
|
254 |
|
255 | config.hooks.emitPostBuild()
|
256 | })
|
257 |
|
258 | tap.test('config - plugins', async (t) => {
|
259 | _clearCurrentConfig()
|
260 |
|
261 | await createConfig({
|
262 | config: {
|
263 | plugins: [
|
264 | () => {
|
265 | t.pass()
|
266 | t.end()
|
267 | },
|
268 | ],
|
269 | },
|
270 | })
|
271 | })
|
272 |
|
273 | tap.test('config - plugin error', async (t) => {
|
274 | _clearCurrentConfig()
|
275 |
|
276 | let called = false
|
277 |
|
278 | const { createConfig } = require('proxyquire')('../lib/config', {
|
279 | './log': {
|
280 | error() {
|
281 | called = true
|
282 | },
|
283 | },
|
284 | })
|
285 |
|
286 | await createConfig({
|
287 | config: {
|
288 | plugins: [
|
289 | () => {
|
290 | throw Error()
|
291 | },
|
292 | ],
|
293 | },
|
294 | })
|
295 |
|
296 | t.ok(called)
|
297 | })
|