如何使用Angular 8设置PhantomJS? ---->切换到人偶

时间:2019-06-26 07:08:35

标签: angular phantomjs puppeteer angular8

我有一个带有单元测试的Angular 8应用程序,当我运行ng test时,将执行测试。我想将测试包括在TFS中以完成CI / CD管道的自动化,为此,我正在尝试使用PhantomJS。

我当前的设置是:

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

我的karma.conf.js文件:

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
  });
};

当我运行ng test --browsers = PhantomJS时,出现此错误:

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

有人可以帮我吗?

谢谢


更新:

我用Puppeteer取代了PhantomJS,现在一切正常。

这是我所做的:

  

npm install --save-dev puppeteer

将我的karma.conf.js文件更改为如下所示:

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   }); };

已添加到我package.json中的脚本部分:

  

“ test-puppeteerer”:“ ng test --watch = false --source-map = false”

注意-source-map = false -否则,它总是会失败

在TFS中,在自定义NPM任务中:

  

运行test-puppeteer

0 个答案:

没有答案