【问题标题】:Angular 4 CLI cannot find name 'jasmine'Angular 4 CLI 找不到名称“茉莉花”
【发布时间】:2017-09-18 23:48:21
【问题描述】:

我正在使用 Angular 4 CLI (v.1.0.0) 并处理测试,我创建了一些使用 jasmine 创建间谍的模拟。在 IDE 中,一切看起来都还不错,但是在终端中,我得到了错误提示“找不到名称 'jasmine'”。

起初我认为问题在于没有将 jasmine 添加到类型中,但我可以看到 package.json 包含对 jasmine 类型 def 的导入,所以我不确定缺少什么。

mocks.ts

export class MockAuthService {
  public login: Function = jasmine.createSpy('login');
}

export class MockHttpService {
  public delete: Function = jasmine.createSpy('delete');
  public get: Function = jasmine.createSpy('get');
  public post: Function = jasmine.createSpy('post');
  public put: Function = jasmine.createSpy('put');
}

运行 ng serve 在终端中返回以下错误消息。

C:/Users/efarley/Desktop/repos/prod/src/app/mocks/mocks.ts 中的错误 (2,23):找不到名称“茉莉花”。 C:/Users/efarley/Desktop/repos/prod/src/app/mocks/mocks.ts (6,24): 找不到名称“茉莉花”。 C:/Users/efarley/Desktop/repos/prod/src/app/mocks/mocks.ts (7,21): 找不到名称“茉莉花”。 C:/Users/efarley/Desktop/repos/prod/src/app/mocks/mocks.ts (8,22): 找不到名称“茉莉花”。 C:/Users/efarley/Desktop/repos/prod/src/app/mocks/mocks.ts (9,21): 找不到名称“茉莉花”。 webpack: 编译失败。

tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "baseUrl": "src",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2016",
      "dom"
    ]
  }
}

tsconfig.spec.json

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/spec",
    "module": "commonjs",
    "target": "es5",
    "baseUrl": "",
    "types": [
      "jasmine",
      "node"
    ]
  },
  "files": [
    "test.ts"
  ],
  "include": [
    "**/*.spec.ts",
    "**/*.d.ts"
  ]
}

tsconfig.app.json

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/app",
    "module": "es2015",
    "baseUrl": "",
    "types": []
  },
  "exclude": [
    "test.ts",
    "**/*.spec.ts"
  ]
}

package.json

{
  "name": "prod",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^4.0.2",
    "@angular/common": "^4.0.0",
    "@angular/compiler": "^4.0.0",
    "@angular/core": "^4.0.0",
    "@angular/flex-layout": "^2.0.0-beta.8",
    "@angular/forms": "^4.0.0",
    "@angular/http": "^4.0.0",
    "@angular/material": "^2.0.0-beta.3",
    "@angular/platform-browser": "^4.0.0",
    "@angular/platform-browser-dynamic": "^4.0.0",
    "@angular/router": "^4.0.0",
    "core-js": "^2.4.1",
    "rxjs": "^5.1.0",
    "zone.js": "^0.8.4"
  },
  "devDependencies": {
    "@angular/cli": "1.0.0",
    "@angular/compiler-cli": "^4.0.0",
    "@types/jasmine": "2.5.38",
    "@types/node": "~6.0.60",
    "codelyzer": "~2.0.0",
    "jasmine-core": "~2.5.2",
    "jasmine-spec-reporter": "~3.2.0",
    "karma": "~1.4.1",
    "karma-chrome-launcher": "~2.0.0",
    "karma-cli": "~1.0.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "karma-coverage-istanbul-reporter": "^0.2.0",
    "protractor": "~5.1.0",
    "ts-node": "~2.0.0",
    "tslint": "~4.5.0",
    "typescript": "~2.2.0"
  }
}

【问题讨论】:

  • 您使用的是哪个版本的 cli?另外,您能否发布所有tsconfig 文件(如果有很多)。如果您在 angular-cli v1.0.0 上,您应该在文件 src/tsconfig.spec.json 中看到 "types": [ "jasmine", ...]
  • 我正在运行 angular-cli v1.0.0。我在 tsconfig.json 中没有看到 types 键,但我看到了 typeRoots。我已更新问题以包含配置文件。
  • 你没有src/tsconfig.spec.json 吗?
  • 啊,是的,现在将其添加到问题中。

标签: karma-jasmine angular-cli typescript-typings


【解决方案1】:

您收到错误是因为 TypeScript 试图在 app 构建中包含模拟,而应用程序(正确地)不知道 Jasmine。

通过将模拟添加到tsconfig.app.json 中的exclude 数组中,将模拟从您的应用编译中排除:

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/app",
    "module": "es2015",
    "baseUrl": "",
    "types": []
  },
  "exclude": [
    "test.ts",
    "**/*.spec.ts",
    "**/*.mock.ts"
  ]
}

【讨论】:

  • 这适用于ng serve,但在运行ng build 时似乎没有任何效果。我使用 Angular CLI 1.6.7。有什么建议吗?
  • 好的,我也可以解决这个问题。我仍然在 DevModule 中导入了我的模拟。我现在将 DevModule 和 TestModule 分开,以确保在 ng build 期间不会加载 TestModule。
【解决方案2】:

我不确定您是否应该以这种方式创建 jasmine 间谍,但无论如何:

问题在于您的文件名

注意:tsconfig.spec.json 有:"include": ["**/*.spec.ts","**/*.d.ts"]

你的文件名是mocks.ts

要么把文件名改成mocks.spec.ts

或添加 "**/*.mock.ts" 并重命名为 service.mock.ts

希望这会有所帮助。

【讨论】:

  • mocks.spec.ts 工作。很好的发现。我很好奇你为什么质疑以这种方式创建间谍。这个想法是,我可以创建传递给单元测试的模拟服务,以与已经监视的每个方法一起使用,因此无需在测试规范中声明 spys。如果我需要修改返回值,我可以在需要的测试中轻松地做到这一点。我觉得这样可以更快地编写测试。我有兴趣阅读您发现的任何建议另一种设置模拟服务的方法。
  • 我质疑是因为我还没有看到它这样做,如果是一个好的做法并且适用于您的用例,没问题。很高兴我能帮忙:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-30
  • 2014-12-12
  • 1970-01-01
  • 1970-01-01
  • 2022-01-23
相关资源
最近更新 更多