Skip to content

Instantly share code, notes, and snippets.

@NickCis
Created February 3, 2019 20:51
Show Gist options
  • Save NickCis/453532071e52714d19a1acb3d7ca6cc2 to your computer and use it in GitHub Desktop.
Save NickCis/453532071e52714d19a1acb3d7ca6cc2 to your computer and use it in GitHub Desktop.
Jest custom command line arguments
'use strict';
const config = {
custom: 'command',
flag: false,
};
const argv = process.argv.slice(0, 2);
// Naive argv parsing
process.argv
.reduce((cmd, arg) => {
if (cmd) {
config[cmd] = arg;
return;
}
if (arg.startsWith('--')) {
const sub = arg.substring('--'.length);
if (Object.keys(config).includes(sub)) {
if (typeof config[sub] === 'boolean') {
config[cmd] = true;
return;
}
return sub;
}
}
argv.push(arg)
});
// Setting real ARGV
process.argv = argv;
// Calling jest runner
require('jest-cli/bin/jest');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment