简体   繁体   English

一起运行 ng test 和 ng serve

[英]Running ng test and ng serve together

I am a relative newbie in Angular 5, and am trying to setup a development setup for my team to build an Angular 5 application.我是 Angular 5 的相对新手,正在尝试为我的团队设置开发设置以构建 Angular 5 应用程序。

What I would like is for the team to be able to run linting & unit tests every time a change is made, and then serve up the changes so that the developer can immediately see the changes she is making.我希望团队能够在每次更改时运行 linting 和单元测试,然后提供更改,以便开发人员可以立即看到她所做的更改。

I have tried the following:我尝试了以下方法:

  • Running ng lint will lint the project运行ng lint将对项目进行 lint
  • Running ng test gets the Karma test runner started and any changes made to the code immediately kicks off the tests and provides feedback to the developer if any changes that were just introduced resulted in a broken test.运行ng test会启动 Karma 测试运行器,对代码所做的任何更改都会立即启动测试,并在刚刚引入的任何更改导致测试中断时向开发人员提供反馈。
  • Running ng serve will internally start the webpack-dev-server and build the project and serve it, so that the added/modified functionality will immediately become visible for the developer to try and validate on the browser.运行ng serve将在内部启动 webpack-dev-server 并构建项目并为其提供服务,以便开发人员可以立即看到添加/修改的功能,以便在浏览器上尝试和验证。

What I want to achieve is for the developers to get constant feedback on all three of the above aspects --- linting errors, broken tests, and serve the functionality --- as they continue to work on developing the project.我想要实现的是让开发人员在继续开发项目的过程中获得有关上述所有三个方面的持续反馈 --- linting 错误、损坏的测试和服务功能 ---。

Is there a way to get all three of these running together and visible to the developers with each change made to the code?有没有办法让所有这三个一起运行,并且在对代码进行每次更改时对开发人员可见?

You can run all three commands simultaneously using the & operator (don't confuse with && ).您可以使用&运算符同时运行所有三个命令(不要与&&混淆)。 The operator will spawn the process but share the output in the console with the next process.操作员将生成进程,但与下一个进程共享控制台中的输出。

For example例如

ng test & ng server & ng lint

All three will run at once.这三个将同时运行。 You could put the above into a bash script to make it easier.您可以将上述内容放入 bash 脚本中以使其更容易。

I did try adding this to package.json as a runnable script, but it didn't work.我确实尝试将其作为可运行脚本添加到package.json中,但没有奏效。

The thing is you cannot run ng lint and ng test all the time, compared to ng serve which will run and will keep the live-reload awake until you terminate it.问题是你不能一直运行ng lintng test ,与ng serve相比,它会运行并保持实时重新加载直到你终止它。

This is why in general, people will integrate the unit tests in a Continuous Integration cycle, with a Jenkins Job / Pipeline in general and linked to their Repository with a WebHook or something like that.这就是为什么一般来说,人们会在持续集成周期中集成单元测试,通常使用 Jenkins 作业/管道,并使用 WebHook 或类似的东西链接到他们的存储库。

In details, it would be something like after every commit / merge request / etc..., the Jenkins job would run the ng lint and ng test commands and output a result which would allow the commit / push / merge request if the tests pass.详细地说,就像在每次commit / merge request /等之后……,Jenkins 作业将运行ng lintng test命令并输出一个结果,如果测试通过,将允许commit / push / merge request .

That way, the people developing would not need to run manually all the time the ng test and ng lint commands, since whatever they do, if the Job running them doesn't pass they are stuck.这样,开发人员就不需要一直手动运行ng testng lint命令,因为无论他们做什么,如果运行它们的作业没有通过,他们就会被卡住。

What you can do, is either enforce a Continuous Integration process for your repository, or enforce your team to check and run the commands when their work is done.您可以做的是,要么为您的存储库实施持续集成流程,要么强制您的团队在他们的工作完成后检查并运行命令。

Obviously, the first choice is better.显然,第一个选择更好。

It is possible to do this using Angular builders.可以使用 Angular 构建器来做到这一点。 By creating your own builder, you can easily invoke existing Angular CLI builders.通过创建自己的构建器,您可以轻松调用现有的 Angular CLI 构建器。 For example:例如:

import { merge } from 'rxjs';
import { executeKarmaBuilder, executeDevServerBuilder } from '@angular-devkit/build-angular';

export function runTestAndServe(context: BuilderContext): Observable<BuilderOutput> {
  return merge(
    executeKarmaBuilder(KARMA_OPTIONS, context),
    executeDevServerBuilder(DEV_SERVER_OPTIONS, context)
  );
}

Make sure that your KARMA_OPTIONS are using --watch .确保您的KARMA_OPTIONS正在使用--watch

@Reactgular If you want to add it as a script to the package.json, you should add it like this: @Reactgular 如果你想把它作为脚本添加到 package.json 中,你应该像这样添加它:

"scripts": {
    "ng": "ng",
    "start": "ng lint; ng serve --host=0.0.0.0 --poll 500 --disableHostCheck=true --source-map=true",
    "test": "ng test --watch=true --source-map=true --code-coverage --browsers Chrome",

} }

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

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