首页 > 解决方案 > 如何使用 Angular 8 设置 PhantomJS?----> 切换到 Puppeteer

问题描述

我有一个带有单元测试的 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-puppeteer”:“ng 测试 --watch=false --source-map=false”

注意--source-map=false - 没有这个它总是失败

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

运行测试木偶

标签: angularphantomjspuppeteerangular8

解决方案


推荐阅读