Karma Test With angular 6

社会主义新天地 提交于 2019-12-23 16:43:41

问题


I am tring to run test of angular using npm

ng test

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

ng test --watch=false

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


回答1:


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

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.

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

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

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

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

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...).

second way: You can display all your test cases in your console using 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'),

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

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

another way:

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.




回答2:


In your karma.conf.js, change singleRun to true.

singleRun: true


来源:https://stackoverflow.com/questions/52700343/karma-test-with-angular-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!