我是 JavaScript 应用程序的 Jasmine/Karma 单元测试的新手。我正在尝试在我当前使用 Angular 1.4.12 和 Webpack 1.13.1 进行捆绑的项目中实现它。我的文件夹结构如下:
'core' 中的 index.js 文件试图要求 Webpack 捆绑所需的各种其他模块。该文件如下所示:
require('../../bower_components/font-awesome/css/font-awesome.min.css');
require('../../bower_components/bootstrap/dist/css/bootstrap.min.css');
require('./scripts/app');
require('./scripts/index');
require('./views/index');
require('./styles/index');
现在,当我尝试运行位于以下位置的示例测试文件时:'modules/st/scripts/controllers/st.test.js',我收到以下错误消息:
未捕获的错误:尚未为上下文加载模块名称“../../bower_components/font-awesome/css/font-awesome.min.css”:_。 在 C:/gitcode/repo/fm-dashboard/node_modules/requirejs/require.js:143使用 require([])
http://requirejs.org/docs/errors.html#notloaded
我的 karma.conf.js 文件如下所示:
var webpack = require('webpack');
var getWebpackConfig = require('./webpack.config.js');
var webpackConfig = getWebpackConfig('test');
webpackConfig.output.path = __dirname+'/_build/test';
webpackConfig.entry = {};
// Karma configuration
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: [
'./node_modules/requirejs/require.js',
'./app/core/index.js',
'./bower_components/angular/angular.js',
'./bower_components/angular-mocks/angular-mocks.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: {
'../app/core/index.js': ['webpack']
},
// 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: true,
// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity,
webpack: webpackConfig,
webpackMiddleware: {
noInfo: true
}
})
}
我的印象是在预处理器对象中包含 Webpack 可以解决这个需求问题,但似乎情况并非如此。
我尝试按照一些人的建议在我的预处理器对象和框架数组中包含“commonjs”,但它没有帮助。谁能让我知道如何摆脱这个“需要”问题并继续我的测试?
提前致谢。