我试图用Karma和Jasmine对我的Angular代码进行单元测试。导航到项目文件夹后,我按照上述顺序执行了以下命令:
npm install --save-dev jasmine (用于安装Jasmine)
npm install --save-dev karma (用于安装Karma)
npm install --save-dev karma-jasmine (用于安装Karma依赖项)
业力初始
之后当我尝试运行业力开始时,我收到以下错误,其中显示执行0 0错误并且没有具体内容。
我为什么要这样做的任何线索?我还在下面添加了我的karma.config.js和spec-bundle.js文件内容。
我的karma.config.js文件:
// Karma configuration file, see link for more information
// https://karma-runner.github.io/0.13/config/configuration-file.html
module.exports = function (config) {
config.set({
basePath: '.',
frameworks: ['jasmine'],
files: [
'../../wwwroot/dist/vendor.js',
'./boot-tests.ts'
],
preprocessors: {
'./boot-tests.ts': ['webpack']
},
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
//browsers: ['PhantomJS'], // use headless browswer
browsers: [
'Chrome',
'Chrome_with_debugging'
],
customLaunchers: {
Chrome_with_debugging: {
base: 'Chrome',
//chromeDataDir: path.resolve(__dirname, '.chrome'),
flags: ['--remote-debugging-port=9333'],
displayName: 'Chrome with remote debugging'
}
},
mime: { 'application/javascript': ['ts', 'tsx'] },
// Which plugins to enable defaults to karma-*
singleRun: false,
webpack: require('../../webpack.config.js')().filter(config => config.target !== 'node'), // Test against client bundle, because tests run in a browser
webpackMiddleware: { stats: 'errors-only' }
});
};

我的spec-bundle.js文件:
/*
* When testing with webpack and ES6, we have to do some extra
* things to get testing to work right. Because we are gonna write tests
* in ES6 too, we have to compile those as well. That's handled in
* karma.conf.js with the karma-webpack plugin. This is the entry
* file for webpack test. Just like webpack will create a bundle.js
* file for our client, when we run test, it will compile and bundle them
* all here! Crazy huh. So we need to do some setup
*/
Error.stackTraceLimit = Infinity;
require('core-js/es6');
require('core-js/es7/reflect');
require('zone.js/dist/zone');
require('zone.js/dist/long-stack-trace-zone');
require('zone.js/dist/proxy'); // since zone.js 0.6.15
require('zone.js/dist/sync-test');
require('zone.js/dist/jasmine-patch'); // put here since zone.js 0.6.14
require('zone.js/dist/async-test');
require('zone.js/dist/fake-async-test');
// RxJS
require('rxjs/Rx');
var testing = require('@angular/core/testing');
var browser = require('@angular/platform-browser-dynamic/testing');
testing.TestBed.initTestEnvironment(
browser.BrowserDynamicTestingModule,
browser.platformBrowserDynamicTesting()
);
/*
* Ok, this is kinda crazy. We can use the context method on
* require that webpack created in order to tell webpack
* what files we actually want to require or import.
* Below, context will be a function/object with file names as keys.
* Using that regex we are saying look in ../Client then find
* any file that ends with spec.ts and get its path. By passing in true
* we say do this recursively
*/
var testContext = require.context('../ClientApp', true, /\.spec\.ts/);
/*
* get all the files, for each file, call the context function
* that will require the file and load it up here. Context will
* loop and require those spec files here
*/
function requireAll(requireContext) {
return requireContext.keys().map(requireContext);
}
// requires and returns all modules that match
var modules = requireAll(testContext);