我正在调试一个nodeJS应用程序。我有一些导致错误的代码,一个变量未定义。当我正常运行代码时,错误非常清楚,很容易找到:
不开玩笑:
➜ server git:(dc/build) ✗ node test/runner.js
/Users/dc/dev/exiteer/xbot/server/src/mup/Story.js:24
Logger.logObj('loaded story', {name: doc.name})
^
ReferenceError: doc is not defined
at Story.reload (/Users/dc/dev/exiteer/xbot/server/src/mup/Story.js:24:42)
at Game.reload (/Users/dc/dev/exiteer/xbot/server/src/mup/Game.js:48:16)
at Object.<anonymous> (/Users/dc/dev/exiteer/xbot/server/test/runner.js:4:10)
太好了,我能修好。
现在,Jest有一些很好的工具来编写测试,所以我想我应该尝试一下。
但这些错误似乎是不可能追踪到的:
➜ server git:(dc/build) ✗ npm run jest
> cbg@0.1.0 jest /Users/dc/dev/exiteer/xbot/server
> jest
PASS src/index.test.js
(node:23114) UnhandledPromiseRejectionWarning: ReferenceError: doc is not defined
(Use `node --trace-warnings ...` to show where the warning was created)
(node:23114) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:23114) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
FAIL src/mup/Actor.test.js
● Console
console.log
actors undefined
at Object.<anonymous> (src/mup/Actor.test.js:9:13)
● Game.js › can load a story
expect(received).toHaveLength(expected)
Matcher error: received value must have a length property whose value must be a number
Received has value: undefined
8 |
9 | console.log('actors', game.story.room.name.actors)
> 10 | expect(game.story.room.actors).toHaveLength(1);
| ^
11 | const actor = game.story.room.actors[0]
12 | const reply = actor.sayTo('hi')
13 | expect(reply).toBe('hi back from Sid')
at Object.<anonymous> (src/mup/Actor.test.js:10:36)
这告诉我我的测试失败的地方,但我更想知道实际的错误在哪里。测试不是最终目标,一个工作的应用程序才是。
我在谷歌上搜索了一下,发现并尝试了这个,但它给出了同样的错误信息。node --trace-warnings node_modules/.bin/jest --no-cache
➜ server git:(dc/build) ✗ npm run test
> cbg@0.1.0 test /Users/dc/dev/exiteer/xbot/server
> node --trace-warnings node_modules/.bin/jest --no-cache
(node:23263) UnhandledPromiseRejectionWarning: ReferenceError: doc is not defined
at emitUnhandledRejectionWarning (internal/process/promises.js:151:15)
at processPromiseRejections (internal/process/promises.js:211:11)
at processTicksAndRejections (internal/process/task_queues.js:98:32)
(node:23263) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
它提供了更多的信息
at emitUnhandledRejectionWarning (internal/process/promises.js:151:15)
at processPromiseRejections (internal/process/promises.js:211:11)
at processTicksAndRejections (internal/process/task_queues.js:98:32)
这对调试我的代码没有太大帮助。
而且Jest似乎吞噬了所有的console.log
,好像它在尽最大努力让调试变得尽可能痛苦。
1条答案
按热度按时间hfsqlsce1#
这是Node.js或Jest中的一个bug,具体取决于你的观点--我几个月前修复了这个问题。
这是因为Jest在JavaScript中抛出来自不同领域的错误-所以
instanceof Error
失败。如果您被迫使用旧版本的Node或Jest,这里有一个解决方案:
我们在Node 1.3中添加了这个钩子,所以它应该可以安全地用于几乎所有的Node代码。