-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjest1.js
executable file
·62 lines (57 loc) · 2.19 KB
/
jest1.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
#! /usr/bin/env node
/* eslint-disable no-console */
const path = require('path');
const chalk = require('chalk');
const fs = require('fs');
const childProcess = require('child_process');
const opn = require('opn');
const args = process.argv.slice(2);
const showUsage = () => {
console.log(chalk.yellow('Usage:'));
console.log(chalk.yellow('--------------------------------------------------------------'));
console.log(chalk.yellow('[**/*.test.js] # test file with coverage report'));
console.log(chalk.yellow('--nowatch # turn off watcher'));
console.log(chalk.yellow('open # opens the coverage report'));
console.log(chalk.yellow('--help # display this message'));
console.log(chalk.yellow('--------------------------------------------------------------'));
};
let command = 'node_modules/jest/bin/jest.js --watch';
let openCoverageReport = false;
if (args.length === 0) {
showUsage();
return;
}
args.forEach(entry => {
if (entry === '-h' || entry === '--help') {
showUsage();
} else if (entry === '--nowatch') {
command = command.replace('--watch', '');
} else if (entry === 'open') {
openCoverageReport = true;
command = command.replace('--watch', '');
} else {
if (!fs.existsSync(entry)) {
console.log(chalk.red(`Error!! Test file: ${entry} doesn't exist.`));
process.exit();
return;
}
command += ` ${entry}`;
const {dir, name, ext} = path.parse(entry);
const filename = name.replace('.test', '') + ext;
const searchPath = `${dir}/${filename}`;
const exists = fs.existsSync(searchPath);
if (!exists) {
console.log(chalk.red(`Error!! Original file: ${searchPath} doesn't exist.`));
} else {
command += ` --coverage --collectCoverageFrom=${searchPath}`;
}
}
});
command += ' --color always';
try {
console.log(command);
childProcess.execSync(command, {stdio: 'inherit'});
if (openCoverageReport) {
opn('./test/coverage-jest/index.html', {wait: false});
}
} catch (e) {} // eslint-disable-line no-empty