【发布时间】:2017-04-12 04:33:43
【问题描述】:
我正在使用 jasmine 使用 karma 工具进行 node.js 单元测试。
运行时出现错误:“require is not defined”。
我尝试了很多方法,例如使用 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"
}
【问题讨论】:
-
budleDelay中有一个类型。应该是bundleDelay。 -
对不起,错字,但这不是原因。我改正了,还是不行
-
你在使用 jasmine-node 吗? github.com/mhevery/jasmine-node
-
不,我已经用 package.json 更新了问题
标签: node.js unit-testing jasmine karma-runner