Angular 5在尝试运行" ng test"时出错

时间:2018-04-24 19:46:13

标签: angular typescript angular5 karma-webpack

我尝试运行ng test时收到错误:

  

HeadlessChrome 65.0.3325(Windows 10.0.0)错误   未捕获的TypeError:无法读取属性' fn'未定义的   在http://localhost:9876/_karma_webpack_/scripts.bundle.js:12

的package.json

 "dependencies": {
    "@angular/animations": "^5.2.0",
    "@angular/common": "^5.2.0",
    "@angular/compiler": "^5.2.0",
    "@angular/core": "^5.2.0",
    "@angular/forms": "^5.2.0",
    "@angular/http": "^5.2.0",
    "@angular/platform-browser": "^5.2.0",
    "@angular/platform-browser-dynamic": "^5.2.0",
    "@angular/router": "^5.2.0",
    "bootstrap": "^4.0.0",
    "core-js": "^2.4.1",
    "font-awesome": "^4.7.0",
    "npm": "^5.8.0",
    "popper.js": "^1.14.1",
    "primeng": "^5.2.3",
    "rxjs": "^5.5.6",
    "zone.js": "^0.8.19"
  },
  "devDependencies": {
    "@angular/cli": "~1.7.0",
    "@angular/compiler-cli": "^5.2.0",
    "@angular/language-service": "^5.2.0",
    "@types/jasmine": "~2.8.3",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "~6.0.60",
    "codelyzer": "^4.0.1",
    "jasmine-core": "~2.8.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~2.0.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "karma-mocha-reporter": "^2.2.5",
    "protractor": "~5.1.2",
    "ts-node": "~4.1.0",
    "tslint": "~5.9.1",
    "typescript": "~2.5.3"
  }

1 个答案:

答案 0 :(得分:0)

我遇到了相同的错误,就我而言,为了解决该错误,我不得不对.angular-cli.json上的脚本重新排序,并将jQuery添加到脚本列表中。

安装jquery:

npm install --save-dev @types/jquery
npm install jquery -save

添加到股票并设置顺序:

"scripts": [
  // any other scripts you may have
  "../node_modules/jquery/dist/jquery.min.js",
  "assets/bootstrap/bootstrap.bundle.min.js"
],

https://github.com/understrap/understrap/issues/449#issuecomment-373323099

并将jquery添加到karma.conf.js

files: [
  './node_modules/jquery/dist/jquery.min.js',
]

https://stackoverflow.com/a/19131293

回答这个旧问题,以防像我这样的人在这里遇到同样的问题。