Skip to content

Commit

Permalink
feat: add cli option "--require" to load external modules
Browse files Browse the repository at this point in the history
  • Loading branch information
DudaGod committed Apr 28, 2020
1 parent e71c5ad commit c93e652
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -1072,6 +1072,7 @@ shows the following
-r, --reporter <reporter> test reporters
-b, --browser <browser> run tests only in specified browser
-s, --set <set> run tests only in the specified set
--require <module> require a module before running hermione
--grep <grep> run only tests matching the pattern
--update-refs update screenshot references or gather them if they do not exist ("assertView" command)
--inspect [inspect] nodejs inspector on [=[host:]port]
Expand All @@ -1093,6 +1094,12 @@ You can choose `flat` or `plain` reporter by option `-r, --reporter`. Default is

* `plain` – information about fails and retries would be placed after each test.

## Require modules

Using `--require` option you can load external modules, which exists in your local machine, before running hermione. This is useful for:
- compilers such as TypeScript via [ts-node](https://www.npmjs.com/package/ts-node) (using `--require ts-node/register`) or Babel via [@babel/register](https://www.npmjs.com/package/@babel/register) (using `--require @babel/register`);
- loaders such as ECMAScript modules via [esm](https://www.npmjs.com/package/esm).

## Overriding settings

All options can also be overridden via command-line flags or environment variables. Priorities are the following:
Expand Down
7 changes: 7 additions & 0 deletions lib/cli/index.js
Expand Up @@ -33,13 +33,16 @@ exports.run = () => {
.option('-r, --reporter <reporter>', 'test reporters', collect)
.option('-b, --browser <browser>', 'run tests only in specified browser', collect)
.option('-s, --set <set>', 'run tests only in the specified set', collect)
.option('--require <module>', 'require module', collect)
.option('--grep <grep>', 'run only tests matching the pattern')
.option('--update-refs', 'update screenshot references or gather them if they do not exist ("assertView" command)')
.option('--inspect [inspect]', 'nodejs inspector on [=[host:]port]')
.option('--inspect-brk [inspect-brk]', 'nodejs inspector with break at the start')
.arguments('[paths...]')
.action(async (paths) => {
try {
handleRequires(program.require);

const isTestsSuccess = await hermione.run(paths, {
reporters: program.reporter || defaults.reporters,
browsers: program.browser,
Expand Down Expand Up @@ -77,3 +80,7 @@ function preparseOption(program, option) {
configFileParser.parse(process.argv);
return configFileParser[option];
}

function handleRequires(requires = []) {
requires.forEach((module) => require(module));
}
16 changes: 16 additions & 0 deletions test/lib/cli/index.js
Expand Up @@ -3,6 +3,7 @@
const {Command} = require('@gemini-testing/commander');
const q = require('q');
const _ = require('lodash');
const proxyquire = require('proxyquire');
const hermioneCli = require('lib/cli');
const info = require('lib/cli/info');
const defaults = require('lib/config/defaults');
Expand Down Expand Up @@ -63,6 +64,21 @@ describe('cli', () => {
assert.calledOnce(Hermione.create);
});

it('should require modules specified in "require" option', async () => {
const fooRequire = sandbox.stub().returns({});

const stubHermioneCli = proxyquire('lib/cli', {
foo: (() => fooRequire())()
});

onParse((parser) => parser.require = ['foo']);

stubHermioneCli.run();
await actionPromise;

assert.calledOnce(fooRequire);
});

it('should create Hermione without config by default', async () => {
hermioneCli.run();
await actionPromise;
Expand Down

0 comments on commit c93e652

Please sign in to comment.