简体   繁体   English

"Stackblitz:如何使用 Angular 执行测试?"

[英]Stackblitz : how can i execute a test using Angular?

How can I execute a test scripts with Stackblitz using an Angular project?如何使用 Angular 项目使用 Stackblitz 执行测试脚本? I see into the package.json a karma packages, so I am wondering if there is the possibily to test my components我看到了 package.json 一个业力包,所以我想知道是否有可能测试我的组件

https:\/\/stackblitz.com\/edit\/redux-in-actions?file=package.json<\/a> https:\/\/stackblitz.com\/edit\/redux-in-actions?file=package.json<\/a>

thanks Andrea谢谢安德里亚

"

One approach is using Jasmine (which is a behavior-driven development framework) to run Angular tests in Stackblitz.一种方法是使用Jasmine (一种行为驱动的开发框架)在 Stackblitz 中运行 Angular 测试。

Stackblitz Demo Stackblitz 演示

Brief description as step by step guide作为分步指南的简要说明

Main steps:主要步骤:

  1. Install Jasmine安装茉莉花
  • add Jasmine as dependency to stackblitz将 Jasmine 作为依赖添加到 stackblitz
  • add file /src/global-jasmine.ts添加文件/src/global-jasmine.ts
     import jasmineRequire from 'jasmine-core/lib/jasmine-core/jasmine.js'; window.jasmineRequire = jasmineRequire;
  • Edit /src/styles.scss编辑/src/styles.scss
     @import 'jasmine-core/lib/jasmine-core/jasmine.css';
  • Add /src/main-testing.ts file添加/src/main-testing.ts file
     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() ); }
  1. Add tests /src/app/app.component.spec.ts eg:添加测试/src/app/app.component.spec.ts例如:

     describe('Testing tests', () => { it('should succeed', () => expect(true).toEqual(true)); it('should fail', () => expect(true).toEqual(false)); });
  2. Run the tests运行测试

    Use "main": "src/main-testing.ts", instead of "main": "src/main.ts", in file angular.json在文件angular.json使用"main": "src/main-testing.ts",而不是"main": "src/main.ts",

The latest version of Jasmine does not work on Stackblitz.最新版本的 Jasmine 不适用于 Stackblitz。 If you are getting an error asking to install jasmine-core, even when installed, try downgrade to version 3.5.0.如果您在要求安装 jasmine-core 时遇到错误,即使已安装,请尝试降级到 3.5.0 版。 This worked for me, you can do this by adding the dependency on the bottom left: jasmine@3.5.0这对我有用,你可以通过在左下角添加依赖来做到这一点:jasmine@3.5.0

"

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

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