Skip to content

How to pass arguments to node in Jest tests? #549

Closed

Description

@martinschayna

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?

Activity

  1. gaearon commented on Sep 2, 2016

    @gaearon
    Contributor

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

  2. modified the milestones: 0.4.0, 1.0.0 on Sep 2, 2016
  3. cpojer commented on Sep 4, 2016

    @cpojer
    Contributor

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

  4. gaearon commented on Sep 4, 2016

    @gaearon
    Contributor

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

  5. tkrotoff commented on Feb 15, 2017

    @tkrotoff

    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
  6. metaskills commented on Jul 18, 2018

    @metaskills

    @tkrotoff Thank you!

  7. dandv commented on Oct 10, 2018

    @dandv

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

  8. locked and limited conversation to collaborators on Jan 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions