Angular 卡玛·茉莉花:进口间谍,无伤大雅

Angular 卡玛·茉莉花:进口间谍,无伤大雅,angular,typescript,jasmine,karma-jasmine,Angular,Typescript,Jasmine,Karma Jasmine,如果我在Angular 4+项目的tsconfig.json文件中设置此选项: "noImplicitAny": true, …并将其导入单元测试中使用: import { Spy } from "karma-jasmine"; …我在npm测试中看到此控制台错误: ERROR in C:/test-project/src/app/components/test/test.component.spec.ts (1,21): Could not find a declaration file

如果我在Angular 4+项目的
tsconfig.json
文件中设置此选项:

"noImplicitAny": true,
…并将其导入单元测试中使用:

import { Spy } from "karma-jasmine";
…我在npm测试中看到此控制台错误:

ERROR in C:/test-project/src/app/components/test/test.component.spec.ts (1,21):
Could not find a declaration file for module 'karma-jasmine'.
'C:/test-project/node_modules/karma-jasmine/lib/index.js' implicitly has an 'any' type.
Try `npm install @types/karma-jasmine` if it exists or add a new declaration (.d.ts) file containing `declare module 'karma-jasmine';`
ERROR in C:/test-project/src/app/components/test/test.component.spec.ts (1,21):
File 'C:/test-project/node_modules/@types/karma-jasmine/index.d.ts' is not a module.

我试过这个:

  • npm安装——保存dev@types/karma jasmine

  • 添加
    “类型”:[“karma jasmine”]
    tsconfig.json

  • …我现在在npm测试中看到控制台错误:

    ERROR in C:/test-project/src/app/components/test/test.component.spec.ts (1,21):
    Could not find a declaration file for module 'karma-jasmine'.
    'C:/test-project/node_modules/karma-jasmine/lib/index.js' implicitly has an 'any' type.
    Try `npm install @types/karma-jasmine` if it exists or add a new declaration (.d.ts) file containing `declare module 'karma-jasmine';`
    
    ERROR in C:/test-project/src/app/components/test/test.component.spec.ts (1,21):
    File 'C:/test-project/node_modules/@types/karma-jasmine/index.d.ts' is not a module.
    

    如果设置了
    “noImplicitAny”:true
    ,如何导入
    Spy

    如果将其设置为
    false
    (默认值),则导入和使用不会出错

    用法示例:

    const testSpy: Spy = spyOn(testService, "test").and.callThrough();
    

    这是因为框架的名称是Jasmine。Karma是规范中不应该提到的测试运行程序,
    Karma jasmine
    是Karma插件,可以无缝运行jasmine规范

    因为茉莉花是,它应该是:

    import * as jasmine from "jasmine";
    
    const spy: jasmine.Spy = ...;
    

    jasmine
    应该是全局的,
    jasmine.Spy
    可以直接使用,而无需导入

    Spy是jasmine API的一部分。因果报应茉莉花将茉莉花融入因果报应@JBNizet yes除了从“jasmine”导入Spy失败外,
    [ts]模块“jasmine”没有导出的成员“Spy”
    。您可以使用
    const foo:jasmine.Spy
    。或者
    import Spy=jasmine.Spy
    如果您只想使用
    const foo:Spy
    。啊,我明白了,这是一个简单的解决方法:)谢谢@jbniset,如果这是您的答案,我会接受。这会导致TypeScript编译器失败,模块“jasmine”没有导出成员“Spy”。我更新了答案。事实上,接口通常是命名的,这样很明显,
    Spy
    属于
    jasmine
    而不是一个类取而代之,但现在我明白了:)