执行node --inspect-brk调试Jest时遇ReferenceError错误求助
Hey there, let's break down why you're hitting this error and how to fix it quickly.
The Root Cause
Your project uses react-scripts (clear from your package.json dependencies and scripts), which wraps Jest and Babel with pre-configured settings to simplify Create React App projects. When you directly call ./node_modules/jest/bin/jest.js in your test:debug script, you're bypassing react-scripts's managed configuration—this leads to Babel not recognizing the settings it expects, hence the "Unknown option: .debug" error.
The Fix
Replace your custom test:debug script with one that uses react-scripts's built-in test runner, which handles all the Babel/Jest setup for you. Update the script in your package.json to:
"test:debug": "node --inspect-brk node_modules/react-scripts/scripts/test.js --runInBand --watch"
Or use this shorter, equivalent version:
"test:debug": "react-scripts --inspect-brk test --runInBand --watch"
Why This Works
react-scripts/scripts/test.js is the entry point Create React App uses for its default test command. It loads all the correct Babel presets, Jest configurations, and environment settings tailored to your React project. By using this instead of calling Jest directly, you avoid the configuration mismatches that triggered the Babel error.
Extra Tips
- After updating the script, run
yarn test:debugand open Chrome tochrome://inspect—you'll see the Node.js debugger session listed, ready to connect for breakpoint debugging. - Your Node v10.17.0 and React v16.12.0 versions are fully compatible with react-scripts 3.3.0, so no version conflicts are at play here.
- Reinstalling
node_moduleswas a good first step, but the issue never lay with dependencies—it was with how you were invoking Jest.
内容的提问来源于stack exchange,提问作者Remi

