Jest: How to send node command line arguments in jest test

Created on 15 Dec 2017  ยท  9Comments  ยท  Source: facebook/jest

I have a utility.js which uses program arguments through process.argv.

When I run jest -t 'utility-spec' --arg1 value1 --arg2 value2 it's throwing exception. Failed to run the test.

(node:8956) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): โ— Unrecognized CLI Parameters:

Following options were not recognized: ["arg1", "arg2"]

How to pass my program argument.?

Most helpful comment

@rambabusaravanan
I did this in my spec.js at the top

process.argv.push('--arg1', 'value1');

describe('check', () => {
.....
});

All 9 comments

Jest doesn't accept node arguments unfortunately. I recommend testing your utility by making a programmatic interface. You can also overwrite process.argv in your test before requiring your module.

@cpojer Can you please show us a sample how to do that?

@rambabusaravanan
I did this in my spec.js at the top

process.argv.push('--arg1', 'value1');

describe('check', () => {
.....
});

This should not be blocking at all... My Jenkins jobs crash because of that errors, with proxy configuration...

For convenience, and in case Repl.it disappears in the future, here is the code that @SimenB linked to in their comment:

test.js

~~~js
process.argv = ['node', 'jest', '--arg1', '1', '--arg2', 'hello']

const argv = require('./argv');

test('argv', () => {
expect(argv).toEqual(['--arg1', '1', '--arg2', 'hello']);
});
~~~

argv.js

~js
module.exports = process.argv.slice(2);
~

It's worth noting that this also works if you put it in a jest setup file

Jest configuration - jest.config.js

module.exports = {
    setupFiles: ['./setup'],
};

Setup File - setup.js

process.argv = ['node', 'jest', 'arg1', 'arg2'];
process.argv.push('--config');  // your flag
process.argv.push('config-local.json');  // flag value

This information might be useful to someone:

I have an npm task which I run via npm run jest -- --password=MY_PASSWORD.
The npm task: "jest": "jest --passWithNoTests --forceExit --silent"

There is a single test file:

test('database', () => {
    return new Promise(function(resolve, reject) {
        dbConnect(function(err, db) {
            if (err) return reject(err);
            resolve(db);
        });
    });
});

dbConnect internally reads password from process.argv to connect to the database. This works, the argument that I pass when running npm is available in dbConnect. However, as soon as there are two test files it no longer works because then the arguments get swallowed.

A very bad workaround is running with --maxWorkers=1. With --maxWorkers=2 the second item in process.argv is node_modules/jest-worker/build/workers/processChild.js, otherwise it's node_modules/.bin/jest. I think if there is only one test file it will also use node_modules/.bin/jest, so the arguments are passed down.

I was surprised when my tests broke after "not changing anything", not realizing what I did was adding a second test file, which then broke everything. But I didn't see it mentioned anywhere that maxWorkers has that effect.

I've tried other methods of injecting my custom --password=MY_PASSWORD argument so it's available in test files but it only works with --maxWorkers=1 or when using only a single test file. :woman_shrugging:

  • node 14.15.0
  • npm 6.14.8
  • jest 25.5.4
  • jest-extended 0.11.5
  • babel-jest 25.5.1
  • babel-plugin-require-context-hook 1.0.0

Setup file:

require('babel-plugin-require-context-hook/register')();
require('regenerator-runtime/runtime');
Was this page helpful?
0 / 5 - 0 ratings