forked from cypress-io/cypress-example-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-examples.js
177 lines (145 loc) · 4.15 KB
/
test-examples.js
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
// a script that goes through each example folder
// and runs tests in each one
// Useful when one needs to test everything in this repo using a single command
/* eslint-disable no-console */
const globby = require('globby')
const bluebird = require('bluebird')
const tb = require('terminal-banner').terminalBanner
const execa = require('execa')
const pluralize = require('pluralize')
const { resolve, join } = require('path')
const fs = require('fs')
const arg = require('arg')
const la = require('lazy-ass')
const is = require('check-more-types')
// to run "npm run test:ci:chrome" scripts in each example
// run this script with "--chrome" CLI flag
const args = arg({
'--chrome': Boolean,
'--brave': Boolean,
'--firefox': Boolean,
'--windows': Boolean,
'--chunk': Number,
'--total-chunks': Number,
// TODO switch from separate --chrome|--brave|--firefox
// to using "--browser chrome" or "--browser firefox" argument
'--browser': String,
})
// fill default values
args['--chunk'] = args['--chunk'] || 0
args['--total-chunks'] = args['--total-chunks'] || 1
console.log('args', args)
// default NPM script name
let scriptName = 'test:ci'
if (args['--chrome']) {
scriptName = 'test:ci:chrome'
}
if (args['--brave']) {
scriptName = 'test:ci:brave'
}
if (args['--firefox']) {
scriptName = 'test:ci:firefox'
}
if (args['--windows']) {
scriptName = 'test:ci:windows'
}
console.log('script name "%s"', scriptName)
const getExamples = () => {
return globby('examples/*', { onlyFiles: false, expandDirectories: false })
}
const printFolders = (folders) => {
console.log(
'Will be running tests in %s',
pluralize('folder', folders.length, true)
)
folders.forEach((name) => console.log(' -', name))
}
const hasPackageScriptName = (folder) => {
const filename = resolve(join(folder, 'package.json'))
if (!fs.existsSync(filename)) {
return false
}
const { scripts } = require(filename)
return scripts && scripts[scriptName]
}
const testExample = (folder) => {
tb(`Testing ${folder}`)
// runs the same script command in each folder
// maybe if there is no script, should skip it?
const filename = resolve(join(folder, 'package.json'))
if (!fs.existsSync(filename)) {
console.log('cannot find file "%s"', filename)
console.log('skipping...')
return
}
const { scripts } = require(filename)
if (!scripts || !scripts[scriptName]) {
console.log('file %s does not have script "%s"', filename, scriptName)
console.log('skipping...')
return
}
return execa('npm', ['run', scriptName], { stdio: 'inherit', cwd: folder })
}
const testExamples = (folders) => {
return bluebird.mapSeries(folders, testExample)
}
const filterSomeFolders = (folders) => {
// if you want to filter some folders by name for example
// we want to skip recipes that have outside service dependencies
// like Codepen demos
const skipCodepenFolder = (name) => {
if (name.includes('codepen')) {
return false
}
return true
}
return folders.filter(skipCodepenFolder)
}
const filterByChunk = (chunk, totalChunks) => {
la(
is.number(chunk) && chunk >= 0,
'expected chunk to be a number >= 0',
chunk
)
la(
is.number(totalChunks) && totalChunks > 0,
'expected total chunks to be >= 1',
totalChunks
)
la(
chunk < totalChunks,
'expected chunk',
chunk,
'to be less than total chunks',
totalChunks
)
return function filterFolders (folders) {
const chunkLength = Math.ceil(folders.length / totalChunks)
console.log(
'Total folders %d chunks %d chunk size %d current chunk %d',
folders.length,
totalChunks,
chunkLength,
chunk
)
return folders.slice(chunkLength * chunk, chunkLength * (chunk + 1))
}
}
/**
* Leaves only folders that have package.json with desired script name
*/
const filterByScriptName = (folders) => {
return folders.filter(hasPackageScriptName)
}
bluebird
.try(getExamples)
.then((list) => list.sort())
.then(filterByScriptName)
.then(filterSomeFolders)
.then(filterByChunk(args['--chunk'], args['--total-chunks']))
.tap(printFolders)
.then(testExamples)
.catch((e) => {
console.error(e.message)
process.exit(1)
})