forked from storybookjs/storybook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
executable file
·202 lines (187 loc) · 5.73 KB
/
test.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
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
#!/usr/bin/env node
const inquirer = require('inquirer');
const program = require('commander');
const childProcess = require('child_process');
const chalk = require('chalk');
const log = require('npmlog');
const path = require('path');
log.heading = 'storybook';
const prefix = 'test';
log.addLevel('aborted', 3001, { fg: 'red', bold: true });
const spawn = command => {
const out = childProcess.spawnSync(`${command}`, {
shell: true,
stdio: 'inherit',
});
if (out.status !== 0) {
process.exit(out.status);
}
return out;
};
const main = program.version('3.0.0').option('--all', `Test everything ${chalk.gray('(all)')}`);
const createProject = ({ defaultValue, option, name, projectLocation, isJest }) => ({
value: false,
defaultValue: defaultValue || false,
option: option || undefined,
name: name || 'unnamed task',
projectLocation,
isJest,
});
const createOption = ({ defaultValue, option, name, extraParam }) => ({
value: false,
defaultValue: defaultValue || false,
option: option || undefined,
name: name || 'unnamed task',
extraParam,
});
const tasks = {
core: createProject({
name: `Core & React & Vue & Polymer & Angular & Svelte ${chalk.gray('(core)')}`,
defaultValue: true,
option: '--core',
projectLocation: path.join(__dirname, '..'),
isJest: true,
}),
'react-native-vanilla': createProject({
name: `React-Native example ${chalk.gray('(react-native-vanilla)')}`,
defaultValue: true,
option: '--reactnative',
projectLocation: path.join(__dirname, '..', 'examples/react-native-vanilla'),
isJest: true,
}),
image: createProject({
name: `Image snapshots for Official storybook ${chalk.gray('(image)')}`,
defaultValue: false,
option: '--image',
projectLocation: path.join(__dirname, '..', 'examples/official-storybook/image-snapshots'),
isJest: true,
}),
// 'crna-kitchen-sink': createProject({
// name: `React-Native-App example ${chalk.gray('(crna-kitchen-sink)')} ${chalk.red(
// '[not implemented yet]'
// )}`,
// defaultValue: false,
// option: '--reactnativeapp',
// projectLocation: './examples/crna-kitchen-sink',
// isJest: true,
// }),
cli: createProject({
name: `Command Line Interface ${chalk.gray('(cli)')}`,
defaultValue: false,
option: '--cli',
projectLocation: './lib/cli',
}),
watchmode: createOption({
name: `Run in watch-mode ${chalk.gray('(watchmode)')}`,
defaultValue: false,
option: '--watch',
extraParam: '--watch',
}),
coverage: createOption({
name: `Output coverage reports ${chalk.gray('(coverage)')}`,
defaultValue: false,
option: '--coverage',
extraParam: '--coverage',
}),
runInBand: createOption({
name: `Run all tests serially in the current process ${chalk.gray('(runInBand)')}`,
defaultValue: false,
option: '--runInBand',
extraParam: '--runInBand',
}),
update: createOption({
name: `Update all snapshots ${chalk.gray('(update)')}`,
defaultValue: false,
option: '--update',
extraParam: '-u --updateSnapshot',
}),
teamcity: createOption({
name: `Use TeamCity reporter`,
defaultValue: false,
option: '--teamcity',
extraParam: '--testResultsProcessor=jest-teamcity-reporter',
}),
};
const getProjects = list => {
const filtered = list.filter(key => key.projectLocation);
if (filtered.length > 0) {
return filtered;
}
// if list would have been empty, we run with default projects
return Object.keys(tasks)
.map(key => tasks[key])
.filter(key => key.projectLocation && key.defaultValue);
};
const getExtraParams = list => list.filter(key => key.extraParam).map(key => key.extraParam);
Object.keys(tasks)
.reduce((acc, key) => acc.option(tasks[key].option, tasks[key].name), main)
.parse(process.argv);
Object.keys(tasks).forEach(key => {
tasks[key].value =
program[tasks[key].option.replace('--', '')] || (program.all && tasks[key].projectLocation);
});
let selection;
if (
!Object.keys(tasks)
.map(key => tasks[key].value)
.filter(Boolean).length
) {
selection = inquirer
.prompt([
{
type: 'checkbox',
message: 'Select which tests to run',
name: 'todo',
pageSize: 8,
choices: Object.keys(tasks)
.map(key => tasks[key])
.filter(key => key.projectLocation)
.map(key => ({
name: key.name,
checked: key.defaultValue,
}))
.concat(new inquirer.Separator())
.concat(
Object.keys(tasks)
.map(key => tasks[key])
.filter(key => key.extraParam)
.map(key => ({
name: key.name,
checked: key.defaultValue,
}))
),
},
])
.then(({ todo }) =>
todo.map(name => tasks[Object.keys(tasks).find(i => tasks[i].name === name)])
);
} else {
selection = Promise.resolve(
Object.keys(tasks)
.map(key => tasks[key])
.filter(item => item.value === true)
);
}
selection
.then(list => {
if (list.length === 0) {
log.warn(prefix, 'Nothing to test');
} else {
const projects = getProjects(list);
const jestProjects = projects.filter(key => key.isJest).map(key => key.projectLocation);
const nonJestProjects = projects.filter(key => !key.isJest);
const extraParams = getExtraParams(list).join(' ');
if (jestProjects.length > 0) {
spawn(`jest --projects ${jestProjects.join(' ')} ${extraParams}`);
}
nonJestProjects.forEach(key =>
spawn(`npm --prefix ${key.projectLocation} test -- ${extraParams}`)
);
process.stdout.write('\x07');
}
})
.catch(e => {
log.aborted(prefix, chalk.red(e.message));
log.silly(prefix, e);
process.exit(1);
});