简体   繁体   中英

How to debug Jest Tests when using Yarn 2 / Yarn PnP

I'd like to use the node --debug-brk feature described in a previous answer and the jest docs to debug in npm/yarn 1 based projects, but node_modules/ is not present in Yarn 2 / PnP (Plug n Play) based projects, so those instructions won't work for me.

The usual way to access a binary in yarn 2 of yarn run --inspect-brk jest --runInBand launches a debugging session from the terminal, but when connecting to Chrome's Remote Target inspector via chrome://inspect , we get an error message about being unable to connect to the jest.js binary, since it's a path to a .zip file.

Uncaught Error: Cannot find module '/Users/MY_USERNAME/PATH_TO_MY_PROJECT/.yarn/cache/jest-npm-24.9.0-8ddb425e99-2.zip/node_modules/jest/bin/jest.js

What workarounds are available without downgrading to Yarn 1?

Update: the workaround reported below is necessary for Node 12.15 and below. After 12.16.1 , you can use the following command directly:

yarn run --inspect-brk jest --runInBand

The fix which was added to node 12.16.1 is here .

Archived answer for older versions of Node, 12.15 and below

One option is to use yarn unplug before using yarn run

   yarn unplug jest

After that, the following works with Jest in Yarn 2 and Chrome's chrome://inspect :

   yarn run --inspect-brk jest --runInBand # any additional jest args

A downside of this approach is that you may need to undo the unplug before checking in your branch, since it's described as a short term measure rather than a long term state to leave a dependency in.

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Jest",
      "skipFiles": ["<node_internals>/**"],
      "cwd": "${workspaceFolder}",
      "runtimeExecutable": "yarn",
      "runtimeArgs": ["run", "--inspect-brk", "jest"],
      "console": "integratedTerminal",
      "internalConsoleOptions": "neverOpen",
      "port": 9229
    }
  ]
}

You can also use the yarn bin command to accomplish this. I was able to run our jest tests using node and some debugging flag helpers (in a Yarn pnp context) using this command:

yarn node --inspect-brk --expose-gc $(yarn bin jest) test --runInBand --silent --logHeapUsage

Credit - Found the solution posted in this jest issues thread .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM