question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to pass arguments to node in Jest tests?

See original GitHub issue

I have tried to use builtin Jest in version 0.3.0 but need to use ES6 Proxy object in test.

This can be done by passing argument --harmony_proxies to node and using harmony-reflect polyfill in test. I am able to import polyfill in tests, but it looks like it’s not possible to run jest in node with any additional arguments.

I used to use this in package.json:

  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "eject": "react-scripts eject",
    "test": "node --harmony_proxies node_modules/jest/bin/jest.js"
  }

Now jest is completely under the hood of react-scripts, which is gooood 👍, but how can I pass arguments to node?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

14reactions
tkrotoffcommented, Feb 15, 2017

For people not using create-react-app and passing by… I do this in my package.json to pass arguments to Node and Jest (v18.1.0) :

"scripts": {
  "test": "node --trace-warnings node_modules/.bin/jest --no-cache"
}

You can verify that arguments are really treated by passing unknown/bad arguments:

"scripts": {
  "test": "node --bad-node-arg node_modules/.bin/jest"
}
$ yarn test
yarn test v0.20.3
 node --bad-node-arg node_modules/.bin/jest 
node: bad option: --bad-node-arg
error Command failed with exit code 9.
"scripts": {
  "test": "node node_modules/.bin/jest --bad-jest-arg"
}
$ yarn test
yarn test v0.20.3
 node node_modules/.bin/jest --bad-jest-arg 
Unrecognized options: bad-jest-arg, badJestArg
0reactions
dandvcommented, Oct 10, 2018

@tkrotoff, that doesn’t work when passing the --experimental-modules flag to node:

% node --experimental-modules node_modules/.bin/jest
(node:208569) ExperimentalWarning: The ESM module loader is experimental.
 FAIL  ./index.test.mjs
  ● Test suite failed to run

    Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

    Here's what you can do:
[...]
    Details:

    /home/dandv/esmjest/index.test.mjs:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import { foo} from './lib';
                                                                                                    ^

    SyntaxError: Unexpected token {

      at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)

My index.test.mjs has import { foo } from './lib'.

PS: See also overwriting argv.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to send node command line arguments in jest test
Jest doesn't accept node arguments unfortunately. I recommend testing your utility by making a programmatic interface.
Read more >
Jest CLI Options
If you run Jest via yarn test , you can pass the command line arguments directly as Jest arguments. Instead of: jest -u...
Read more >
Jest: Passing custom arguments - Medium
Jest : Passing custom arguments · First try: Run jest with any argument · Second try: Google to the rescue? · The third...
Read more >
How to pass command line options to Jest? - Knapsack Pro
Instead, use --updateSnapshot . If you want to pass arguments related to Node you should use NODE_OPTIONS environment variable. See example for ...
Read more >
jest pass arguments to node, jest test with parameters, jest ...
jest pass arguments to node. Launching the Node.js process as: node process-args.js hello "Rahul Kumar" 123. Would generate the output: 0: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found