我正在使用jamamine进行node.js单元测试,使用karma工具。
我在运行时遇到错误:"要求未定义"。
我尝试过很多东西,比如使用karma-browserify,requirejs,但没有一个工作过。
我在这个link 中尝试了所有建议的解决方案,但它们都没有为我工作。
这是我的karma.config.js
// Karma configuration
// Generated on Mon Nov 28 2016 13:37:38 GMT+0530 (India 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',
'browserify'
],
// list of files / patterns to load in the browser
files: [
'src/**/*.js',
'src/server.js',
'test/*.js'
],
browserify: {
debug: true,
transform: [ 'brfs' ]
},
watchify: {
poll: true
},
// 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/*.js': [ 'browserify' ]
},
browserify: {
bundleDelay: 2000, // This may reduce instances of the karma-browserify "require is not defined" bug
debug: true,
},
// 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
})
};

package.json
"devDependencies": {
"browserify": "^13.1.1",
"grunt": "^0.4.5",
"grunt-contrib-jshint": "^0.11.3",
"grunt-jasmine-node": "^0.3.1",
"grunt-jscs": "^2.5.0",
"jasmine-core": "^2.2.0",
"karma": "^1.3.0",
"karma-browserify": "^5.1.0",
"karma-chrome-launcher": "^2.0.0",
"karma-jasmine": "^1.0.2",
"load-grunt-tasks": "^3.4.0",
"watchify": "^3.7.0"
}
答案 0 :(得分:0)
此错误可能是由require
和src/**/*.js
个文件中的src/server.js
次来电引起的。
您在浏览器中加载这些文件:
files: [
'src/**/*.js',
'src/server.js',
'test/*.js'
],
没有使用broserify进行预处理:
preprocessors: {
'test/*.js': [ 'browserify' ]
},