在PhantomJs中进行测试时,如何修复'SyntaxError:使用保留字'import'\ nat test.ts:3:0'

时间:2019-04-23 12:39:28

标签: javascript phantomjs karma-jasmine

我目前正在建立Angular的ionic项目。我正在尝试使用Jenkins中的PhantomJS与Karma进行单元测试。当我使用命令

ng test 

它显示了我无法修复的以下错误。

 10% building 7/7 modules 0 active23 04 2019 14:07:39.703:INFO [karma-server]: Karma v3.1.4 server started at http://0.0.0.0:9876/
23 04 2019 14:07:39.709:INFO [launcher]: Launching browsers PhantomJS with concurrency unlimited
23 04 2019 14:07:39.716:INFO [launcher]: Starting browser PhantomJS                                                                                                              23 04 2019 14:07:45.328:INFO [PhantomJS 2.1.1 (Windows 8.0.0)]: Connected on socket eBN8aVTIrLGzxs-gAAAA with id 91154415
23 04 2019 14:07:45.347:WARN [middleware:karma]: Invalid file type, defaulting to js. ts
PhantomJS 2.1.1 (Windows 8.0.0) ERROR
  {
    "message": "SyntaxError: Use of reserved word 'import'\nat test.ts:3:0",
    "str": "SyntaxError: Use of reserved word 'import'\nat test.ts:3:0"
  }

PhantomJS 2.1.1 (Windows 8.0.0) ERROR
  {
    "message": "SyntaxError: Use of reserved word 'import'\nat test.ts:3:0",
    "str": "SyntaxError: Use of reserved word 'import'\nat test.ts:3: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'),  /*comment out this line to disable the karma-chrome-launcher*/
      require('karma-phantomjs-launcher'),  /* add this line to disable the karma-phantomjs-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
    },
    files: [
      { pattern: './test.ts', watched: false }
    ],                                            
    preprocessors: {

    },
    mime: {
      'text/x-typescript': ['ts', 'tsx']
    },
    coverageIstanbulReporter: {
      reports: ['html', 'lcovonly'],
      fixWebpackSourcePaths: true
    },
    angularCli: {
      environment: 'dev'
    },
    reporters: config.angularCli && config.angularCli.codeCoverage
      ? ['progress', 'coverage-istanbul']
      : ['progress', 'kjhtml'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
     browsers: ['PhantomJS'], /*remove chrome and replace it with PhantomJS */
    singleRun: true  /*make it true to run test suits only one time*/
  });
};

这是错误所指的test.js:

import 'zone.js/dist/zone-testing';
import { getTestBed } from '@angular/core/testing';
import {
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting
} from '@angular/platform-browser-dynamic/testing';

declare const require: any;

// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting()
);
// Then we find all the tests.
const context = require.context('./', true, /\.spec\.ts$/);
// And load the modules.
context.keys().map(context);

0 个答案:

没有答案