Create-react-app: How to pass arguments to node in Jest tests?

Created on 2 Sep 2016  ยท  6Comments  ยท  Source: facebook/create-react-app

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 :+1:, but how can I pass arguments to node?

proposal

Most helpful comment

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

All 6 comments

I think we should enable --harmony_proxies by default. How can we do this?

@martinschayna can you upgrade to Node 6 where it is available by default? I don't think we should change how node is invoked; the flags are there for a reason (the feature is experimental in the version of node you are using).

Ah, I didn't realize it just works in Node 6. Let's close then.

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

@tkrotoff Thank you!

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

Was this page helpful?
0 / 5 - 0 ratings