1

我正在使用 grunt-triggered Cucumber JS 任务尝试一些带有 nodeJS 的 BDD,它工作正常,除了我不想看到完整的 JS 异常堆栈以进行我知道当然会失败的测试。相反,我宁愿只看到异常的(更有用的)头部。这可能看起来微不足道,但我相信最好通过这些工具的快速反馈来实现 BDD。例如,以下是模块中基本缺失方法的完整堆栈跟踪:

TypeError: Object #<Object> has no method 'delete'
    at World.<anonymous> ($HOME/features/step_definitions/delete-item.steps.js:20:21)
    at Object.invoke ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/support_code/step_definition.js:84:14)
    at Object.execute ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/ast/step.js:157:22)
    at Object.acceptVisitor ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/ast/step.js:149:12)
    at Object.executeStep ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:178:12)
    at Object.processStep ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:173:14)
    at $HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:66:16
    at callUserFunctionAndBroadcastAfterEvent ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:90:9)
    at iterate ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:14:11)
    at Object.forEach ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:23:7)
    at broadcastToListeners ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:117:19)
    at onRuntimeListenersComplete ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:113:9)
    at iterate ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:14:11)
    at $HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:20:11
    at hear ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/listener.js:8:9)
    at $HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/listener/summary_formatter.js:16:7
    at Object.hear ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/listener.js:8:9)
    at Object.hear ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/listener/summary_formatter.js:15:18)
    at $HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:118:51
    at processItem ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:19:9)
    at iterate ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:12:11)
    at Object.forEach ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:23:7)
    at broadcastToListeners ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:117:19)
    at Object.broadcastEvent ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:109:7)
    at Object.broadcastBeforeEvent ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:100:12)
    at Object.broadcastEventAroundUserFunction ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:84:12)
    at Object.visitStep ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:63:12)
    at $HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/ast/scenario.js:95:17
    at processItem ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:19:9)
    at iterate ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:12:11)
    at $HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:20:11
    at iterate ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:14:11)
    at Object.forEach ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:23:7)
    at broadcastToListeners ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:117:19)
    at onRuntimeListenersComplete ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:113:9)
    at iterate ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:14:11)
    at $HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:20:11
    at hear ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/listener.js:8:9)
    at $HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/listener/summary_formatter.js:16:7
    at Object.hear ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/listener.js:8:9)
    at Object.hear ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/listener/summary_formatter.js:15:18)
    at $HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:118:51
    at processItem ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:19:9)
    at iterate ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:12:11)
    at Object.forEach ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:23:7)
    at broadcastToListeners ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:117:19)
    at Object.broadcastEvent ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:109:7)
    at Object.broadcastAfterEvent ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:105:12)
    at $HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:95:32
    at iterate ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:14:11)
    at Object.forEach ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:23:7)
    at broadcastToListeners ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:117:19)
    at onRuntimeListenersComplete ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:113:9)
    at iterate ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:14:11)
    at $HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:20:11
    at handleStepResult ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/listener/summary_formatter.js:27:5)
    at hear ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/listener.js:6:9)
    at $HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/listener/summary_formatter.js:16:7
    at handleStepResult ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/listener/stats_journal.js:39:5)
    at Object.hear ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/listener.js:6:9)
    at Object.hear ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/listener/summary_formatter.js:15:18)
    at $HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:118:51
    at processItem ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:19:9)
    at iterate ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:12:11)
    at Object.forEach ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:23:7)
    at broadcastToListeners ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:117:19)
    at Object.broadcastEvent ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:109:7)
    at Object.visitStepResult ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:79:12)
    at $HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/ast/step.js:150:17
    at codeCallback ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/support_code/step_definition.js:61:11)
    at World.<anonymous> ($HOME/features/step_definitions/delete-item.steps.js:16:3)
    at Object.invoke ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/support_code/step_definition.js:84:14)
    at Object.execute ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/ast/step.js:157:22)
    at Object.acceptVisitor ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/ast/step.js:149:12)
    at Object.executeStep ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:178:12)
    at Object.processStep ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:173:14)
    at $HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:66:16
    at callUserFunctionAndBroadcastAfterEvent ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:90:9)
    at iterate ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:14:11)
    at Object.forEach ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:23:7)
    at broadcastToListeners ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:117:19)
    at onRuntimeListenersComplete ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:113:9)
    at iterate ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:14:11)
    at $HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:20:11
    at hear ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/listener.js:8:9)
    at $HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/listener/summary_formatter.js:16:7
    at Object.hear ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/listener.js:8:9)
    at Object.hear ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/listener/summary_formatter.js:15:18)
    at $HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:118:51
    at processItem ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:19:9)
    at iterate ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:12:11)
    at Object.forEach ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:23:7)
    at broadcastToListeners ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:117:19)
    at Object.broadcastEvent ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:109:7)
    at Object.broadcastBeforeEvent ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:100:12)
    at Object.broadcastEventAroundUserFunction ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:84:12)
    at Object.visitStep ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:63:12)
    at $HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/ast/scenario.js:95:17
    at processItem ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:19:9)
    at iterate ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:12:11)
    at Object.forEach ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:23:7)
    at Object.instructVisitorToVisitSteps ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/ast/scenario.js:94:13)
    at Object.instructVisitorToVisitScenarioSteps ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/ast/scenario.js:90:12)
    at $HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/ast/scenario.js:75:14
    at Object.instructVisitorToVisitBackgroundSteps ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/ast/scenario.js:85:9)
    at Object.acceptVisitor ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/ast/scenario.js:74:12)
    at $HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:48:41
    at callUserFunction ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/support_code/library/hooker.js:41:11)
    at iterate ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:14:11)
    at Object.forEach ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:23:7)
    at Object.triggerBeforeHooks ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/support_code/library/hooker.js:63:19)
    at callBeforeHooks ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/support_code/library/hooker.js:37:16)
    at iterate ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:14:11)
    at Object.forEach ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:23:7)
    at hookedUpFunction ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/support_code/library/hooker.js:27:21)
    at callUserFunctionAndBroadcastAfterEvent ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:90:9)
    at iterate ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:14:11)
    at Object.forEach ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:23:7)
    at broadcastToListeners ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:117:19)
    at onRuntimeListenersComplete ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:113:9)
    at iterate ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:14:11)
    at $HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:20:11
    at hear ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/listener.js:8:9)
    at $HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/listener/summary_formatter.js:16:7
    at handleBeforeScenarioEvent ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/listener/stats_journal.js:24:5)
    at Object.hear ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/listener.js:6:9)
    at Object.hear ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/listener/summary_formatter.js:15:18)
    at $HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:118:51
    at processItem ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:19:9)
    at iterate ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:12:11)
    at Object.forEach ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/type/collection.js:23:7)
    at broadcastToListeners ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:117:19)
    at Object.broadcastEvent ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:109:7)
    at Object.broadcastBeforeEvent ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:100:12)
    at Object.broadcastEventAroundUserFunction ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:84:12)
    at $HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/runtime/ast_tree_walker.js:52:14
    at $HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/support_code/library.js:71:11
    at process._tickCallback (node.js:419:13)
    at Function.Module.runMain (module.js:499:11)
    at startup (node.js:119:16)
    at node.js:906:3

