[英]Mocha, “require is not defined” when test in browser
I'm using Mocha 5.2.0, npm 6.1.0, and I'm trying to test in Chrome browser. 我正在使用Mocha 5.2.0,npm 6.1.0,并且正在尝试在Chrome浏览器中进行测试。
But When I run the test, the browser says "require is not defined". 但是,当我运行测试时,浏览器会显示“需求未定义”。
I want to see the test result in browser, but it works in terminal. 我想在浏览器中查看测试结果,但可以在终端中使用。
I'm not using build system like webpack etc. 我没有使用webpack等构建系统。
test/test.js 测试/ test.js
var assert = require("assert");
describe("Array", function() {
describe("#indexOf()", function() {
it("should return -1 when the value is not present", function() {
assert.equal([1,2,3].indexOf(4), -1);
});
});
});
index.html index.html
...
<link href="https://cdnjs.cloudflare.com/ajax/libs/mocha/5.2.0/mocha.min.css" rel="stylesheet">
...
<body>
<div id="mocha"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/5.2.0/mocha.min.js"></script>
<script>
mocha.setup("bdd");
</script>
<script src="test/test.js"></script>
<script>
mocha.checkLeaks();
mocha.run();
</script>
</body>
package.json package.json
{
"scripts": {
"test": "mocha"
}
}
Edited Karthikeyan 卡西凯扬(Karthikeyan) 编辑
index.html index.html
<script data-main="test/test.js" src="require.js"></script>
require.js:168 Uncaught Error: Module name "assert" has not been loaded yet for context: _. require.js:168未捕获的错误:尚未为上下文_加载模块名称“ assert”。 Use require([])
使用require([])
Require.js is missing . Require.js丢失。 Import require.js inside your body before importing mocha.js
在导入mocha.js之前,先将require.js导入体内
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.