简体   繁体   English

运行 ng test 时缺少声明文件

[英]Declaration file missing when running ng test

I recently started working on an existing Angular project The unit tests have been broken for quite some time.我最近开始研究现有的 Angular 项目 单元测试已经中断了很长一段时间。 Unfortunatey, I haven't been able to get them running due to reference errors to an object that is defined in a declaration file.不幸的是,由于对声明文件中定义的 object 的引用错误,我无法让它们运行。 This declaration file is located in the root of the project.此声明文件位于项目的根目录中。 At runtime, the actual library is loaded from our development servers and everything works ok.在运行时,实际的库是从我们的开发服务器加载的,一切正常。

When running ng test however, Jasmine is complaining about references not being found:然而,在运行ng test时,Jasmine 抱怨找不到引用:

    ReferenceError: SomeObject is not defined
        at <Jasmine>
        at MyService.getDetails (http://localhost:9876/_karma_webpack_/src/app/MyService.service.ts:196:33)

This is the tsconfig for the running app, which is running fine:这是正在运行的应用程序的 tsconfig,运行良好:

{
  "extends": "../tsconfig.base.json",
  "compilerOptions": {
    "outDir": "../out-tsc/app",
    "baseUrl": "./",
    "experimentalDecorators": true,
    "types": []
  },
  "files": [
    "main.ts",
    "polyfills.ts",
    "someobject.d.ts"
  ],
  "include": [
    "src/**/*.d.ts"
  ]
}

The test configuration is similar, but gives the above error:测试配置类似,但出现上述错误:

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

The someobject.d.ts file is located in the root of the project, at the same level as polyfills.ts and main.ts. someobject.d.ts 文件位于项目的根目录,与 polyfills.ts 和 main.ts 处于同一级别。

This is only the second Angular app I have worked on, so I'm not that experienced in this area.这只是我从事的第二个 Angular 应用程序,所以我在这方面的经验并不多。 It is also the first time that I have encountered a TypeScript declaration file.也是第一次遇到TypeScript声明文件。 Most resources I found online are about dealing with declaration files distributed through npm for libraries like lodash.我在网上找到的大多数资源都是关于处理通过 npm 为 lodash 等库分发的声明文件。 I found little to none information to work with declaration files in the project like this.我发现几乎没有信息可以在这样的项目中使用声明文件。

I'm looking for some pointers in the right direction to go from here.我正在从这里寻找一些指向 go 的正确方向的指针。

Obviously, I edited class and filenames as I'm not at liberty to share source code verbatim.显然,我编辑了 class 和文件名,因为我不能随意逐字分享源代码。

After trying several things, I settled by adding the actual library and include it only for testing in angular.json:在尝试了几件事之后,我通过添加实际库来解决问题,并将其仅包含在 angular.json 中进行测试:

"test": {
    "builder": "@angular-devkit/build-angular:karma",
    "options": {
        ...
        "scripts": [
            "someobject.js"
        ],
        ....
    }
}

This way, the script is available when testing, but not included in production where it is provided by an external service.这样,脚本在测试时可用,但不包含在由外部服务提供的生产环境中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM