首页 文章

Angular 2单元测试:找不到名称'describe'

提问于
浏览
154

我正在关注this tutorial from angular.io

正如他们所说,我创建了hero.spec.ts文件来创建单元测试:

import { Hero } from './hero';
describe('Hero', () => {
  it('has name', () => {
    let hero: Hero = {id: 1, name: 'Super Cat'};
    expect(hero.name).toEqual('Super Cat');
  });
  it('has id', () => {
    let hero: Hero = {id: 1, name: 'Super Cat'};
    expect(hero.id).toEqual(1);
  });
});

单元测试就像魅力一样 . 问题是:我看到一些错误,这些错误在教程中提到:

我们的编辑和编译器可能会抱怨他们不知道它和期望是什么,因为他们缺少描述Jasmine的打字文件 . 我们现在可以忽略那些烦人的抱怨,因为它们是无害的 .

他们确实忽略了它 . 即使这些错误是无害的,但当我收到大量错误时,它在我的输出控制台中看起来并不好看 .

我得到的例子:

找不到名字'描述' . 找不到名字'它' . 找不到名字'期待' .

我该怎么做才能解决它?

12 回答

  • 3

    在我的情况下,解决方案是删除 tsconfig.json 中的 typeRoots .

    你可以阅读TypeScript doc

    如果指定了typeRoots,则仅包含typeRoots下的包 .

  • 130

    我'm on ' Angular 6 ', ' typescript 2.7 ', and I' m使用' jest ' framework to unit test, I had ' @types/jest ' installed and added on ' typeRoots ' inside ' tsconfig.json ' .
    但是仍然有下面的显示错误(即:在终端上没有错误)

    找不到名字描述

    并添加导入:

    import {} from 'jest'; // in my case or jasmine if you're using jasmine
    

    在技术上没有做任何事情,所以我想,有一个导入这个问题的导入,然后我发现,如果删除文件

    tsconfigspec.json

    src/ 文件夹中,为我解决了问题 . 因为 @typesrootTypes 之前导入 .
    我建议你做同样的事情并删除这个文件,里面不需要配置 . (ps:如果你和我一样的情况)
    ✌✌✌

  • 12

    我已达到今天的最新状态,并发现解决此问题的最佳方法是什么也不做......没有 typeRoots 没有 types 没有 exclude 没有 include 所有默认设置似乎都运行得很好 . 实际上,除非我将它们全部删除,否则它不适合我 . 我有: "exclude": [ "node_modules" ] 但这是默认值,所以我删除了 .

    我有: "types": [ "node" ] 来通过一些编译器警告 . 但是现在我也删除了它 .

    不应该的警告是: error TS2304: Cannot find name 'AsyncIterable'. 来自 node_modules\@types\graphql\subscription\subscribe.d.ts

    这是非常令人讨厌的所以我在tsconfig中这样做,所以它加载它: "compilerOptions": { "target": "esnext", } 因为它's in the esnext set. I'米没有直接使用它所以不用担心这里的兼容性 . 希望以后不会烧我 .

  • 1

    使用Typescript@2.0或更高版本,您可以使用npm install安装类型

    npm install --save-dev @types/jasmine
    

    然后使用tsconfig.json中的 typeRoots 选项自动导入类型 .

    "typeRoots": [
          "node_modules/@types"
        ],
    

    此解决方案不需要来自'jasmine'的import {};在每个spec文件中 .

  • 2
    npm install @types/jasmine
    

    正如在一些评论中所提到的,不再需要 "types": ["jasmine"] ,所有 @types 包都自动包含在编译中(因为我认为是v2.1) .

    在我看来,最简单的解决方案是排除 tsconfig.json 中的测试文件,如:

    "exclude": [
        "node_modules",
        "**/*.spec.ts"
    ]
    

    这对我有用 .

    官方tsconfig docs中的更多信息 .

  • 5

    你需要为茉莉花安装打字 . 假设您使用的是相对较新版本的typescript 2,您应该可以:

    npm install --save-dev @types/jasmine

  • 0

    看看导入可能你有 cycle dependency ,这是我的情况下的错误,使用 import {} from 'jasmine'; 将修复控制台中的错误并使代码可编译但不删除恶魔的根(在我的情况下循环依赖) .

  • 313

    这个问题的解决方案与他的答案中的@Pace has written有关 . 但是,它没有意思,我会自己写 .

    SOLUTION:

    添加此行:

    ///<reference path="./../../../typings/globals/jasmine/index.d.ts"/>
    

    hero.spec.ts 文件的开头修复了问题 . 路径指向 typings 文件夹(存储所有类型的文件夹) .

    要安装打字,您需要在项目的根目录中创建 typings.json 文件,其中包含以下内容:

    {
      "globalDependencies": {
        "core-js": "registry:dt/core-js#0.0.0+20160602141332",
        "jasmine": "registry:dt/jasmine#2.2.0+20160621224255",
        "node": "registry:dt/node#6.0.0+20160807145350"
      }
    }
    

    并运行 typings install (其中 typings 是NPM包) .

  • 2

    使用Typescript@2.0或更高版本,您可以使用以下命令安装类型:

    npm install -D @types/jasmine
    

    然后使用 tsconfig.json 中的 types 选项自动导入类型:

    "types": ["jasmine"],
    

    此解决方案在每个spec文件中不需要 import {} from 'jasmine'; .

  • 22

    为了使TypeScript编译器在编译期间使用所有可见的类型定义,应该从 tsconfig.json 文件中的 compilerOptions 字段中完全删除 types 选项 .

    compilerOptions 字段中存在一些 types 条目时,会出现此问题,同时缺少 jest 条目 .

    因此,为了解决问题, tscongfig.json 中的 compilerOptions 字段应该在 types 区域中包含 jest 或者完全摆脱 types

    {
      "compilerOptions": {
        "esModuleInterop": true,
        "target": "es6",
        "module": "commonjs",
        "outDir": "dist",
        "types": ["reflect-metadata", "jest"],  //<--  add jest or remove completely
        "moduleResolution": "node",
        "sourceMap": true
      },
      "include": [
        "src/**/*.ts"
      ],
      "exclude": [
        "node_modules"
      ]
    }
    
  • 0

    我希望你已安装 -

    npm install --save-dev @types/jasmine

    然后将以下导入放在 hero.spec.ts 文件的顶部 -

    import {} from 'jasmine';

    它应该解决问题 .

  • 0

    只需要执行以下操作即可在存在多个node_module的Lerna Mono-repo中选择@types .

    npm install -D @types/jasmine
    

    然后在每个模块或应用程序的每个tsconfig.file中

    "typeRoots": [
      "node_modules/@types",
      "../../node_modules/@types" <-- I added this line
    ],
    

相关问题