forked from kentcdodds/testing-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpackage-scripts.js
More file actions
479 lines (460 loc) · 13.5 KB
/
Copy pathpackage-scripts.js
File metadata and controls
479 lines (460 loc) · 13.5 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
const path = require('path')
const {
concurrent,
series,
runInNewWindow,
crossEnv,
rimraf,
commonTags,
ifWindows,
} = require('nps-utils')
const {oneLine} = commonTags
const hiddenFromHelp = true
const inApi = (...scripts) => series('cd api', ...scripts, 'cd ..')
const inClient = (...scripts) => series('cd client', ...scripts, 'cd ..')
const delay = s => ifWindows(`timeout ${s}`, `sleep ${s}`)
const ignoreOutput = s =>
`echo ${s} && ${s} ${ifWindows('> NUL', '&>/dev/null')}`
const ignoreError = s => `${s} || true`
const splitVerifyDescription = oneLine`
This verifies that the final version actually passes the tests.
To do this, we first use split-guide and place the final version
in the place of the exercises, then we run the tests, then
we re-run the split to have the exercises where they should be.
`
module.exports = {
scripts: {
default: {
description: 'starts mongo, the API, and the client in production mode',
script: concurrent.nps('mongo.silent', 'api', 'client'),
},
separateStart: {
hiddenFromHelp,
description: 'Runs all the start scripts in individual terminals',
script: series(
runInNewWindow.nps('mongo'),
runInNewWindow.nps('api'),
runInNewWindow.nps('client'),
),
},
mongo: {
description: oneLine`
Create the .mongo-db dir and start the mongod process.
This also ignores all the output. If you want to see
the output, then run: "npm start mongo.start" instead.
`,
script: series('mkdirp .mongo-db', 'nps mongo.start'),
silent: {
description: 'Starts the mongo server and ignores the output',
script: ignoreOutput('nps mongo'),
},
start: {
description: oneLine`
starts the mongodb server with the
database pointing to ./mongo-db
`,
script: `mongod --dbpath "${path.join(__dirname, './.mongo-db')}"`,
},
stop: {
description: 'stops the mongo server',
script: 'mongo admin --eval "db.shutdownServer()"',
},
},
api: getApiScripts(),
client: {
default: {
description: 'start the production client server',
script: inClient('npm start --silent -- "default 8080"'),
},
test: getTestScripts('client'),
demo: getDemoScripts('client'),
unit: 'nps client.test.unit.watch',
integration: 'nps client.test.integration.watch',
},
build: {
default: {
description: 'Build both the client and the api',
script: concurrent.nps('build.api', 'build.client'),
},
api: {
script: series('cd api', 'npm start build --silent'),
description: 'run the api build',
},
client: {
script: series('cd client', 'npm start build --silent'),
description: 'run the client build',
},
},
dev: {
description: 'starts everything in dev mode',
script: concurrent.nps('dev.mongo', 'dev.client', 'dev.api'),
separate: {
hiddenFromHelp,
description: 'Runs all of the dev scripts in individual terminals',
script: series(
runInNewWindow.nps('dev.mongo --silent'),
runInNewWindow.nps('npm start dev.client --silent'),
runInNewWindow.nps('dev.api --silent'),
),
},
// dev is the same as live for mongo for now...
mongo: {
description: oneLine`
starts the dev-mode mongo server
(same as production mode for now)
`,
script: 'nps mongo',
silent: 'nps mongo.silent',
},
client: {
description: 'starts the client server in dev mode',
script: series('cd client', 'npm start dev --silent'),
},
api: {
description: 'starts the api server in dev mode',
script: series('cd api', 'npm start dev --silent'),
},
},
e2e: getE2EScripts(),
test: {
description: 'run the api and client tests in parallel',
script: concurrent.nps('api.test', 'client.test'),
},
lint: {
hiddenFromHelp,
script: 'eslint . --cache',
description: 'lint project files',
},
format: {
hiddenFromHelp,
script: 'prettier --write "**/*.+(js|json|less|css|ts|md)"',
description: 'autoformat project files',
},
validate: {
description: 'validates that things are set up properly',
script: series(
'nps lint',
concurrent.nps(
'split.api.verify',
'split.client.verify',
'split.e2e.verify',
),
),
},
split: {
default: {
script: concurrent.nps('split.api', 'split.client', 'split.e2e'),
hiddenFromHelp,
},
verify: {
script: concurrent.nps(
'split.api.verify',
'split.client.verify',
'split.e2e.verify',
),
hiddenFromHelp,
},
client: {
default: {
script: series(
rimraf('client-final'),
oneLine`
split-guide generate
--no-clean
--templates-dir other/templates/client
--exercises-dir client
--exercises-final-dir client-final
`,
),
hiddenFromHelp,
},
verify: {
hiddenFromHelp,
description: splitVerifyDescription,
script: series(
rimraf('client-final', 'node_modules/.tmp/client'),
oneLine`
split-guide generate
--no-clean
--templates-dir other/templates/client
--exercises-dir node_modules/.tmp/client
--exercises-final-dir client
`,
concurrent.nps('client.test', 'client.demo'),
'nps split.client',
),
},
},
api: {
default: {
script: series(
rimraf('api-final'),
oneLine`
split-guide generate
--no-clean
--templates-dir other/templates/api
--exercises-dir api
--exercises-final-dir api-final
`,
),
hiddenFromHelp,
},
verify: {
hiddenFromHelp,
description: splitVerifyDescription,
script: series(
rimraf('api-final', 'node_modules/.tmp/api'),
oneLine`
split-guide generate
--no-clean
--templates-dir other/templates/api
--exercises-dir node_modules/.tmp/api
--exercises-final-dir api
`,
concurrent.nps('api.test', 'api.demo'),
'nps split.api',
),
},
},
e2e: {
default: {
script: series(
rimraf('cypress-final'),
oneLine`
split-guide generate
--no-clean
--templates-dir other/templates/cypress
--exercises-dir cypress
--exercises-final-dir cypress-final
`,
),
hiddenFromHelp,
},
verify: {
hiddenFromHelp,
description: splitVerifyDescription,
script: series(
rimraf('cypress-final', 'node_modules/.tmp/cypress'),
oneLine`
split-guide generate
--no-clean
--templates-dir other/templates/cypress
--exercises-dir node_modules/.tmp/cypress
--exercises-final-dir cypress
`,
'nps e2e',
'nps split.e2e',
),
},
},
},
contributors: {
add: {
description: oneLine`
Prompt to add a new contributor
to the contributors table
`,
script: 'all-contributors add',
},
generate: {
description: 'regenerates the contributors table',
script: 'all-contributors generate',
},
},
},
}
function getE2EScripts() {
const allScripts = ['client', 'cypress', 'mongo', 'api']
const cypresslessScripts = allScripts.filter(x => x !== 'cypress')
const devMap = s => `"${crossEnv(`STDIO=inherit nps e2e.dev.${s}`)}"`
const runMap = s => `"nps e2e.run.${s}"`
const {run, dev} = allScripts.reduce(
(runDev, scriptName) => {
const script = `node ./scripts/e2e-${scriptName}`
runDev.run[scriptName] = {
script,
hiddenFromHelp,
}
runDev.dev[scriptName] = {
script: crossEnv(`E2E_DEV=true ${script}`),
hiddenFromHelp,
}
return runDev
},
{run: {}, dev: {}},
)
const defaultScript = getDefaultScript(
allScripts,
runMap,
'--kill-others --success first',
)
const loadDatabase = {
script: crossEnv(
oneLine`
MONGO_PORT=27018
MONGO_PATH=./.e2e/mongo-db
MONGODB_URI="mongodb://localhost:27018/conduit"
node ./scripts/load-database.js
`,
),
hiddenFromHelp,
}
Object.assign(dev, {
default: {
description: 'Run the e2e services and cypress in dev mode.',
script: getConcurrentScript(allScripts, devMap),
},
services: {
hiddenFromHelp,
description: oneLine`
starts all the services.
Use if you already have cypress running
`,
script: getConcurrentScript(cypresslessScripts, devMap),
},
})
const noBuild = {
script: getBuildessScript(
allScripts,
runMap,
'--kill-others --success first',
),
hiddenFromHelp,
}
return {
default: {
script: defaultScript,
description: oneLine`
Runs everything you need for a full E2E test run.
Note that there are various combinations of these
scripts which you can run. See the child scripts
of e2e. Also note that if you specify the
environment variable of \`STDIO=inherit\`, you will
be able to see the output of services which could
bet quite handy.
`,
},
loadDatabase,
run,
dev,
noBuild,
}
function getDefaultScript(scripts, prefix, flags = '') {
const prepare = concurrent.nps('e2e.loadDatabase', 'build')
return series(prepare, getConcurrentScript(scripts, prefix, flags))
}
function getBuildessScript(scripts, prefix, flags) {
return series(
'nps e2e.loadDatabase',
getConcurrentScript(scripts, prefix, flags),
)
}
function getConcurrentScript(scripts, map, flags = '') {
const npsScripts = scripts.map(map)
return oneLine`
concurrently
${flags}
--prefix-colors "bgGreen.bold,bgBlue.bold,bgMagenta.bold,bgCyan.bold"
--prefix "[{name}]"
--names "${scripts.join(',')}"
${npsScripts.join(' ')}
`
}
}
function getTestScripts(dir) {
const inDir = (...scripts) => series(`cd ${dir}`, ...scripts, 'cd ..')
return {
default: {
description: `run all the ${dir} tests`,
script: concurrent.nps(`${dir}.test.unit`, `${dir}.test.integration`),
},
unit: {
default: {
description: `run the ${dir} unit tests`,
script: inDir('npm start test.unit --silent'),
},
watch: {
description: `run the ${dir} unit tests in watch mode`,
script: inDir('npm start test.unit.watch --silent'),
},
},
integration: {
default: {
description: `run the ${dir} integration tests`,
script: inDir('npm start test.integration --silent'),
},
watch: {
description: `run the ${dir} integration tests in watch mode`,
script: inDir('npm start test.integration.watch --silent'),
},
},
}
}
function getDemoScripts(dir) {
const inDir = (...scripts) => series(`cd ${dir}`, ...scripts, 'cd ..')
return {
default: {
description: `run all the ${dir} demo tests`,
script: concurrent.nps(
`${dir}.demo.unit.single`,
`${dir}.demo.integration.single`,
),
},
unit: {
single: {
description: `run the ${dir} unit demo tests`,
script: inDir('npm start demo.unit --silent'),
},
default: {
description: `run the ${dir} demo unit tests in watch mode`,
script: inDir('npm start demo.unit.watch --silent'),
},
},
integration: {
single: {
description: `run the ${dir} integration demo tests`,
script: inDir('npm start demo.integration --silent'),
},
default: {
description: `run the ${dir} demo integration tests in watch mode`,
script: inDir('npm start demo.integration.watch --silent'),
},
},
}
}
function getApiScripts() {
const testScripts = getTestScripts('api')
return {
default: {
description: 'start the api server in production mode',
script: inApi('npm start --silent'),
},
test: Object.assign(testScripts, {
integration: {
default: {
description: 'Starts mongo, then starts the integration tests',
script: series(
// make sure that it's not running before we start it
ignoreError(ignoreOutput('nps mongo.stop')),
oneLine`
concurrently
--kill-others
--success first
--prefix "[{name}]"
--names dev.mongo.silent,dev.api,api.test.integration
"nps dev.mongo.silent"
"
${delay(2)} &&
${inApi('npm start test.integration --silent')}
"
`,
),
},
watch: testScripts.integration.watch,
},
}),
demo: getDemoScripts('api'),
unit: 'nps api.test.unit.watch',
integration: 'nps api.test.integration.watch',
}
}