javascript - 如果测试有错误,Karma 不会失败

标签 javascript unit-testing karma-runner

当由于错误(如语法错误)导致 Karma 无法运行测试时,Karma 会发出这样的警告:

INFO [karma]: Karma v0.12.31 server started at http://localhost:9876/
INFO [launcher]: Starting browser PhantomJS
INFO [PhantomJS 1.9.8 (Linux)]: Connected on socket WT-AsaXZq4odkQe2DgZJ with id 10861626
WARN [web-server]: 404: /undefinedhttp%3A%2F%2Flocalhost%3A9876%2Fbase%2Ftests%2Funit%2Fjs%2Fspecs%2Fcore%2FDeviceSpec.js%3F1e346ab1ad6e6e9240be6a6d5effaaa8f0dd96d8/9/%22ReferenceError%3A%20Can't%20find%20variable%3A%20SYNTAXERROR%22

未转义的最后一行是:“/undefinedhttp://localhost:9876/base/tests/unit/js/specs/core/DeviceSpec.js?1e346ab1ad6e6e9240be6a6d5effaaa8f0dd96d8/9/”ReferenceError: Can't find variable: SYNTAXERROR” "

这是正确的,因为我出于该问题的目的在测试中的某处插入了 SYNTAXERROR。

因此测试被忽略,但是,由于其他测试都正常,Karma 返回测试已通过:

 PhantomJS 1.9.8 (Linux): Executed 41 of 41 SUCCESS (0.354 secs / 0.016 secs)
➜ echo $?
0

如果 Karma 不能执行所有测试,我希望它失败(我想不出有什么理由不这样做!)。

我还没有找到任何配置变量以这种方式运行。

我找到了 Karma 打印 warning above 的行但我不明白为什么它被处理为 404...

有什么想法可以让 Karma 在无法执行所有测试时失败吗?

最佳答案

如果 WARN 被 karma 显示,我使用的脚本会失败:

#!/bin/bash
set -eo pipefail

mkdir -p tests/unit/js/dist || true
./node_modules/.bin/karma start tests/unit/js/karma.conf.js --port 9875 | tee tests/unit/js/dist/results.txt

if grep 'WARN' tests/unit/js/dist/results.txt; then
    exit 1
else
    exit 0
fi

关于javascript - 如果测试有错误,Karma 不会失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30105924/

相关文章:

javascript - 我需要在测试中注入(inject)插件,否则我会收到错误日志

javascript - WebStorm、Karma 和 Angular。如何调试 Angular 服务

node.js - 在 Karma 中设置 NODE_ENV 以运行 webpack 的正确方法是什么?

javascript - ubuntu 上的 meteor 错误。退出并显示代码 : 1 => Your application is crashing. 等待文件更改

python - 来自其他模块的模拟功能

android - Robolectric 不执行 Runnable

unit-testing - 单元测试作为构建的一部分

javascript - 如何以相反的顺序迭代 JavaScript 对象?

javascript - 如何使用谷歌地图准确获取当前地址?

javascript - 如何在 Cypress 中输入一个很长的字符串来测试输入框?