如何在Stackblitz中使用角度设置笑话

时间:2019-07-08 22:29:55

标签: angular unit-testing jestjs stackblitz

有人在stackblitz项目示例中使用玩笑来进行角度单元测试吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

我以前曾经这样做过,但是对于茉莉而言,并不是开玩笑。您可能要遵循并在其上构建:

简要说明

添加依赖项:jasmine-core@types/jasmine

添加/src/global-jasmine.ts文件:

import jasmineRequire from 'jasmine-core/lib/jasmine-core/jasmine.js';
window.jasmineRequire = jasmineRequire;

编辑/src/styles.scss并添加:

@import 'jasmine-core/lib/jasmine-core/jasmine.css';

添加/src/main-testing.ts文件(在main.ts旁边)并粘贴以下内容:

import './global-jasmine'
import 'jasmine-core/lib/jasmine-core/jasmine-html.js';
import 'jasmine-core/lib/jasmine-core/boot.js';

import './polyfills';

// This file is required by karma.conf.js and loads recursively all the .spec and framework files
import 'zone.js/dist/async-test';
import 'zone.js/dist/fake-async-test';
import 'zone.js/dist/long-stack-trace-zone';
import 'zone.js/dist/proxy.js';
import 'zone.js/dist/sync-test';

// Requires 'zone.js/dist/proxy.js' and 'zone.js/dist/sync-test';
import 'zone.js/dist/jasmine-patch';

import { getTestBed } from '@angular/core/testing';
import {
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting
} from '@angular/platform-browser-dynamic/testing';

// stuff to test
import './app/app.component.spec.ts'

jasmine.getEnv().configure({random: false});
bootstrap();

function bootstrap () {
  if (window.jasmineRef) {
    location.reload();
    return;
  } else {
    window.onload();
    window.jasmineRef = jasmine.getEnv();
  }

  // First, initialize the Angular testing environment.
  getTestBed().initTestEnvironment(
    BrowserDynamicTestingModule,
    platformBrowserDynamicTesting()
  );
}

创建示例单元测试/src/app/app.component.spec.ts

describe('Testing tests', () => {
  it('should succeed', () => expect(true).toEqual(true));
  it('should fail', () => expect(true).toEqual(false));
});

该文件将被运行,因为它已在main-testing.ts要测试的东西注释中列出。如果要测试其他文件,请相应地更改链接。

最后,要运行测试,请编辑angular.json并更改行

            "main": "src/main.ts",

            "main": "src/main-testing.ts",

要再次运行该应用程序,请将其改回。