【问题标题】:Uncaught SyntaxError: Use of const in strict mode | Travis-ci karma test with chromium throws未捕获的 SyntaxError:在严格模式下使用 const | Travis-ci 业力测试与铬投掷
【发布时间】:2016-08-26 18:01:57
【问题描述】:

我正在尝试在 travis-ci 构建中运行我的业力测试。我想出了如何让铬运转,但现在它抛出了一个错误。但是,我在本地没有遇到此错误,因此我认为它一定与 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);
};

【问题讨论】:

    标签: javascript typescript webpack karma-jasmine travis-ci


    【解决方案1】:

    添加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/
    

    【讨论】:

      猜你喜欢
      • 2016-08-15
      • 2014-05-01
      • 2019-02-04
      • 2018-03-27
      • 2017-10-11
      • 1970-01-01
      • 1970-01-01
      • 2013-05-28
      相关资源
      最近更新 更多