storybook/scripts/test.js

197 lines
5.5 KiB
JavaScript
Raw Normal View History

2017-08-28 15:15:37 +02:00
#!/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');
2017-08-28 15:15:37 +02:00
log.heading = 'storybook';
const prefix = 'test';
log.addLevel('aborted', 3001, { fg: 'red', bold: true });
2020-03-27 20:04:50 +01:00
const spawn = (command) => {
2017-09-06 17:47:21 +02:00
const out = childProcess.spawnSync(`${command}`, {
2017-08-28 15:15:37 +02:00
shell: true,
stdio: 'inherit',
});
2017-09-06 17:47:21 +02:00
if (out.status !== 0) {
process.exit(out.status);
}
2017-09-06 17:47:21 +02:00
return out;
};
2017-08-28 15:15:37 +02:00
const main = program.version('3.0.0').option('--all', `Test everything ${chalk.gray('(all)')}`);
2018-08-22 19:06:31 +02:00
const createProject = ({ defaultValue, option, name, projectLocation, isJest, script }) => ({
2017-08-28 15:15:37 +02:00
value: false,
defaultValue: defaultValue || false,
option: option || undefined,
name: name || 'unnamed task',
2018-08-22 19:06:31 +02:00
script,
2017-08-28 15:15:37 +02:00
projectLocation,
2017-09-04 01:59:55 +03:00
isJest,
2017-08-28 15:15:37 +02:00
});
const createOption = ({ defaultValue, option, name, extraParam }) => ({
value: false,
defaultValue: defaultValue || false,
option: option || undefined,
name: name || 'unnamed task',
extraParam,
});
2017-08-28 15:15:37 +02:00
const tasks = {
core: createProject({
2018-08-27 19:42:20 +02:00
name: `Core & Examples 🎨 ${chalk.gray('(core)')}`,
2017-08-28 15:15:37 +02:00
defaultValue: true,
option: '--core',
projectLocation: '<all>',
2017-09-04 01:59:55 +03:00
isJest: true,
2017-08-28 15:15:37 +02:00
}),
2019-11-24 16:50:53 +01:00
puppeteer: createProject({
name: `Puppeteer and A11y tests for Official storybook ${chalk.gray('(puppeteer)')}`,
2017-12-01 17:45:59 +01:00
defaultValue: false,
2019-11-24 16:50:53 +01:00
option: '--puppeteer',
projectLocation: path.join(__dirname, '..', 'examples/official-storybook/storyshots-puppeteer'),
isJest: true,
2017-12-01 17:45:59 +01:00
}),
2017-09-04 01:59:55 +03:00
cli: createProject({
name: `Command Line Interface ${chalk.gray('(cli)')}`,
defaultValue: false,
option: '--cli',
2018-08-23 16:34:24 +03:00
projectLocation: './lib/cli',
2017-09-04 01:59:55 +03:00
}),
watchmode: createOption({
2017-08-28 15:15:37 +02:00
name: `Run in watch-mode ${chalk.gray('(watchmode)')}`,
defaultValue: false,
option: '--watch',
extraParam: '--watch',
}),
2017-08-28 19:44:34 +02:00
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',
}),
2019-03-18 22:27:32 +01:00
w2: createOption({
name: `Run all tests in max 2 processes process ${chalk.gray('(w2)')}`,
defaultValue: false,
option: '--w2',
extraParam: '-w 2',
}),
reportLeaks: createOption({
name: `report memory leaks ${chalk.gray('(reportLeaks)')}`,
defaultValue: false,
option: '--reportLeaks',
extraParam: '--detectLeaks',
}),
2017-09-04 01:59:55 +03:00
update: createOption({
name: `Update all snapshots ${chalk.gray('(update)')}`,
defaultValue: false,
option: '--update',
extraParam: '-u --updateSnapshot',
2017-09-04 01:59:55 +03:00
}),
2017-08-28 15:15:37 +02:00
};
2020-03-27 20:04:50 +01:00
const getProjects = (list) => list.filter((key) => key.projectLocation);
2020-03-27 20:04:50 +01:00
const getScripts = (list) => list.filter((key) => key.script);
2020-03-27 20:04:50 +01:00
const getExtraParams = (list) => list.filter((key) => key.extraParam).map((key) => key.extraParam);
2017-08-28 15:15:37 +02:00
Object.keys(tasks)
.reduce((acc, key) => acc.option(tasks[key].option, tasks[key].name), main)
.parse(process.argv);
2020-03-27 20:04:50 +01:00
Object.keys(tasks).forEach((key) => {
tasks[key].value =
program[tasks[key].option.replace('--', '')] || (program.all && tasks[key].projectLocation);
2017-08-28 15:15:37 +02:00
});
let selection;
if (
!Object.keys(tasks)
2020-03-27 20:04:50 +01:00
.map((key) => tasks[key].value)
.filter(Boolean).length
) {
2017-08-28 15:15:37 +02:00
selection = inquirer
.prompt([
{
type: 'checkbox',
message: 'Select which tests to run',
name: 'todo',
2018-08-22 08:54:36 +02:00
pageSize: 18,
choices: Object.values(tasks)
2020-03-27 20:04:50 +01:00
.filter((key) => !key.extraParam)
.map((key) => ({
name: key.name,
checked: key.defaultValue,
2017-08-28 15:15:37 +02:00
}))
.concat(new inquirer.Separator())
.concat(
Object.keys(tasks)
2020-03-27 20:04:50 +01:00
.map((key) => tasks[key])
.filter((key) => key.extraParam)
.map((key) => ({
name: key.name,
checked: key.defaultValue,
}))
),
2017-08-28 15:15:37 +02:00
},
])
.then(({ todo }) =>
2020-03-27 20:04:50 +01:00
todo.map((name) => tasks[Object.keys(tasks).find((i) => tasks[i].name === name)])
2017-08-28 15:15:37 +02:00
);
} else {
selection = Promise.resolve(
Object.keys(tasks)
2020-03-27 20:04:50 +01:00
.map((key) => tasks[key])
.filter((item) => item.value === true)
2017-08-28 15:15:37 +02:00
);
}
selection
2020-03-27 20:04:50 +01:00
.then((list) => {
2017-08-28 15:15:37 +02:00
if (list.length === 0) {
log.warn(prefix, 'Nothing to test');
} else {
2017-09-04 01:59:55 +03:00
const projects = getProjects(list);
2020-03-27 20:04:50 +01:00
const jestProjects = projects.filter((key) => key.isJest).map((key) => key.projectLocation);
const nonJestProjects = projects.filter((key) => !key.isJest);
2017-09-04 01:59:55 +03:00
const extraParams = getExtraParams(list).join(' ');
const jest = path.join(__dirname, '..', 'node_modules', '.bin', 'jest');
2017-09-04 01:59:55 +03:00
if (jestProjects.length > 0) {
2020-03-27 20:04:50 +01:00
const projectsParam = jestProjects.some((project) => project === '<all>')
? ''
: `--projects ${jestProjects.join(' ')}`;
2020-05-24 14:52:58 +02:00
const cmd = `cross-env NODE_OPTIONS=--max_old_space_size=4096 ${jest} ${projectsParam} ${extraParams}`;
2019-07-13 07:42:16 -04:00
2019-07-09 09:39:22 +02:00
spawn(cmd);
2017-09-04 01:59:55 +03:00
}
2020-03-27 20:04:50 +01:00
nonJestProjects.forEach((key) =>
2017-09-04 01:59:55 +03:00
spawn(`npm --prefix ${key.projectLocation} test -- ${extraParams}`)
);
2018-08-22 08:54:36 +02:00
const scripts = getScripts(list);
2020-03-27 20:04:50 +01:00
scripts.forEach((key) => spawn(`${key.script} -- ${extraParams}`));
2018-08-22 08:54:36 +02:00
2017-08-28 15:15:37 +02:00
process.stdout.write('\x07');
}
})
2020-03-27 20:04:50 +01:00
.catch((e) => {
2017-08-28 15:15:37 +02:00
log.aborted(prefix, chalk.red(e.message));
log.silly(prefix, e);
2017-09-06 17:47:21 +02:00
process.exit(1);
2017-08-28 15:15:37 +02:00
});