[英]Angular Cli - SyntaxError: 'import' and 'export' may appear only with 'sourceType: module'
I am trying to generate coverage report with angular cli. 我正在尝试生成带有角度cli的覆盖率报告。 however I keep getting following error.
但是我不断收到以下错误。 I am not quite sure if This is cli error or problem with my Karma.conf?
我不确定这是cli错误还是我的Karma.conf有问题?
ng test
/ npm test
Runs fine... So something to do with code-coverage option? 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)
My karma.conf looks like this 我的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
});
};
I hit the same issue today, the fix in my case was a change to the tsconfig.spec.json file. 我今天遇到了同样的问题,我的问题是对tsconfig.spec.json文件的更改。 I changed the module entry from
"module": "es2015"
to "module": "commonjs"
我将模块条目从
"module": "es2015"
更改为"module": "commonjs"
This resolved the compilation error running ng test --cc
and created the expected coverage report. 这解决了运行
ng test --cc
的编译错误,并创建了预期的覆盖率报告。 Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.