Angular编译器即使在@ types / jasmine中也找不到jasmine.CustomMatchers

时间:2019-01-17 20:46:45

标签: angular typescript karma-jasmine

我刚刚从Angular guide下载了示例测试项目: https://angular.io/generated/zips/testing/testing.zip

ng serve出现错误:

src/testing/jasmine-matchers.d.ts(3,67): error TS2694: Namespace 'jasmine' has no exported member 'CustomMatcher'.

我对Angular很陌生。有人可以帮助我了解问题所在吗?

我已经完成npm install,当我转到node_modules / @ types / jasmine时,我看到CustomMatchers是在index.d.ts中定义的

Angular 7

Typescript 3.0

"@types/jasmine": "^2.8.14",
"@types/jasminewd2": "^2.0.4",
"@types/node": "~8.9.4",
"jasmine-core": "~2.99.1",
"jasmine-marbles": "^0.4.0",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~3.0.0",

2 个答案:

答案 0 :(得分:1)

事实证明,错误是由Typescript和@ types / jasmine软件包的不兼容版本引起的。以下版本应该可以工作:

Angular CLI: 6.2.9
Node: 10.11.0
OS: darwin x64
Angular: 6.1.10
typescript                        2.7.2
"@types/jasmine": "2.8.9"

要安装正确的版本,请执行以下操作:

删除 package-lock.json node_modules / ,然后执行npm cache clean --forcenpm install

答案 1 :(得分:0)

我有nodenpm版本,如下所示:

node --version
v10.15.3

npm --version
6.9.0

我收到如下错误:

ERROR in src/app/model/testing/index.ts(1,15): error TS2307: Cannot find module './fake-hero.service'.
src/testing/jasmine-matchers.d.ts(3,67): error TS2694: Namespace 'jasmine' has no exported member 'CustomMatcher'.
src/testing/jasmine-matchers.ts(7,3): error TS2708: Cannot use namespace 'jasmine' as a value.
src/testing/jasmine-matchers.ts(12,32): error TS2694: Namespace 'jasmine' has no exported member 'CustomMatcher'.
src/testing/jasmine-matchers.ts(14,97): error TS2694: Namespace 'jasmine' has no exported member 'CustomMatcherResult'.

在文件src/app/model/testing/index.ts中注释如下一行,即可解决此问题。

//export * from './fake-hero.service';

似乎它也存在与jasmineCustomMatcher相关的错误。但事实并非如此。

有趣的是,当我停止服务器并使用ng serve重新运行时,我再次遇到jasmin相关的错误。但是当我通过在注释行中放置一个空格来编辑上述文件(index.ts)时,该文件已成功编译!

这是我的devDependencies:

"devDependencies": {
  "@angular-devkit/build-angular": "^0.11.0",
  "@angular/cli": "^7.1.0",
  "@angular/compiler-cli": "^7.1.0",
  "@angular/platform-server": "^7.1.0",
  "@types/jasmine": "~2.8.8",
  "@types/jasminewd2": "^2.0.4",
  "@types/node": "~8.9.4",
  "jasmine-core": "~2.99.1",
  "jasmine-marbles": "^0.4.0",
  "jasmine-spec-reporter": "~4.2.1",
  "karma": "~3.0.0",
  "karma-chrome-launcher": "~2.2.0",
  "karma-coverage-istanbul-reporter": "~2.0.1",
  "karma-jasmine": "~1.1.2",
  "karma-jasmine-html-reporter": "^0.2.2",
  "lodash": "^4.16.2",
  "protractor": "~5.4.0",
  "ts-node": "~7.0.0",
  "tslint": "~5.11.0",
  "typescript": "~3.1.1"
}