debugging - 如何在 create-react-app 上调试 jest 单元测试?

标签 debugging jestjs create-react-app

我使用 create-react-app 并且我想调试我的单元测试

作为 Jest documentation says ,可以使用以下命令进行调试:

node --debug-brk --inspect ./node_modules/.bin/jest -i [any other arguments here]

不幸的是,它不适用于 create-react-app。
我收到了这个错误:
node --debug-brk --inspect ./node_modules/.bin/jest -i
Debugger listening on port 9229.
Warning: This is an experimental feature and could change at any time.
To start debugging, open the following URL in Chrome:
    chrome-devtools://devtools/remote/serve_file/@60cd6e859b9f557d2312f5bf532f6aec5f284980/inspector.html?experiments=true&v8only=true&ws=localhost:9229/node
Debugger attached.
module.js:457
    throw err;
    ^

Error: Cannot find module '/Users/asafkatz/dev/newmaps/node_modules/.bin/jest'
    at Function.Module._resolveFilename (module.js:455:15)
    at Function.Module._load (module.js:403:25)
    at Timeout.Module.runMain [as _onTimeout] (module.js:590:10)
    at tryOnTimeout (timers.js:232:11)
    at Timer.listOnTimeout (timers.js:202:5)
Waiting for the debugger to disconnect...

在 create-react-app 上调试 jest 单元测试的正确方法是什么?

最佳答案

create react app 文档有关于如何调试测试的文档。
https://create-react-app.dev/docs/debugging-tests/
总之,您所要做的就是将其添加到您的 package.json

"scripts": {
"test:debug": "react-scripts --inspect-brk test --runInBand --no-cache"
}
放置 debugger;在您的测试中声明并运行
$ npm run test:debug

关于debugging - 如何在 create-react-app 上调试 jest 单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39798495/

相关文章:

使用 Pydev 进行 Django/Docker/远程调试

reactjs - 引用错误 : React is not defined in jest tests

javascript - 使用旧版本的 Node 创建 React App

create-react-app - 卸载 Expo CLI

npm - 为什么我的 npm_modules 文件夹在 VS Code 中都突出显示为红色?

c++ - 使用 GDB 在 Windows 下调试 DLL

c - 不兼容的返回类型

c# - Visual Studio 崩溃! - 在实例化枚举器后修改了集合

typescript - 如何在 Jest 中的 beforeAll/beforeEach 和测试之间共享数据?

javascript - enzyme |有条件地检查 Prop