javascript - 未捕获的语法错误 : Use of const in strict mode | Travis-ci karma test with chromium throws

标签 javascript typescript webpack karma-jasmine travis-ci

我正在尝试在 travis-ci 构建中运行我的 karma 测试。我想出了如何让 chromium 运行起来,但现在它抛出了一个错误。但是,我在本地没有遇到此错误,因此我认为它一定与 travis-ci 环境有关。

26 08 2016 17:43:47.109:INFO [karma]: Karma v1.2.0 server started at http://localhost:9876/
26 08 2016 17:43:47.112:INFO [launcher]: Launching browser Chrome_travis_ci with unlimited concurrency
26 08 2016 17:43:47.279:INFO [launcher]: Starting browser Chromium
26 08 2016 17:43:49.456:INFO [Chromium 37.0.2062 (Ubuntu 0.0.0)]: Connected on socket /#NbeFghd0qtvN2-vKAAAA with id 90741507
Chromium 37.0.2062 (Ubuntu 0.0.0) ERROR
  Uncaught SyntaxError: Use of const in strict mode.
  at webpack:///src/app/app.spec.ts:1:0 <- spec-bundle.js:46116

spec-bundle.js

/**
 * @author: @AngularClass
 */

Error.stackTraceLimit = Infinity;

require('core-js/es6');
require('core-js/es7/reflect');

// Typescript emit helpers polyfill
require('ts-helpers');

require('zone.js/dist/zone');
require('zone.js/dist/long-stack-trace-zone');
require('zone.js/dist/async-test');
require('zone.js/dist/fake-async-test');
require('zone.js/dist/sync-test');
require('zone.js/dist/proxy');
require('zone.js/dist/jasmine-patch');

// RxJS
require('rxjs/Rx');

var testing = require('@angular/core/testing');
var browser = require('@angular/platform-browser-dynamic/testing');

testing.setBaseTestProviders(
  browser.TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
  browser.TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS
);

/*
 * Ok, this is kinda crazy. We can use the the context method on
 * require that webpack created in order to tell webpack
 * what files we actually want to require or import.
 * Below, context will be an function/object with file names as keys.
 * using that regex we are saying look in ./src/app and ./test then find
 * any file that ends with spec.js and get its path. By passing in true
 * we say do this recursively
 */
var testContext = require.context('../src', true, /\.spec\.ts/);

/*
 * get all the files, for each file, call the context function
 * that will require the file and load it up here. Context will
 * loop and require those spec files here
 */
function requireAll(requireContext) {
  return requireContext.keys().map(requireContext);
}

// requires and returns all modules that match
var modules = requireAll(testContext);

app.spec.ts

import { addProviders, inject } from '@angular/core/testing';

// Load the implementations that should be tested
import { AppComponent } from './app.component';

describe('AppComponent', () => {
  // provide our implementations or mocks to the dependency injector
  beforeEach(() => addProviders([AppComponent]));

  it('should have a name', inject([AppComponent], (app) => {
    expect(app.name).toEqual('spring-boot-angular2');
  }));

});

karma.conf.js

/**
 * @author: @AngularClass
 */
var path = require('path');

module.exports = function(config) {
  var testWebpackConfig = require('./webpack.test.js');

  var configuration = {

    // base path that will be used to resolve all patterns (e.g. files, exclude)
    basePath: '',

    /*
     * Frameworks to use
     *
     * available frameworks: https://npmjs.org/browse/keyword/karma-adapter
     */
    frameworks: ['jasmine'],

    // list of files to exclude
    exclude: [ ],

    /*
     * list of files / patterns to load in the browser
     *
     * we are building the test environment in ./spec-bundle.js
     */
    files: [ { pattern: './spec-bundle.js', watched: false } ],

    /*
     * preprocess matching files before serving them to the browser
     * available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
     */
    preprocessors: { './spec-bundle.js': ['coverage', 'webpack', 'sourcemap'] },

    // Webpack Config at ./webpack.test.js
    webpack: testWebpackConfig,

    coverageReporter: {
      dir : '../coverage/',
      reporters: [
        { type: 'text-summary' },
        { type: 'json' },
        { type: 'html' }
      ]
    },

    // Webpack please don't spam the console when running in karma!
    webpackServer: { noInfo: true },

    /*
     * test results reporter to use
     *
     * possible values: 'dots', 'progress'
     * available reporters: https://npmjs.org/browse/keyword/karma-reporter
     */
    reporters: [ 'mocha', 'coverage' ],

    // web server port
    port: 9876,

    // enable / disable colors in the output (reporters and logs)
    colors: true,

    /*
     * level of logging
     * possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
     */
    logLevel: config.LOG_INFO,

    // do not fail on empty test suite
    //failOnEmptyTestSuite: false,

    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: false,

    /*
     * start these browsers
     * available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
     */
    browsers: [
      'Chromium'
    ],

    customLaunchers: {
      Chrome_travis_ci: {
        base: 'Chromium',
        chromeDataDir: path.resolve(__dirname, '.chrome'),
        flags: ['--no-sandbox']
      }
    },

    /*
     * Continuous Integration mode
     * if true, Karma captures browsers, runs the tests and exits
     */
    singleRun: true
  };

  if(process.env.TRAVIS){
    configuration.browsers = ['Chrome_travis_ci'];
  }

  config.set(configuration);
};

最佳答案

添加 dist: trusty 解决了我的问题。找到我的答案 here

完整的.travis.yml

sudo: required
dist: trusty

language: java

before_install:
  - sudo apt-get update
  - sudo apt-get install -y libappindicator1 fonts-liberation
  - wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
  - sudo dpkg -i google-chrome*.deb
  - export CHROME_BIN=/usr/bin/google-chrome
  - export DISPLAY=:99.0
  - sh -e /etc/init.d/xvfb start

jdk:
  - oraclejdk8

before_cache:
  - rm -f $HOME/.gradle/caches/modules-2/modules-2.lock
cache:
  directories:
    - $HOME/.gradle/caches/
    - $HOME/.gradle/wrapper/

关于javascript - 未捕获的语法错误 : Use of const in strict mode | Travis-ci karma test with chromium throws,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39172222/

相关文章:

php - 将 javascript 与 Twitter API 结合使用

javascript - 如何快速修复和构建 NPM 包中的错误,以便构建服务器可以使用它?

TypeScript+Webpack 解析符号链接(symbolic link)文件夹中的依赖关系

node.js - npm start 不起作用。我已经尝试了一切。我很困惑

javascript - 如何优化在 canvas JS 上绘制大量元素?

javascript - JavaScript 和 JQuery 新手 : Why will my AJAX anchors work only once?

javascript - Chrome 扩展仅在刷新时运行,而不是在页面导航时运行

typescript - JSX 元素类型 'HTMLImageElement' 不是 JSX 元素的构造函数

javascript - 方法返回 id 但不保存其他数据点

webpack - "SyntaxError: Cannot use import statement outside a module"与 Babel、Jest 和 webpack