简体   繁体   English

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

[英]How to setup jest with angular in stackblitz

Is it possible to setup stackblitz project to run angular unit-testing with jest? 是否可以设置stackblitz项目以开玩笑地运行角度单元测试?

Update: 更新:

Got it working with codesandbox(it's already running on jest) but have yet not been able to make it run with stackblitz, taken in consideration they work differently. 考虑到它们的工作原理不同,它可以与codesandbox一起使用(它已经在开玩笑地运行了),但还不能使其与stackblitz一起运行。

Thanks in advance! 提前致谢!

I have done this before, but for Jasmine not Jest. 我以前做过,但茉莉花不是开玩笑。 You may want to follow and build it upon it: 您可能要遵循并在其上构建:

Brief description 简要描述;简介

Add dependencies: jasmine-core and @types/jasmine . 添加依赖项: jasmine-core@types/jasmine

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

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

Edit /src/styles.scss and add: 编辑/src/styles.scss并添加:

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

Add /src/main-testing.ts file (aside main.ts ) and paste the following content: 添加/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()
  );
}

Create a sample unit test /src/app/app.component.spec.ts : 创建一个样本单元测试/src/app/app.component.spec.ts

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

That file will be run because it was listed in stuff to test comment in main-testing.ts . 该文件将被运行,因为它已被列在main-testing.ts 用于测试注释的内容中。 If you want to test other file(s), change the links accordingly. 如果要测试其他文件,请相应地更改链接。

And finally, to run the tests, edit angular.json and change the line 最后,要运行测试,请编辑angular.json并更改行

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

to

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

To run again the application, change it back. 要再次运行该应用程序,请将其改回。

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

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