How do you debug Jest Tests?

后端 未结 5 1415
庸人自扰
庸人自扰 2020-12-09 17:57

I can\'t find any information on debugging my unit tests written with Jest.

How do you debug Jest Tests ?

5条回答
  •  遥遥无期
    2020-12-09 18:26

    You can use Chrome DevTools to debug Jest tests.

    First, start Node debugger in your project:

    node --inspect-brk  --runInBand 
    

    Examples:

    • If you install Jest locally (Linux example):

      node --inspect-brk ./node_modules/jest/bin/jest.js --runInBand mymodule/test.js

    • If you install Jest globally (Windows example):

      node --inspect-brk "C:\\Program Files\\nodejs\\node_modules\\jest\\bin\\jest.js" --runInBand mymodule\\test.js

    Then, you can open the Google Chrome browser, and type in the address bar:

    chrome://inspect
    

    Now click the inspect link under "Remote Target" to open Chrome DevTools.

    Note that you probably need to add the source code folder to the workspace in chrome-devtools, so as to be able to set breakpoints.

    Now you can press F8 to start debugging.

    [FYI]:

    • My Node version: v10.11.0
    • My Jest version: 23.6.0
    • My Google Chrome version: 71.0.3578.98

    [Update] Regarding the step of adding the source code folder to the workspace in chrome-devtools (as asked by Sam), it looks like below:

    And then you can open your script files and set the breakpoints:

    You find the simple demo project on my GitHub repo.

提交回复
热议问题