随机脚本错误。在角单元/集成测试中

时间:2018-07-19 07:24:54

标签: javascript angular jasmine karma-jasmine karma-runner

我有以下问题: 当我使用Karma为Angular项目运行单元/集成测试时,我的测试成功/失败的机会大概为50:50。通常,它可以在我的机器(:D)上运行,但不能在我们的构建服务器上使用,这使得它真的非常不可靠。

失败时,它始终是一条隐秘的错误消息,内容为

  

脚本错误。

...仅此而已。现在,奇怪的是,它始终是另一个失败的测试。

这是我已经做过或看过的东西:

这些是我的依赖项:

"dependencies": {
    "@angular/animations": "^6.0.9",
    "@angular/cdk": "^6.4.0",
    "@angular/common": "^6.0.9",
    "@angular/compiler": "^6.0.9",
    "@angular/core": "^6.0.9",
    "@angular/flex-layout": "^6.0.0-beta.16",
    "@angular/forms": "^6.0.9",
    "@angular/http": "^6.0.9",
    "@angular/material": "^6.4.0",
    "@angular/platform-browser": "^6.0.9",
    "@angular/platform-browser-dynamic": "^6.0.9",
    "@angular/platform-server": "^6.0.9",
    "@angular/router": "^6.0.9",
    "@ngrx/effects": "^6.0.1",
    "@ngrx/router-store": "^6.0.1",
    "@ngrx/store": "^6.0.1",
    "@ngrx/store-devtools": "^6.0.1",
    "core-js": "^2.5.7",
    "devextreme": "^18.1.4",
    "devextreme-angular": "^18.1.4",
    "i": "^0.3.6",
    "jquery": "^3.3.1",
    "ngx-scrollbar": "^2.1.0",
    "node-sass": "^4.9.0",
    "npm": "^6.2.0",
    "rxjs": "^6.2.2",
    "stream": "0.0.2",
    "web-animations-js": "^2.3.1",
    "zone.js": "^0.8.26"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.6.3",
    "@angular/cli": "^6.0.8",
    "@angular/compiler-cli": "^6.0.9",
    "@angular/language-service": "^6.0.9",
    "@types/jasmine": "^2.8.8",
    "@types/jasminewd2": "^2.0.3",
    "@types/node": "^10.5.2",
    "codelyzer": "^4.4.2",
    "fs-extra": "^6.0.1",
    "git-describe": "^4.0.3",
    "gulp": "^4.0.0",
    "gulp-param": "^1.0.3",
    "gulp-run": "^1.7.1",
    "hogan.js": "^3.0.2",
    "husky": "^0.14.3",
    "jasmine-core": "^3.1.0",
    "jasmine-spec-reporter": "^4.2.1",
    "jszip": "^3.1.5",
    "karma": "^2.0.4",
    "karma-chrome-launcher": "~2.2.0",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^2.0.1",
    "karma-jasmine": "^1.1.2",
    "karma-jasmine-html-reporter": "^1.2.0",
    "license-checker": "^20.1.0",
    "lint-staged": "^7.2.0",
    "markdown-include": "^0.4.3",
    "prettier": "^1.13.7",
    "protractor": "^5.3.2",
    "readline-sync": "^1.4.9",
    "run-sequence": "^2.2.1",
    "stylelint": "^9.3.0",
    "stylelint-config-recommended-scss": "^3.2.0",
    "stylelint-config-standard": "^18.2.0",
    "stylelint-scss": "^3.1.3",
    "ts-node": "^6.2.0",
    "tslint": "^5.11.0",
    "tslint-config-prettier": "^1.13.0",
    "typescript": "2.7.2",
    "yargs": "^11.1.0"
  }

下一步我该怎么做?

我很感谢每一个提示...谢谢

2 个答案:

答案 0 :(得分:1)

根据我的经验,很多情况下,此类随机错误是由于脏状态所遗留的一项测试而又导致其他一些测试失败。 AFAIK不需要或未定义Karma的默认顺序。 我会尝试注释掉(二进制搜索样式)测试的前半部分,看看它是否始终如一地通过。

答案 1 :(得分:0)

最终解决了问题。

我以某种方式走上了正确的道路,并弄清楚了哪个测试失败或正在导致其他测试失败。

// causing part in the tests
beforeEach(() => {
   fixture = TestBed.createComponent(FilterSidebarComponent);
   component = fixture.componentInstance;
   component.data = {} as DataClass;
   fixture.detectChanges();
 });

现在的问题是,在组件的订阅中,我调用了类DataClass的特定方法:

this.attributeXY.subscribe(event => {
    this.data.reset(); <-- reset() was undefined
    if (event) {
     // do something 
     ...
    }
  })

现在剩下的问题是,为什么在这种情况下无法投射空对象? 我的期望是,方法reset()也将被推断出