3

I have an Angular 8 application with unit tests and when I run ng test the tests are executed. I want to include the tests in TFS to complete automate my CI/CD pipeline and for that I'm trying to use PhantomJS.

My Current setup is:

Angular CLI: 8.0.4
Node: 12.4.0
OS: win32 x64
Angular: 8.0.2
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.800.4
@angular-devkit/build-angular     0.800.4
@angular-devkit/build-optimizer   0.800.4
@angular-devkit/build-webpack     0.800.4
@angular-devkit/core              8.0.4
@angular-devkit/schematics        8.0.4
@angular/cdk                      8.0.1
@angular/cli                      8.0.4
@angular/material                 8.0.1
@ngtools/webpack                  8.0.4
@schematics/angular               8.0.4
@schematics/update                0.800.4
rxjs                              6.5.2
typescript                        3.4.5
webpack                           4.30.0

My karma.conf.js file:

module.exports = function (config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine', '@angular-devkit/build-angular'],
    plugins: [
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      require('karma-phantomjs-launcher'),
      require('karma-jasmine-html-reporter'),
      require('karma-coverage-istanbul-reporter'),
      require('karma-junit-reporter'),
      require('@angular-devkit/build-angular/plugins/karma')
    ],
    client: {
      clearContext: false // leave Jasmine Spec Runner output visible in browser
    },
    coverageIstanbulReporter: {
      dir: require('path').join(__dirname, '../coverage/FeedbackApp'),
      reports: ['html', 'lcovonly', 'text-summary'],
      fixWebpackSourcePaths: true
    },
    reporters: ['progress', 'kjhtml', 'junit'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    captureTimeout: 60000,
    singleRun: true
  });
};

When I run ng test --browsers=PhantomJS I get this errors:

10% building 2/2 modules 0 active26 06 2019 08:55:52.878:INFO [karma-server]: Karma v4.1.0 server started at http://0.0.0.0:9876/
26 06 2019 08:55:52.881:INFO [launcher]: Launching browsers PhantomJS with concurrency unlimited
26 06 2019 08:55:52.892:INFO [launcher]: Starting browser PhantomJS                     26 06 2019 08:55:59.266:INFO [PhantomJS 2.1.1 (Windows 8.0.0)]: Connected on socket HEg48BpEY_ePpotFAAAA with id 68924062
PhantomJS 2.1.1 (Windows 8.0.0) ERROR
  SyntaxError: Use of reserved word 'class'
  at http://localhost:9876/_karma_webpack_/polyfills.js:3008:0

PhantomJS 2.1.1 (Windows 8.0.0) ERROR
  SyntaxError: Use of reserved word 'class'
  at http://localhost:9876/_karma_webpack_/polyfills.js:3008:0

PhantomJS 2.1.1 (Windows 8.0.0) ERROR
  SyntaxError: Use of reserved word 'class'
  at http://localhost:9876/_karma_webpack_/vendor.js:88:0

PhantomJS 2.1.1 (Windows 8.0.0) ERROR
  SyntaxError: Use of reserved word 'class'
  at http://localhost:9876/_karma_webpack_/vendor.js:88:0

PhantomJS 2.1.1 (Windows 8.0.0) ERROR
  SyntaxError: Unexpected token ')'
  at http://localhost:9876/_karma_webpack_/main.js:362:0

PhantomJS 2.1.1 (Windows 8.0.0) ERROR
  SyntaxError: Unexpected token ')'
  at http://localhost:9876/_karma_webpack_/main.js:362:0

Can anyone help me with this ?

Thanks


UPDATE:

I replaced PhantomJS by Puppeteer, and everything is working now.

Here is what I have done:

npm install --save-dev puppeteer

Changed my karma.conf.js file to look like this:

const process = require('process'); process.env.CHROME_BIN = 
require('puppeteer').executablePath();

module.exports = function (config) {   config.set({
    basePath: '',
    frameworks: ['jasmine', '@angular-devkit/build-angular'],
    plugins: [
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      require('karma-jasmine-html-reporter'),
      require('karma-coverage-istanbul-reporter'),
      require('@angular-devkit/build-angular/plugins/karma')
    ],
    client: {
      clearContext: false // leave Jasmine Spec Runner output visible in browser
    },
    coverageIstanbulReporter: {
      dir: require('path').join(__dirname, '../coverage/FeedbackApp'),
      reports: ['html', 'lcovonly'],
      fixWebpackSourcePaths: true
    },
    reporters: ['progress', 'kjhtml'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['ChromeHeadlessNoSandbox'],
    customLaunchers: {
      ChromeHeadlessNoSandbox: {
        base: 'ChromeHeadless',
        flags: ['--no-sandbox','--headless','--disable-gpu','--disable-translate','--disable-extensions']
      }
    },
    singleRun: true   }); };

Added to scripts section in my package.json:

"test-puppeteer": "ng test --watch=false --source-map=false"

Attention to the --source-map=false - without this it always fails

In TFS, in a custom NPM task:

run test-puppeteer

José Matos
  • 438
  • 4
  • 12
  • Related issue on [GitHub](https://github.com/angular/angular-cli/issues/14691#issuecomment-503492253). Someone posted a workaround there. – Charmin Jul 03 '19 at 06:43
  • If you only wan to not open the browser during the tests, use chromHeadless option `ng test --browsers ChromeHeadless`. https://stackoverflow.com/questions/46405196/how-can-i-run-tests-with-a-headless-browser – Bruno João Jul 24 '19 at 12:13

0 Answers0