2

在运行我的测试时,我似乎无法修复这个错误。

09 04 2017 22:08:20.577:INFO [karma]: Karma v1.6.0 server started at http://0.0.0.0:9876/
09 04 2017 22:08:20.580:INFO [launcher]: Launching browser PhantomJS with unlimited concurrency
09 04 2017 22:08:20.602:INFO [launcher]: Starting browser PhantomJS
09 04 2017 22:08:23.661:INFO [PhantomJS 2.1.1 (Windows 8 0.0.0)]: Connected on socket SPM1D8Mj1w6ABbGuAAAA with id 7910462
PhantomJS 2.1.1 (Windows 8 0.0.0) ERROR
  SyntaxError: Use of reserved word 'let' in strict mode
  at test/browser/index.js:886

我相信这在某些软件包更新期间开始发生。

我的 Karma 配置如下所示:

require('babel-register');

const webpack = require('../webpack.config.babel.js');

module.exports = function(config) {
  config.set({
    basePath: '../',
    frameworks: ['mocha', 'chai-sinon'],
    browsers: ['PhantomJS'],
    files: ['test/browser/**/*.js'],
    preprocessors: {
      'test/**/*.js': ['webpack'],
      'src/**/*.js': ['webpack']
    },
    webpack,
    webpackMiddleware: { noInfo: true }
  });
};

测试非常简单

import { h, render, rerender } from 'preact';
import { route } from 'preact-router';
import App from '../../src/components/app';

/*global sinon,expect*/
describe('App', () => {
  var scratch;

  before(() => {
    scratch = document.createElement('div');

    (document.body || document.documentElement).appendChild(scratch);
  });

  beforeEach(() => {
    scratch.innerHTML = '';
  });

  after(() => {
    scratch.parentNode.removeChild(scratch);
  });

  describe('routing', () => {
    it('should render the homepage', () => {
      render(<App />, scratch);
      expect(scratch.innerHTML).not.contain('Home');
    });

    it('should render the header', () => {
      render(<App />, scratch);
      expect(scratch.innerHTML).to.contain('header');
    });

    it('should render the footer', () => {
      render(<App />, scratch);

      expect(scratch.innerHTML).to.contain('footer');
    });

    it('should render the social media links', () => {
      render(<App />, scratch);

      expect(scratch.innerHTML).to.contain('a');
    });
  });
});

知道我什至可以找出它在说哪个“让”吗?

我在 Node v7.8.0 上运行它

4

4 回答 4

1

问题是特定于浏览器的。因此,如果您不使用 Babel 将源代码编译为 ES5,那么测试根本不会在 PhantomJS 中运行。Webpack 即时处理编译。由于我看不到您的 Webpack 配置文件的来源,我猜测问题是“webpack.config.babel.js”中的属性值。

于 2017-04-19T19:31:48.870 回答
0

我刚刚遇到了类似的问题。根据@STEEL 的回答,通过安装npm install karma-chrome-launcher浏览器并将其更改为 ChromeHeadless,我能够运行测试。但是,我发现 Chrome 工作而 PhantomJS 没有工作的原因是因为我的 babel.config.js 文件中有 Chrome 而不是 PhantomJS,所以 Karma 没有正确地转换为 PhantomJS。一旦我将 PhantomJS 添加到我的 babel 预设中的目标列表中,它就会正确编译。这是我的babel.config.js文件的样子:

const presets = [
  ["@babel/env", {
    targets: {
      edge: "17",
      firefox: "60",
      chrome: "67",
      safari: "11.1",
      phantomjs: "2.1.1"
    },
    useBuiltIns: "usage"
  }]
];

module.exports = { presets };

我的配置没有从 webpack 配置文件加载设置。相反,它使用以下字段在 karma.config.js 中设置 webpack 的配置:

webpack: {
  module: {
    rules: [
      { test: /\.js/, exclude: /node_modules/, loader: 'babel-loader' }
    ]
  },
  watch: true,
  mode: 'none'
},
于 2018-10-09T01:52:07.247 回答
0

This Might solve your issue. You need to add babel-polyfill and phantomjs-polyfill on your karma file as-

module.exports = function(config) {
  config.set({
    ....
    files: [
      './node_modules/babel-polyfill/dist/polyfill.js',
       './node_modules/phantomjs-polyfill/bind-polyfill.js',
       'test/browser/**/*.js'
    ],
    ....
  });
};
于 2017-04-18T12:36:29.723 回答
0

在运行 Karma 测试时,我也遇到了与 Ubuntu Linux 相同的问题。问题在于业力配置中的浏览器字段。

我有Chrome,后来我试过了PhantomJS。但最终发现在 Linux 上使用“Chromeheadless”可以解决问题。由于仅运行控制台视图的机器只能运行无头应用程序。

使用 Chromeheadless

browsers: ['ChromeHeadless'],

此外,如果您仍然遇到问题,请尝试flags: ['--no-sandbox'],在您的karma.conf.js

于 2018-05-22T10:17:14.717 回答