我正在尝试使用karma和jasmine运行测试,但我正面临着这样的问题" Uncaught ReferenceError:require未定义"错误。对不起......你能帮我弄清楚我做错了什么吗?
请参阅我的环境,发生此错误的位置,目录结构为:
myproj
|_ tests
| |_ example_spec.js
|
|_ karma-conf.js
|_ package.json
example_spec.js文件包含以下代码:
var chai = require('chai');
var expect = chai.expect;
describe('Given an array', function () {
'use strict';
describe('the method push', function () {
it('should add an element', function () {
var arr = [];
arr.push("hello");
expect(arr.length).to.equal(1);
expect(arr).to.contain("hello");
});
});
});
package.json文件具有以下依赖项:
"devDependencies": {
"grunt": "~0.4.5",
"frisby": "~0.8.5",
"load-grunt-config": "~0.16.0",
"karma": "~0.12.31",
"karma-jasmine": "~0.3.5",
"karma-chrome-launcher": "~0.1.7",
"karma-junit-reporter": "~0.2.2",
"chai": "~2.2.0",
"chai-as-promised": "~4.3.0",
"jasmine-node-karma": "~1.6.1",
"jasmine-node": "~1.14.5"
}
然后,我尝试按以下命令运行测试:
./node_modules/karma/bin/karma start karma-conf.js
引发以下引用错误:
Uncaught ReferenceError: require is not defined
at /home/dir/dir/myproj/tests/example_spec.js:4
注意:Mocha不参与此项目。