Created
March 15, 2018 23:46
-
-
Save ardok/851685bf255fdd9533797055fbc355f3 to your computer and use it in GitHub Desktop.
Running jest in node and browser env, then combine the coverage with istanbul
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable */ | |
const common = require('../jest-config.common'); | |
module.exports = Object.assign({}, common, { | |
"coverageDirectory": "<rootDir>/coverage/browser", | |
"testMatch": [ | |
"**/test/browser/**/*.test.js" | |
], | |
"setupFiles": [ | |
"<rootDir>/src/test/browser/setup.js" | |
] | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable */ | |
module.exports = { | |
"verbose": true, | |
"rootDir": "../../../", | |
"collectCoverage": true, | |
"collectCoverageFrom": [ | |
"!**/node_modules/**", | |
"<rootDir>/src/client/**/*.js", | |
"<rootDir>/src/server/**/*.js", | |
"<rootDir>/src/shared/**/*.js", | |
"!<rootDir>/src/shared/types/**/*.js" | |
], | |
"coverageReporters": [ | |
"json" | |
], | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable */ | |
const common = require('../jest-config.common'); | |
module.exports = Object.assign({}, common, { | |
"testEnvironment": "node", | |
"coverageDirectory": "<rootDir>/coverage/node", | |
"testMatch": [ | |
"**/test/node/**/*.test.js" | |
], | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
"async": "^2.6.0", | |
"chalk": "^2.3.2", | |
node 6.10.0 | |
*/ | |
/* eslint-disable no-console */ | |
/* | |
This will run jest in 2 different environments, node and browser. | |
Then it will run `map-coverage.js` to combine the coverage report. | |
*/ | |
const async = require('async'); | |
const chalk = require('chalk'); | |
const {spawn} = require('child_process'); | |
function getArgs() { | |
/* | |
Get all the arguments that are passed in. | |
You need to pass it in by doing `npm run jest -- -u`. | |
Remember the `--` first. | |
It will all go into `jest` config options. | |
*/ | |
const args = []; | |
process.argv.forEach((val, index) => { | |
if (index === 0 || index === 1) { | |
// first is node path file, second is name of js file | |
return; | |
} | |
args.push(val); | |
}); | |
return args; | |
} | |
function isTestEnv(args, env) { | |
const envTestIndex = args.indexOf(`--test=${env}`); | |
return envTestIndex > -1; | |
} | |
function clearOwnOptions(args) { | |
/* | |
Method to clear our own options. | |
This only supports `--test=browser` and `--test=node` for now. | |
--test=browser means that we will only run browser tests. | |
*/ | |
const cleared = args.slice(); | |
if (isTestEnv(args, 'browser')) { | |
cleared.splice(cleared.indexOf('--test=browser'), 1); | |
} | |
if (isTestEnv(args, 'node')) { | |
cleared.splice(cleared.indexOf('--test=node'), 1); | |
} | |
return cleared; | |
} | |
function createCommands(args, modArgs) { | |
/* | |
Method to create the commands to run. | |
*/ | |
const joinedArgs = modArgs.join(' '); | |
const commands = []; | |
const isTestBrowser = isTestEnv(args, 'browser'); | |
const isTestNode = isTestEnv(args, 'node'); | |
// Using `./node_modules/.bin/jest` so that people won't need to configure their own $PATH or mistakenly installing `jest` globally | |
const pushTestNode = () => commands.push({id: 'node', cmd: `./node_modules/.bin/jest --config src/test/node/jest-config.node.js ${joinedArgs}`}); | |
const pushTestBrowser = () => commands.push({id: 'browser', cmd: `./node_modules/.bin/jest --config src/test/browser/jest-config.browser.js ${joinedArgs}`}); | |
if (isTestNode || isTestBrowser) { | |
if (isTestNode) { | |
pushTestNode(); | |
} | |
if (isTestBrowser) { | |
pushTestBrowser(); | |
} | |
} else { | |
pushTestNode(); | |
pushTestBrowser(); | |
} | |
commands.push({id: 'coverage', cmd: 'node ./scripts/map-coverage.js'}); | |
return commands; | |
} | |
const args = getArgs(); | |
const modArgs = clearOwnOptions(args); | |
const commands = createCommands(args, modArgs); | |
function runExec(cmd, idx) { | |
return (callback) => { | |
const dashes = '-'.repeat(cmd.cmd.length); | |
console.log([ | |
chalk.cyan(dashes), | |
chalk.cyan(cmd.cmd), | |
chalk.cyan(dashes), | |
'', | |
].join('\n')); | |
const splitCmd = cmd.cmd.split(' '); | |
const spawnedProcess = spawn(splitCmd[0], splitCmd.slice(1), { | |
stdio: 'inherit', | |
}); | |
spawnedProcess.on('close', callback); | |
}; | |
} | |
async.series( | |
commands.map((cmd, idx) => runExec(cmd, idx)) | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
"istanbul-api": "^1.3.1", | |
"istanbul-lib-coverage": "^1.2.0", | |
node 6.10.0 | |
*/ | |
const {createReporter} = require('istanbul-api'); | |
const istanbulCoverage = require('istanbul-lib-coverage'); | |
const coverageNode = require('../coverage/node/coverage-final.json'); | |
const coverageBrowser = require('../coverage/browser/coverage-final.json'); | |
const map = istanbulCoverage.createCoverageMap(); | |
map.merge(coverageBrowser); | |
map.merge(coverageNode); | |
const reporter = createReporter(); | |
const summary = istanbulCoverage.createCoverageSummary(); | |
map.files().forEach((f) => { | |
const fc = map.fileCoverageFor(f); | |
const s = fc.toSummary(); | |
summary.merge(s); | |
}); | |
reporter.addAll(['json', 'text', 'cobertura']); | |
reporter.write(map); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
... | |
"scripts": { | |
..., | |
"jest": "node ./jest.js" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment