Karma tests succeed but PhantomJS returns "An error was thrown in afterAll" SyntaxError: Unexpected token ','

Karma tests succeed but PhantomJS returns "An error was thrown in afterAll" SyntaxError: Unexpected token ','

我们所有的 karma 单元测试似乎在我们的自动构建中都成功了,但是 PhantomJS runner returns 构建框架 (TeamCity) 的非零结果。我可以在日志中找到的唯一错误是:

[17:53:31][JavaScript Unit Tests] PhantomJS 2.1.1 (Windows 8.0.0) ERROR
[17:53:31][JavaScript Unit Tests]   An error was thrown in afterAll
[17:53:31][JavaScript Unit Tests]   SyntaxError: Unexpected token ','

项目正在使用:

karma.conf.js:

// Karma Configuration
module.exports = function (config) {
    config.set({
        browsers: ["PhantomJS"],
        files: [
            "scripts/vendor/angularjs/1.4.8/angular.js",
            "scripts/vendor/angularjs/1.4.8/angular-animate.js",
            "scripts/vendor/*.js",
            "scripts/angular/**/*.module.js",
            "scripts/angular/**/*.app.js",
            "scripts/angular/**/*.js",
            "scripts/angular/**/spec/*.spec.js"
        ],
        exclude: [
            "scripts/vendor/angular-1*.js",
            "scripts/vendor/angular-animate.js",
            "scripts/angular/anycastManagementApp/anycastManagement.app.js",
            "scripts/angular/cart/**/.js",
            "scripts/angular/cart/*.js",
            "scripts/vendor/bootstrap.min.js",
            "scripts/angular/angular-bootstrap-datetimepicker/**/*.*"
        ],
        plugins: [
            "karma-jasmine",
            "karma-jasmine-matchers",
            "karma-chrome-launcher",
            "karma-phantomjs-launcher",
            "karma-teamcity-reporter",
            "karma-spec-reporter",
            "karma-coverage"
        ],
        frameworks: ["jasmine", "jasmine-matchers"],
        port: 9000,
        logLevel: config.LOG_DEBUG,
        client: {
            jasmine: {
                random: false
            }
        }
    });
}

我尝试过的事情:

我的下一步:我想我会开始禁用个别测试,看看是否有任何测试对这个 afterAll 结果有影响。

更新:通过 xdescribe 禁用所有测试没有解决错误。所以我知道错误与测试本身无关,而是与 plugins/libraries.

中的某些错误或不兼容有关

显然 PhantomJS 自 2016 年 (v2.1.1) 以来就没有更新过,并且不是一个活跃的项目。看来 recommendation is to replace PhantomJS in your projects with ChromeHeadless.

就连 PhantomJS 的维护者 Vitaliy Slobodin 也谈到了 ChromeHeadless:

I think people will switch to it, eventually. Chrome is faster and more stable than PhantomJS. And it doesn’t eat memory like crazy.

如果能找到一个不依赖于 Google 的无头浏览器就好了,但显然 PhantomJS 无论如何都在使用 WebKit-based 引擎,所以我们并没有真正失去任何浏览器variety/breadth 通过进行此更改。

按照链接文章中的说明,我的问题差不多解决了。我将 运行 转换为另一个与丢失的库引用相关的类似 afterAll error。我怀疑这是与 PhantomJS 相同的错误,但没有被正确报告。切换到 ChromeHeadless 后,错误报告正确并且易于修复。