如何在Angular中测试传递后运行ng测试然后进行构建

mat*_*ace 2 karma-jasmine angular-cli angular

我有一个Angular 4应用程序,我正在为它建立一个构建管道.我想要做的是运行管道ng test,然后一旦测试通过然后运行ng build

有没有人有我如何做到这一点的例子?

Mau*_*ana 8

在package.json中,您可以创建自定义脚本.

"scripts": {
"build": "ng build",
"test": "ng test --single-run",
"ci": "npm run test && npm run build"
},
Run Code Online (Sandbox Code Playgroud)

然后跑

npm run ci
Run Code Online (Sandbox Code Playgroud)

  • 也许`--no-watch`? (2认同)

Voj*_*cka 7

要添加其他答案:从Angular 6开始--single-run,它不再起作用,但是您应该改用以下方法:

--watch=false
Run Code Online (Sandbox Code Playgroud)

在这里描述:

通过Karma执行构建后,将执行测试,并且它将自动监视文件中的更改。您可以通过--watch = false一次运行测试。

因此,现在您需要执行以下操作:

ng test --watch=false && ng build
Run Code Online (Sandbox Code Playgroud)


小智 5

ng test --single-run && ng build

解释:

  • 首先确保将 ng test 作为单一运行运行,否则它永远不会终止:ng test --single-run
  • 然后使用双 & 符号&&指示下一个命令仅应在成功退出状态下运行。
  • 然后ng build

这应该适用于 Windows 和 Linux 系统...