我正在尝试使用Karma + Jasmine对我的Aurelia(+ Webpack + TypeScript)应用进行单元测试。我尝试使用the skeleton附带的测试样板。
在骨架项目中,以下npm脚本运行完美:
"test": "cross-env NODE_ENV=test ./node_modules/karma/bin/karma start test/karma.conf.js"
但是当我在我的项目上尝试相同时,它会抛出错误(没有进一步的堆栈跟踪):
DEBUG [config]: Loading config C:\Path\to\my\Website\test\karma.conf.js
ERROR [config]: Invalid config file!
它不提供有关配置无效原因的任何其他信息。
以下是骨架附带的karma.conf.js
:
"use strict";
const path = require('path');
module.exports = function(config) {
config.set({
// base path that will be used to resolve all patterns (e.g. files, exclude)
basePath: __dirname,
/*
* Frameworks to use
*
* available frameworks: https://npmjs.org/browse/keyword/karma-adapter
*/
frameworks: ['jasmine'],
// list of files to exclude
exclude: [ ],
/*
* list of files / patterns to load in the browser
*
* we are building the test environment in ./spec-bundle.js
*/
files: [
{ pattern: 'spec-bundle.js', watched: false },
],
/*
* preprocess matching files before serving them to the browser
* available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
*/
preprocessors: {
'spec-bundle.js': ['coverage', 'webpack', 'sourcemap']
},
webpack: require('../webpack.config'),
coverageReporter: {
reporters: [{
type: 'json',
subdir: '.',
file: 'coverage-final.json'
}]
},
remapIstanbulReporter: {
src: path.join(__dirname, 'coverage/coverage-final.json'),
reports: {
html: path.join(__dirname, 'coverage/')
},
timeoutNotCreated: 1000,
timeoutNoMoreFiles: 1000
},
// Webpack please don't spam the console when running in karma!
webpackServer: { noInfo: true},
/*
* test results reporter to use
*
* possible values: 'dots', 'progress'
* available reporters: https://npmjs.org/browse/keyword/karma-reporter
*/
reporters: [ 'mocha', 'coverage', 'karma-remap-istanbul' ],
// web server port
port: 9876,
// enable / disable colors in the output (reporters and logs)
colors: true,
/*
* level of logging
* possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
*/
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,
/*
* start these browsers
* available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
*/
browsers: [
'Chrome',
// TODO: https://www.npmjs.com/package/karma-electron
],
/*
* Continuous Integration mode
* if true, Karma captures browsers, runs the tests and exits
*/
singleRun: true
});
};
要解决此问题,我还尝试使用karma.conf.js
在我的网站根目录下开始生成新的karma init
,并且我遇到与karma start
完全相同的错误。下面是配置文件,因此生成:
// Karma configuration
// Generated on Wed Dec 14 2016 07:50:15 GMT+0100 (W. Europe Standard Time)
module.exports = function (config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
'./test/**/*Spec.js'
],
// list of files to exclude
exclude: [
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],
// web server port
port: 9876,
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false,
// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity
});
};
我使用的是以下版本的npm包:
"karma": "^1.3.0",
"karma-chrome-launcher": "^2.0.0",
"karma-coverage": "^1.1.1",
"karma-jasmine": "^1.0.2",
"karma-mocha-reporter": "^2.2.0",
"karma-remap-istanbul": "^0.2.1",
"karma-sourcemap-loader": "^0.3.7",
"karma-webpack": "^1.8.0",
"typescript": "2.1.1",
"webpack": "2.1.0-beta.27",
请建议如何解决此问题,或至少要调查更多(调试)信息。
更新:在rimraf
node_modules
之后获得以下堆栈跟踪并重新安装软件包:
DEBUG [config]: Loading config C:\Path\to\my\Website\test\karma.conf.js
ERROR [config]: Invalid config file!
TSError: ⨯ Unable to compile TypeScript
Cannot find type definition file for 'dir1'. (2688)
Cannot find type definition file for 'dir2'. (2688)
Cannot find type definition file for 'dir3'. (2688)
Cannot find type definition file for 'lang1'. (2688)
Cannot find type definition file for 'dir4'. (2688)
Cannot find type definition file for 'lang2'. (2688)
Cannot find type definition file for 'maps'. (2688)
Cannot find type definition file for 'valueConverters'. (2688)
Cannot find type definition file for 'views'. (2688)
at getOutput (C:\Path\to\my\Website\node_modules\ts-node\src\index.ts:312:17)
at C:\Path\to\my\Website\node_modules\ts-node\src\index.ts:343:18
at Object.compile (C:\Path\to\my\Website\node_modules\ts-node\src\index.ts:476:19)
at Module.m._compile (C:\Path\to\my\Website\node_modules\ts-node\src\index.ts:406:44)
at Module._extensions..js (module.js:579:10)
at Object.require.extensions.(anonymous function) [as .js] (C:\Path\to\my\Website\node_modules\ts-node\src\index.ts:409:12)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.parseConfig (C:\Path\to\my\Website\node_modules\karma\lib\config.js:342:22)
at new Server (C:\Path\to\my\Website\node_modules\karma\lib\server.js:56:20)
at Object.exports.run (C:\Path\to\my\Website\node_modules\karma\lib\cli.js:280:7)
at Object.<anonymous> (C:\Path\to\my\Website\node_modules\karma\bin\karma:3:23)
at Module._compile (module.js:570:32)
答案 0 :(得分:2)
今天早上遇到同样的问题,我发现更改ts节点版本解决了这个问题。我可以在没有使用ts-node@3.2.0进行任何配置更改的情况下重现此问题,但是使用ts-node@3.1.0整个版本都可以正常工作,所以看起来有一个重大变化。