gpt4 book ai didi

unit-testing - ng 测试 - 意外 token 'const'

转载 作者:太空狗 更新时间:2023-10-29 18:31:20 26 4
gpt4 key购买 nike

我使用 angular-cli 创建了一个应用程序。现在我正在尝试运行一些单元测试,但我想用 PhantomJS 运行它们而不是 Chrome。所以我将 package.json 更改为如下所示:

{
"name": "frontend",
"version": "0.0.0",
"license": "MIT",
"scripts": {
"ng": "ng",
"start": "ng serve",
"start-lite": "tsc && concurrently \"npm run tsc:w\" \"npm run lite\"",
"lite": "lite-server",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"tsc": "tsc",
"tsc:w": "tsc -w",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/common": "^2.4.0",
"@angular/compiler": "^2.4.0",
"@angular/core": "^2.4.0",
"@angular/forms": "^2.4.0",
"@angular/http": "^2.4.0",
"@angular/platform-browser": "^2.4.0",
"@angular/platform-browser-dynamic": "^2.4.0",
"@angular/router": "^3.4.0",
"angular2-wizard": "^0.3.0",
"bootstrap": "^3.3.7",
"core-js": "^2.4.1",
"karma-phantomjs-launcher": "^1.0.4",
"ng2-bootstrap": "^1.4.0",
"rxjs": "^5.1.0",
"video.js": "^5.17.0",
"zone.js": "^0.7.6"
},
"devDependencies": {
"@angular/cli": "1.0.0-rc.1",
"@angular/compiler-cli": "^2.4.0",
"@types/jasmine": "2.5.38",
"@types/node": "~6.0.60",
"codelyzer": "~2.0.0",
"concurrently": "^2.2.0",
"jasmine-core": "~2.5.2",
"jasmine-spec-reporter": "~3.2.0",
"karma": "~1.4.1",
"karma-chrome-launcher": "~2.0.0",
"karma-cli": "~1.0.1",
"karma-coverage-istanbul-reporter": "^0.2.0",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"lite-server": "^2.2.2",
"phantomjs": "^2.1.7",
"protractor": "~5.1.0",
"ts-node": "~2.0.0",
"tslint": "~4.4.2",
"typescript": "~2.0.0"
}
}

karma 配置

karma.conf.js ->

module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['jasmine', '@angular/cli'],
plugins: [
require('karma-jasmine'),
require('karma-phantomjs-launcher'),
require('karma-jasmine-html-reporter'),
require('karma-coverage-istanbul-reporter'),
require('@angular/cli/plugins/karma')
],
client:{
clearContext: false // leave Jasmine Spec Runner output visible in browser
},
files: [
{ pattern: './src/test.ts', watched: false }
],
preprocessors: {
'./src/test.ts': ['@angular/cli']
},
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'],
singleRun: false
});
};

错误

但现在的问题是,每次我运行测试时都会出现错误:

PhantomJS 2.1.1 (Windows 8 0.0.0) ERROR
SyntaxError: Unexpected token 'const'
at webpack:///src/app/app.component.ts:1:0 <- src/test.ts:63183

PhantomJS 2.1.1 (Windows 8 0.0.0) ERROR
SyntaxError: Unexpected token 'const'
at webpack:///src/app/app.component.ts:1:0 <- src/test.ts:63183

如果需要,我可以提供 src/test.ts 但它只是一个基本的单元测试。我查看了所有文件并将 const 替换为 var。遗憾的是,这并没有解决任何问题。


我调查过但无助于找到解决方案的问题:

最佳答案

原来在 src/tsconfig.spec.json 中将 es6 更改为 es5 解决了这个问题。

(以为我只有 1 个 tsconfig.json,之前从未使用过 angular/cli

关于unit-testing - ng 测试 - 意外 token 'const',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43319260/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com