【问题标题】:Angular testing - test.ts not found角度测试 - 未找到 test.ts
【发布时间】:2018-10-27 02:22:12
【问题描述】:

我正在尝试按照官方 Angular 文档为 Angular 项目设置测试 - https://angular.io/guide/testing#service-tests

我使用第一个(顶部)链接从上面的页面下载了示例 Angular 项目。我已经完成了npm install,当我运行ng serve 时,它构建得很好。

当我使用 CLI 运行 ng test 时,我收到以下消息:

ERROR in Entry module not found: Error: Cant' resolve 'C:Code\testing\src\test.ts' in 'C:\Code\testing'

ERROR in error TS6053: File 'C:Code\testing\src\test.ts' not found.

我查看了这个问题 - How to resolve test.ts when running ng test?,但在这种情况下该文件确实存在,但在 Angular 示例项目中它根本不存在。

(当我第一次运行ng test 时,我最初收到一条关于茉莉花弹珠丢失的消息,我使用以下方法解决了这个问题:)

npm install jasmine-marbles --save

文档说:

您可以通过编辑 karma.conf.js 和 src/ 文件夹中的 test.ts 文件。

所以我知道 test.cs 是某种配置文件,但是如何生成它呢?它也不存在于 Angular 的“实时示例”项目中。我怎么知道我生成的 test.cs 可以在这个项目中可靠地工作?

【问题讨论】:

    标签: javascript angular unit-testing testing jasmine


    【解决方案1】:

    我使用@joshbaeha 对ng new 一个新的Angular 5 项目的建议并复制了test.ts 文件,该文件似乎是完全通用的,并且不依赖于项目结构或其他任何东西。现在一切正常。这里是:

    test.ts

    // This file is required by karma.conf.js and loads recursively all the .spec and framework files
    
    import 'zone.js/dist/zone-testing';
    import { getTestBed } from '@angular/core/testing';
    import {
      BrowserDynamicTestingModule,
      platformBrowserDynamicTesting
    } from '@angular/platform-browser-dynamic/testing';
    
    declare const require: any;
    
    // First, initialize the Angular testing environment.
    getTestBed().initTestEnvironment(
      BrowserDynamicTestingModule,
      platformBrowserDynamicTesting()
    );
    // Then we find all the tests.
    const context = require.context('./', true, /\.spec\.ts$/);
    // And load the modules.
    context.keys().map(context);
    

    【讨论】:

      【解决方案2】:

      我不知道你是否可以只生成 test.ts 文件,但据我所知,当你使用 angular-cli 创建一个新的 angular 项目时会自动生成这个文件。因此,您可以使用 angular-cli 创建一个新项目,然后从该新项目中复制 src/test.ts 文件

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-14
        • 2017-05-13
        • 2021-09-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多