然后是更有用但仍然不明确的:

Failing scenarios:
$HOME/features/delete-item.feature:6 # Scenario: Item removed from portfolio
$HOME/features/delete-item.feature:11 # Scenario: Item not accessible from portfolio

4 scenarios (2 failed, 2 passed)
12 steps (2 failed, 2 skipped, 8 passed)

实际上,在大多数情况下,我唯一关心的是前四行:

TypeError: Object #<Object> has no method 'delete'
at World.<anonymous> ($HOME/features/step_definitions/delete-item.steps.js:20:21)
at Object.invoke ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/support_code/step_definition.js:84:14)
at Object.execute ($HOME/node_modules/grunt-cucumber/node_modules/cucumber/lib/cucumber/ast/step.js:157:22)

有任何想法吗?

4

1 回答 1

1

您可以绝对限制 node.js 中收集的堆栈跟踪的大小。

在您的代码中,设置以下内容以仅记录错误消息(标头)并且不记录堆栈跟踪

Error.stackTraceLimit = 0;

或者,仅查看前四行(错误消息加上 3 个堆栈帧)使用

Error.stackTraceLimit = 3;

该值为节点提供了要收集的最大堆栈帧数,因此您可以将其设置为在任何给定上下文中适合您的错误日志记录目的的任何值。

如果您需要在任何时候将其重新设置以收集完整的堆栈跟踪,您可以使用

Error.stackTraceLimit = Infinity;

更新

您可能还想查看使用hide-stack-frames-from

这通过从堆栈跟踪中过滤掉不需要的帧,只留下相关的帧,而不是截断堆栈并希望前 n 行将指向您根本问题。

例如,您可以将上面的行替换为

require('hide-stack-frames-from')('cucumber', 'bluebird', 'superagent');

只需传递要过滤掉其堆栈帧的任何模块的名称。

于 2015-02-19T17:31:14.050 回答