我使用 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/