ng test - SyntaxError:意外的令牌' const'

时间:2018-02-27 14:33:46

标签: angular typescript phantomjs angular-cli karma-jasmine

非常感谢所有对此的帮助。

我正在使用angular / cli建立一个角度5项目。我已经构建了项目的很大一部分,并且没有遇到构建或服务命令的任何问题。

我正在尝试运行我的测试套件并收到以下错误:

27 02 2018 14:22:58.588:WARN [karma]: No captured browser, open http://localhost:9876/
27 02 2018 14:22:58.601:INFO [karma]: Karma v1.7.1 server started at http://0.0.0.0:9876/
27 02 2018 14:22:58.601:INFO [launcher]: Launching browser PhantomJS with unlimited concurrency
27 02 2018 14:22:58.608:INFO [launcher]: Starting browser PhantomJS
 10% building modules 3/3 modules 0 active(node:82890) DeprecationWarning: loaderUtils.parseQuery() received a non-string value which can be problematic, see https://github.com/webpack/loader-utils/issues/56
parseQuery() will be replaced with getOptions() in the next major version of loader-utils.
27 02 2018 14:23:06.801:WARN [karma]: No captured browser, open http://localhost:9876/  
27 02 2018 14:23:06.988:INFO [PhantomJS 2.1.1 (Mac OS X 0.0.0)]: Connected on socket BcW5WVSiPfS1DRQdAAAA with id 61500965
PhantomJS 2.1.1 (Mac OS X 0.0.0) ERROR
  SyntaxError: Unexpected token 'const'
  at http://localhost:9876/_karma_webpack_/vendor.bundle.js:28
PhantomJS 2.1.1 (Mac OS X 0.0.0) ERROR
  SyntaxError: Unexpected token 'const'
  at http://localhost:9876/_karma_webpack_/vendor.bundle.js:28

PhantomJS 2.1.1 (Mac OS X 0.0.0) ERROR
  SyntaxError: Unexpected token 'const'
  at http://localhost:9876/_karma_webpack_/vendor.bundle.js:28
PhantomJS 2.1.1 (Mac OS X 0.0.0) ERROR
  SyntaxError: Unexpected token 'const'
  at http://localhost:9876/_karma_webpack_/vendor.bundle.js:28

我已经检查了我的tsconfig文件,并且都将目标设置为" es5"。

我正在使用PhantomJS进行浏览器测试,并尝试使用cobertura收集收集报道。

请参阅以下文件以了解我的设置。

提前致谢。

tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "noEmitHelpers": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ]
  }
}

的src / tsconfig.spec.json

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/spec",
    "baseUrl": "./",
    "module": "commonjs",
    "target": "es5",
    "types": [
      "jasmine",
      "node"
    ]
  },
  "files": [
    "test.ts"
  ],
  "include": [
    "**/*.spec.ts",
    "**/*.d.ts"
  ]
}

karma.conf.js

// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/configuration-file.html

module.exports = function(config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine', '@angular/cli'],
    plugins: [
      require('karma-jasmine'),
      require('karma-phantomjs-launcher'),
      require('karma-junit-reporter'),
      require('karma-jasmine-html-reporter'),
      require('karma-coverage-istanbul-reporter'),
      require('@angular/cli/plugins/karma')
    ],
    client: {
      clearContext: true // leave Jasmine Spec Runner output visible in browser
    },
    coverageReporter: {
      type: 'cobertura'
    },
    coverageIstanbulReporter: {
      reports: ['html', 'cobertura'],
      fixWebpackSourcePaths: true
    },
    remapIstanbulReporter: {
      reports: {
        cobertura: './coverage/cobertura.xml'
      }
    },
    remapCoverageReporter: {
      cobertura: './coverage/cobertura.xml'
    },
    angularCli: {
      environment: 'dev'
    },
    junitReporter: {
      outputDir: 'phantomjs',
      outputFile: 'test.xml'
    },
    reporters: ['progress', 'kjhtml', 'junit'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['PhantomJS'],
    singleRun: false
  });
};

.angular-cli.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "project": {
    "name": "my.project"
  },
  "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": [
        "assets",
        "favicon.ico",
        "web.config"
      ],
      "index": "index.html",
      "main": "main.ts",
      "polyfills": "polyfills.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.app.json",
      "testTsconfig": "tsconfig.spec.json",
      "prefix": "app",
      "styles": [
        "styles.scss"
      ],
      "scripts": [],
      "environmentSource": "environments/environment.ts",
      "environments": {
        "dev": "environments/environment.ts",
        "develop": "environments/environment.develop.ts",
        "test": "environments/environment.test.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ],
  "lint": [
    {
      "project": "src/tsconfig.app.json",
      "exclude": "**/node_modules/**"
    },
    {
      "project": "src/tsconfig.spec.json",
      "exclude": "**/node_modules/**"
    },
    {
      "project": "e2e/tsconfig.e2e.json",
      "exclude": "**/node_modules/**"
    }
  ],
  "test": {
    "karma": {
      "config": "./karma.conf.js"
    }
  },
  "defaults": {
    "styleExt": "scss",
    "component": {}
  }
}

1 个答案:

答案 0 :(得分:0)

axl-code中提出建议后,将其切换为使用无头chrome浏览器可以解决我遇到的问题,希望对您有所帮助!