28

node can be started with various options. Especially interesting is the --inspect flag:

node --inspect node_modules/.bin/jest some.spec.js

Is it possible to pass the --inspect flag somehow to yarn run? For example:

yarn run test --inspect some.spec.js 

There is a similar question for npm run, where it seems to be not possible.

Styx
  • 9,863
  • 8
  • 43
  • 53
dre-hh
  • 7,840
  • 2
  • 33
  • 44

2 Answers2

45

In general, yarn run ... does not support passing arbitrary arguments to NodeJS. However, the --inspect flag is an exception depending on your version of Yarn.

As of March 2022, recent versions of Yarn support both --inspect and --inspect-brk arguments for the yarn run command. The answer to your questions is now "yes", and the following will work:

yarn run --inspect some.spec.js

For older versions of Yarn (or even NPM), there are a couple of options.

First, you can use the NODE_OPTIONS environment variable to pass arguments to NodeJS. For example,

export NODE_OPTIONS="--inspect"
yarn run test some.spec.js

then in the package.json, you can define a script to take advantage of this:

"scripts": {
  "test": "jest",
  "test:inspect": "NODE_OPTIONS='--inspect' yarn run test"
}

Second, as you mentioned, you can use NodeJS directly,

node --inspect ./node_modules/jest-cli/bin/jest.js some.spec.js

For older versions of Yarn, those may be your only two options. However, both options work for both NPM and Yarn.

timrs2998
  • 1,612
  • 18
  • 14
  • An alternative (described [here](http://artsy.github.io/blog/2018/08/24/How-to-debug-jest-tests/)) is to use `"test:inspect": "node --inspect node_modules/.bin/jest --runInBand"`, which then allows running `yarn test:debug` to start a debug session in the tests. – waldyrious Jan 17 '19 at 18:19
  • 1
    I tried on windows with `set NODE_OPTIONS="--max-old-space-size=81392"` followed by `gulp` but it doesn't seem to take effect. – Andy Sep 03 '19 at 19:02
  • `2.1.0` is the version of yarn that introduced the `--inspect-brk` support – Matthias Feb 04 '23 at 01:14
0

Alternatively you can use npx and pass node arguments with a -n or --node-arg flagnpx -n=--inspect-brk jest

npx feels more intuitive when dealing with cli's or packages that alias commands.

fengelhardt
  • 965
  • 11
  • 24
  • this answer would fit more here https://stackoverflow.com/questions/37317557/pass-arguments-to-node-executable-when-running-npm-run Since there is no npx for yarn – dre-hh Aug 09 '21 at 09:13
  • 1
    @dre-hh there is `yarn dlx` for yarn 2+. – wegry Nov 08 '22 at 18:49