【问题标题】:Angular compiler can't find jasmine.CustomMatchers even though it's in the @types/jasmineAngular 编译器找不到 jasmine.CustomMatchers,即使它位于 @types/jasmine
【发布时间】:2019-04-29 03:41:39
【问题描述】:

我刚刚从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 时,我可以看到在 index.d.ts 中定义了 CustomMatchers

角度 7

打字稿 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",

【问题讨论】:

    标签: angular typescript karma-jasmine


    【解决方案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.jsonnode_modules/ 然后执行 npm cache clean --forcenpm install

    【讨论】:

    • 我在 Angular 9 上,我的类型脚本版本为 "typescript": "3.7.5" ,如果我使用 "@types/jasmine": "2.8.9" 问题仍然存在
    【解决方案2】:

    我有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"
    }
    

    【讨论】:

      猜你喜欢
      • 2021-08-15
      • 1970-01-01
      • 2021-07-25
      • 2020-02-15
      • 1970-01-01
      • 1970-01-01
      • 2019-12-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多