在Angular应用程序中,我试图运行我的业力单元测试并生成TS报告。但是,预处理器编译我在karma.conf.js中映射的TS文件时似乎存在问题。
我不确定是否需要单独的Karma库来预编译TS文件...
这是我运行测试时遇到的错误:
START:
ℹ 「wdm」: Hash: c43e7d5fdeaf3f27d46f
Version: webpack 2.7.0
Time: 2228ms
ℹ 「wdm」: Compiled successfully.
ℹ 「wdm」: Compiling...
ℹ 「wdm」: wait until bundle finished: noop
...
10 08 2018 13:18:57.946:ERROR [preprocessor.coverage]: Line 1: Unexpected token
at /Users/bbns/net/balances/ng-balances/lib/src/services/quote-bar.service.spec.d.ts
Failed to parse file: /Users/bbns/net/balances/ng-balances/lib/src/services/quote-bar.service.ts
10 08 2018 13:18:57.939:ERROR [preprocessor.coverage]: Line 1: Unexpected token
...
ERROR in ./src/services/quote-bar.service.spec.ts
Module parse failed: /Users/bbns/net/balances/ng-balances/lib/src/services/quote-bar.service.spec.d.ts Unexpected token (8:23)
**You may need an appropriate loader to handle this file type.**
|
| describe('QuoteBarService', () => {
| let quoteBarService: QuoteBarService;
| let logService: LogService;
| let dateTimeString: string;
ℹ 「wdm」: Failed to compile.
10 08 2018 15:48:22.801:INFO [karma]: Karma v2.0.5 server started at http://0.0.0.0:9876/
10 08 2018 15:48:22.801:INFO [launcher]: Launching browser Chrome with unlimited concurrency
10 08 2018 15:48:22.807:INFO [launcher]: Starting browser Chrome
这是我的karma.conf.js
'use strict';
module.exports = function (config) {
var configuration = {
basePath: '.',
frameworks: [ 'jasmine-ajax', 'jasmine' ],
files: [
'src/services/*.spec.ts',
{ pattern: "node_modules/typescript/**/*.ts" },
// System.js for module loading
'node_modules/systemjs/dist/system.src.js',
'karma-test-shim.js',
// Polyfills
'node_modules/core-js/client/shim.js',
'node_modules/reflect-metadata/Reflect.js',
// zone.js
'node_modules/zone.js/dist/zone.js',
'node_modules/zone.js/dist/long-stack-trace-zone.js',
'node_modules/zone.js/dist/proxy.js',
'node_modules/zone.js/dist/sync-test.js',
'node_modules/zone.js/dist/jasmine-patch.js',
'node_modules/zone.js/dist/async-test.js',
'node_modules/zone.js/dist/fake-async-test.js',
'node_modules/hammerjs/hammer.js',
// RxJs
{ pattern: 'node_modules/rxjs/**/*.js', included: false, watched: false },
{ pattern: 'node_modules/rxjs/**/*.js.map', included: false, watched: false },
// tslib
{ pattern: 'node_modules/tslib/tslib.js', included: false, watched: false },
// Paths loaded via module imports:
// Angular itself
{ pattern: 'node_modules/@angular/**/*.js', included: false, watched: false },
{ pattern: 'node_modules/@angular/**/*.js.map', included: false, watched: false },
{ pattern: 'node_modules/@ngx-translate/core/**/*.js', included: false, watched: false },
{ pattern: 'node_modules/ngx-responsive/**/*.js', included: false, watched: false },
{ pattern: 'node_modules/moment-timezone/**/*.js', included: false, watched: false },
// { pattern: 'node_modules/moment-timezone/**/*.js.map', included: false, watched: false },
{ pattern: 'node_modules/moment/**/*.js', included: false, watched: false },
// { pattern: 'node_modules/moment/**/*.js.map', included: false, watched: false },
{ pattern: 'node_modules/@pershing/**/bundles/*.js', included: false, watched: false },
// ag-grid related files for testing
{ pattern: 'node_modules/ag-grid-angular/dist/**/*.js', included: false, served: true, watched: false },
{ pattern: 'node_modules/ag-grid-angular/main.js', included: false, served: true, watched: false },
{ pattern: 'node_modules/ag-grid/dist/**/*.js', included: false, served: true, watched: false },
{ pattern: 'node_modules/ag-grid/main.js', included: false, served: true, watched: false },
// include a theme to avoid warnings during test execution
{ pattern: 'node_modules/@angular/material/prebuilt-themes/indigo-pink.css', included: true, watched:true, served: true },
// paths loaded via module imports
{pattern: 'src/**/*.js', included: false, watched: true},
{pattern: 'src/**/*.html', included: true, served: true, watched: true},
{pattern: 'src/**/*.css', included: true, served: true, watched: true},
// paths to support debugging with source maps in dev tools
{ pattern: 'src/**/*.ts', included: false, watched: false },
{ pattern: 'src/**/*.js.map', included: false, watched: false },
// paths to support testing sharing
{ pattern: 'index.js', included: false, watched: false },
{ pattern: 'index.js.map', included: false, watched: false }
],
exclude: [
'node_modules/**/*spec.js'
],
port: 9876,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
colors: true,
autoWatch: true,
browsers: ['Chrome'],
customLaunchers: {
HeadlesChrome: {
base: 'Chrome',
flags: [ '--no-sandbox', '--headless' ]
}
},
// Coverage reporter generates the coverage
reporters: [ 'coverage', 'progress', 'remap-coverage', 'karma-coverage', 'karma-remap-coverage' ],
preprocessors: {
'src/services/*.ts': ['coverage'],
'src/services/*.spec.ts': ['webpack', 'sourcemap']
},
client: {
captureConsole: true,
mocha: {
bail: true
}
},
coverageReporter: {
includeAllSources: true,
type: 'in-memory',
dir: 'coverage/',
subdir: 'report',
reporters: [
{type: 'lcov'}
]
},
remapCoverageReporter: {
'text-summary': null,
html: './coverage/html',
cobertura: './coverage/cobertura.xml'
},
};
config.set(configuration)
};
这是我的ts.config.json
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"skipLibCheck": true,
"noLib": false,
"allowUnreachableCode": false,
"allowUnusedLabels": false,
"noImplicitAny": false,
"noImplicitReturns": false,
"noImplicitUseStrict": false,
"noFallthroughCasesInSwitch": true,
"removeComments": true,
"declaration": true,
"lib": [
"es2015",
"dom"
],
"typeRoots": [
"node_modules/@types"
],
"suppressImplicitAnyIndexErrors": true
},
"include": [
"src/**/*.ts",
"node_modules/typescript/**/*.ts"
],
"exclude": [
"demo",
"node_modules",
"dist",
"tools",
"gulpfile.ts",
"gulpfile.d.ts"
],
"angularCompilerOptions": {
"strictMetadataEmit": false,
"skipTemplateCodegen": true
}
}
...最后在我的package.json中,这是
script that runs the test:
"scripts": {
...
"test": "npm run build && karma start karma.conf.js --reporters coverage --single-run",
...
},