|
| 1 | +#! /usr/bin/env node |
| 2 | +/* eslint-disable no-console */ |
| 3 | +const path = require('path'); |
| 4 | +const chalk = require('chalk'); |
| 5 | +const fs = require('fs'); |
| 6 | +const childProcess = require('child_process'); |
| 7 | +const opn = require('opn'); |
| 8 | + |
| 9 | +const args = process.argv.slice(2); |
| 10 | + |
| 11 | +const showUsage = () => { |
| 12 | + console.log(chalk.yellow('Usage:')); |
| 13 | + console.log(chalk.yellow('--------------------------------------------------------------')); |
| 14 | + console.log(chalk.yellow('[**/*.test.js] # test file with coverage report')); |
| 15 | + console.log(chalk.yellow('--nowatch # turn off watcher')); |
| 16 | + console.log(chalk.yellow('open # opens the coverage report')); |
| 17 | + console.log(chalk.yellow('--help # display this message')); |
| 18 | + console.log(chalk.yellow('--------------------------------------------------------------')); |
| 19 | +}; |
| 20 | + |
| 21 | +let command = 'node_modules/jest/bin/jest.js --watch'; |
| 22 | +let openCoverageReport = false; |
| 23 | +if (args.length === 0) { |
| 24 | + showUsage(); |
| 25 | + return; |
| 26 | +} |
| 27 | +args.forEach(entry => { |
| 28 | + if (entry === '-h' || entry === '--help') { |
| 29 | + showUsage(); |
| 30 | + } else if (entry === '--nowatch') { |
| 31 | + command = command.replace('--watch', ''); |
| 32 | + } else if (entry === 'open') { |
| 33 | + openCoverageReport = true; |
| 34 | + command = command.replace('--watch', ''); |
| 35 | + } else { |
| 36 | + if (!fs.existsSync(entry)) { |
| 37 | + console.log(chalk.red(`Error!! Test file: ${entry} doesn't exist.`)); |
| 38 | + process.exit(); |
| 39 | + return; |
| 40 | + } |
| 41 | + command += ` ${entry}`; |
| 42 | + |
| 43 | + const {dir, name, ext} = path.parse(entry); |
| 44 | + const filename = name.replace('.test', '') + ext; |
| 45 | + const searchPath = `${dir}/${filename}`; |
| 46 | + const exists = fs.existsSync(searchPath); |
| 47 | + if (!exists) { |
| 48 | + console.log(chalk.red(`Error!! Original file: ${searchPath} doesn't exist.`)); |
| 49 | + } else { |
| 50 | + command += ` --coverage --collectCoverageFrom=${searchPath}`; |
| 51 | + } |
| 52 | + } |
| 53 | +}); |
| 54 | + |
| 55 | +command += ' --color always'; |
| 56 | +try { |
| 57 | + console.log(command); |
| 58 | + childProcess.execSync(command, {stdio: 'inherit'}); |
| 59 | + if (openCoverageReport) { |
| 60 | + opn('./test/coverage-jest/index.html', {wait: false}); |
| 61 | + } |
| 62 | +} catch (e) {} // eslint-disable-line no-empty |
0 commit comments