角6的Karma测试 [英] Karma Test With angular 6

查看:90
本文介绍了角6的Karma测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正准备使用npm进行角度测试

I am tring to run test of angular using npm

ng test

但是问题是chrome在测试完成后开始而不是停止,所以我使用了:

but the problem is that chrome start and not stop after test finish so I used :

ng test --watch=false

但是会导致错误"Chrome 69.0.3497(Linux 0.0.0)错误" chrome超时 我试图在连续部署服务器上运行它,所以此错误会导致在过程中失败,无论如何停止该想法

but that cause error "Chrome 69.0.3497 (Linux 0.0.0) ERROR" which make chrome timeout I am trying to run that on continuous deployment server so this error cause fail in the process any idea how to stop that

推荐答案

您可以在基于Unix的系统中使用无头铬

You can avoid this under unix based system using the Headless chrome

这是在无头环境中运行Chrome浏览器的一种方法.本质上,没有Chrome就能运行Chrome!它将Chromium和Blink渲染引擎提供的所有现代Web平台功能引入命令行.

It's a way to run the Chrome browser in a headless environment. Essentially, running Chrome without chrome! It brings all modern web platform features provided by Chromium and the Blink rendering engine to the command line.

第一:
将您的karma.conf.js配置为使用无头镀铬,例如添加customLaunchers:

first:
configure your karma.conf.js to use headless chrome e.g adding a customLaunchers:

...
browsers: ['Chrome'],
customLaunchers: {
    ChromeNoSandboxHeadless: {
       base: 'ChromeHeadless',
       flags: ['--no-sandbox']
    }
 }, 

秒: 告诉您的package.json脚本使用您的配置进行测试:

second: tell your package.json script to use your config for testing:

"scripts": {
   ...
    "test": "ng test --browsers=ChromeNoSandboxHeadless",
   ...
  },

然后运行npm run testyarn test,您的测试将在不打开浏览器的情况下运行.您可以在首选的浏览器(Firefox,铬,铬等)中打开显示的url.

then run npm run test or yarn test and your tests will works without open your browser. You can open the displayed url in your prefered browsers(firefox, chrome, chromium etc...).

第二种方式: 您可以使用 Karma-mocha-reporter 在控制台中显示所有测试用例.

second way: You can display all your test cases in your console using Karma-mocha-reporter

  • 首先:安装npm install karma-mocha-reporter --save-dev
  • 第二:要求您的记者在plugins下的karma.conf.js中,例如require('karma-mocha-reporter'),
  • First : install npm install karma-mocha-reporter --save-dev
  • Second: require your reporter inside karma.conf.js under plugins like require('karma-mocha-reporter'),

然后将新的报告器mocha添加到您的报告器数组:reporters: ['mocha', 'progress', 'kjhtml']

Then add the new reporter mocha to your reporters array: reporters: ['mocha', 'progress', 'kjhtml']

使用npm run testyarn test运行测试会将报告显示到控制台中.

Run your test using npm run test or yarn test will display the report into your console.

另一种方式:

看看选项singleRun(布尔默认设置为false).设置true,Karma将启动并捕获所有已配置的浏览器,运行测试,然后以退出代码0或1退出,具体取决于所有测试通过还是失败. Alternativ,使用标志npm run test --single-run运行它.

in your karma.config.json take a look at the option singleRun (a boolean default set to false). Set true, Karma will start and capture all configured browsers, run tests and then exit with an exit code of 0 or 1 depending on whether all tests passed or any tests failed. Alternativ, run it using the flag npm run test --single-run.

这篇关于角6的Karma测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