Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to send node command line arguments in jest test #5089

Closed
Pasupathi-Rajamanickam opened this issue Dec 15, 2017 · 10 comments
Closed

How to send node command line arguments in jest test #5089

Pasupathi-Rajamanickam opened this issue Dec 15, 2017 · 10 comments

Comments

@Pasupathi-Rajamanickam
Copy link

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.?

@cpojer
Copy link
Member

cpojer commented Dec 16, 2017

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 cpojer closed this as completed Dec 16, 2017
@rambabusaravanan
Copy link

rambabusaravanan commented May 29, 2018

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

@SimenB
Copy link
Member

SimenB commented May 29, 2018

https://repl.it/@SimenB/SphericalFirsthandOpengroup

@Pasupathi-Rajamanickam
Copy link
Author

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

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

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

@GuiF007
Copy link

GuiF007 commented Jul 4, 2018

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

@roryokane
Copy link

roryokane commented Apr 5, 2020

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

test.js

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

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

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

argv.js

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

@tomnlittle
Copy link

tomnlittle commented Apr 8, 2020

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'];

@akopchinskiy
Copy link

process.argv.push('--config');  // your flag
process.argv.push('config-local.json');  // flag value

@bendulum
Copy link

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. 🤷‍♀️

  • 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');

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants