How to debug Jest Tests when using Yarn 2 / Yarn PnP
Asked Answered
O

3

5

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?

Ovenware answered 13/3, 2020 at 14:41 Comment(0)
D
6
{
  "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
    }
  ]
}
Disorganization answered 23/9, 2020 at 10:51 Comment(0)
O
5

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.

Ovenware answered 13/3, 2020 at 14:41 Comment(0)
H
2

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.

Hydrophilous answered 26/4, 2022 at 22:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.