我正在尝试使用angular cli生成覆盖率报告。但是我一直在跟踪错误。我不太确定这是我的Karma.conf的cli错误还是问题? ng test
/ npm test
运行正常...那么与代码覆盖率选项有关吗?
Error: Module build failed: SyntaxError: 'import' and 'export' may appear only with 'sourceType: module' (3:0)
at Parser.pp$5.raise (C:\Development\source\\node_modules\babylon\lib\index.js:4443:13)
at Parser.pp$1.parseStatement (C:\Development\source\\node_modules\babylon\lib\index.js:1875:16)
at Parser.parseStatement (C:\Development\source\\node_modules\babylon\lib\index.js:5795:22)
at Parser.pp$1.parseBlockBody (C:\Development\source\\node_modules\babylon\lib\index.js:2262:21)
at Parser.pp$1.parseTopLevel (C:\Development\source\\node_modules\babylon\lib\index.js:1772:8)
at Parser.parse (C:\Development\source\\node_modules\babylon\lib\index.js:1667:17)
at Object.parse (C:\Development\source\\node_modules\babylon\lib\index.js:7163:37)
at Instrumenter.instrumentSync (C:\Development\source\\node_modules\istanbul-lib-instrument\dist\instrumenter.js:121:31)
at Instrumenter.instrument (C:\Development\source\\node_modules\istanbul-lib-instrument\dist\instrumenter.js:176:32)
at Object.module.exports (C:\Development\source\\node_modules\istanbul-instrumenter-loader\index.js:25:25),Module build failed: SyntaxError: 'import
' and 'export' may appear only with 'sourceType: module' (2:0)
at Parser.pp$5.raise (C:\Development\source\\node_modules\babylon\lib\index.js:4443:13)
at Parser.pp$1.parseStatement (C:\Development\source\\node_modules\babylon\lib\index.js:1875:16)
at Parser.parseStatement (C:\Development\source\\node_modules\babylon\lib\index.js:5795:22)
at Parser.pp$1.parseBlockBody (C:\Development\source\\node_modules\babylon\lib\index.js:2262:21)
at Parser.pp$1.parseTopLevel (C:\Development\source\\node_modules\babylon\lib\index.js:1772:8)
at Parser.parse (C:\Development\source\\node_modules\babylon\lib\index.js:1667:17)
at Object.parse (C:\Development\source\\node_modules\babylon\lib\index.js:7163:37)
at Instrumenter.instrumentSync (C:\Development\source\\node_modules\istanbul-lib-instrument\dist\instrumenter.js:121:31)
at Instrumenter.instrument (C:\Development\source\\node_modules\istanbul-lib-instrument\dist\instrumenter.js:176:32)
at Object.module.exports (C:\Development\source\\node_modules\istanbul-instrumenter-loader\index.js:25:25)
at Compiler.compiler.plugin (C:\Development\source\\node_modules\@angular\cli\plugins\karma-webpack-throw-error.js:10:23)
at Compiler.applyPlugins (C:\Development\source\\node_modules\tapable\lib\Tapable.js:61:14)
at Watching._done (C:\Development\source\\node_modules\webpack\lib\Compiler.js:91:17)
at onCompiled (C:\Development\source\\node_modules\webpack\lib\Compiler.js:50:17)
at C:\Development\source\\node_modules\webpack\lib\Compiler.js:480:13
at next (C:\Development\source\\node_modules\tapable\lib\Tapable.js:138:11)
at Compiler.<anonymous> (C:\Development\source\\node_modules\webpack\lib\CachePlugin.js:62:5)
at Compiler.applyPluginsAsyncSeries (C:\Development\source\\node_modules\tapable\lib\Tapable.js:142:13)
at C:\Development\source\\node_modules\webpack\lib\Compiler.js:477:10
at Compilation.applyPluginsAsyncSeries (C:\Development\source\\node_modules\tapable\lib\Tapable.js:131:46)
at self.applyPluginsAsync.err (C:\Development\source\\node_modules\webpack\lib\Compilation.js:640:19)
at next (C:\Development\source\\node_modules\tapable\lib\Tapable.js:138:11)
at Compilation.<anonymous> (C:\Development\source\\node_modules\webpack\lib\ProgressPlugin.js:122:5)
at Compilation.applyPluginsAsyncSeries (C:\Development\source\\node_modules\tapable\lib\Tapable.js:142:13)
at self.applyPluginsAsync.err (C:\Development\source\\node_modules\webpack\lib\Compilation.js:631:11)
at next (C:\Development\source\\node_modules\tapable\lib\Tapable.js:138:11)
我的karma.conf看起来像这样
module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['jasmine', '@angular/cli'],
plugins: [
require('karma-jasmine'),
require('karma-jasmine-html-reporter'),
require('karma-coverage-istanbul-reporter'),
require('karma-chrome-launcher'),
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', 'json'],
fixWebpackSourcePaths: true
},
reporters: config.angularCli && config.angularCli.codeCoverage
? ['progress', 'coverage-istanbul']
: ['progress', 'kjhtml'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false
});
};
答案 0 :(得分:3)
我今天遇到了同样的问题,我的案例中的修复是对tsconfig.spec.json文件的更改。我从"module": "es2015"
更改了模块条目
到"module": "commonjs"
这解决了运行ng test --cc
的编译错误并创建了预期的覆盖率报告。希望这有帮助!