【问题标题】:Add tests to an Angular 'minimal' CLI app将测试添加到 Angular“最小”CLI 应用程序
【发布时间】:2018-06-05 18:59:12
【问题描述】:

我使用ng new my-app --minimal 搭建了一个 Angular 5 CLI 应用程序以供使用。这样做了一段时间后,我发现我确实想要一个规范文件。所以我所做的是:

  1. 使用类创建文件/src/app/my-thing.ts
  2. 创建一个文件/src/app/my-thing.spec.ts,内容为describe('MyThing', () => { ... }

但显然describe(...)找不到,因为--minimal skips setting up tests,所以没有什么支持的。

如何正确添加与ng test 配合使用的测试设置?

【问题讨论】:

  • 你肯定需要茉莉花
  • @BradleyDotNET 谢谢回复。我的第一次尝试只是npm i jasmine 并从那里开始,但结果并不好,所以过了一会儿我决定做一个更“蛮力”的方法。我已将其作为答案分享,但仍然希望有人能教我一个更聪明的方法/技巧。

标签: angular


【解决方案1】:

我更愿意了解是否有基于 CLI 的解决方案,但现在我只是继续推进并使用了一些 git、diff 和 cli 魔法来了解启动和运行所需的内容。这是愚蠢的解决方案:

  1. 更新.angular-cli.json删除:

    "class": {
      "spec": false
    }
    
  2. .angular-cli.json 设置"spec": false 更新为true "component" 内的任何地方

  3. 将这些添加到package.json:

    "@types/jasmine": "~2.8.3",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "~6.0.60",
    "jasmine-core": "~2.8.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~2.0.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    

    并运行 npm install 以安装这些软件包。

  4. 在项目根目录的新文件karma.conf.js 中添加这个(或类似的):

    module.exports = function (config) {
      config.set({
        basePath: '',
        frameworks: ['jasmine', '@angular/cli'],
        plugins: [
          require('karma-jasmine'),
          require('karma-chrome-launcher'),
          require('karma-jasmine-html-reporter'),
          require('karma-coverage-istanbul-reporter'),
          require('@angular/cli/plugins/karma')
        ],
        client:{
          clearContext: false // leave Jasmine Spec Runner output visible in browser
        },
        coverageIstanbulReporter: {
          reports: [ 'html', 'lcovonly' ],
          fixWebpackSourcePaths: true
        },
        angularCli: {
          environment: 'dev'
        },
        reporters: ['progress', 'kjhtml'],
        port: 9876,
        colors: true,
        logLevel: config.LOG_INFO,
        autoWatch: true,
        browsers: ['Chrome'],
        singleRun: false
      });
    };
    
  5. 将此添加到新文件src/test.ts

    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);
    
  6. 添加这个新文件src/tsconfig.spec.json:

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

现在您应该可以运行 ng test 并看到您的测试运行了。

希望这对某人有所帮助。

【讨论】:

    猜你喜欢
    • 2020-01-13
    • 2016-11-16
    • 2016-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-11
    • 2019-01-03
    相关资源
    最近更新 更多