Mocha,"require is not defined" 在浏览器中测试时
Mocha, "require is not defined" when test in browser
我使用的是 Mocha 5.2.0、npm 6.1.0,我正在尝试在 Chrome 浏览器中进行测试。
但是当我运行测试时,浏览器显示"require is not defined"。
我想在浏览器中查看测试结果,但它在终端中有效。
我没有使用 webpack 等构建系统
test/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
...
<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
{
"scripts": {
"test": "mocha"
}
}
已编辑 Karthikeyan
index.html
<script data-main="test/test.js" src="require.js"></script>
require.js:168 未捕获错误:尚未为上下文加载模块名称 "assert":_。使用 require([])
Require.js 缺失。在导入 mocha.js
之前将 require.js 导入体内
我使用的是 Mocha 5.2.0、npm 6.1.0,我正在尝试在 Chrome 浏览器中进行测试。
但是当我运行测试时,浏览器显示"require is not defined"。
我想在浏览器中查看测试结果,但它在终端中有效。
我没有使用 webpack 等构建系统
test/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
...
<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
{
"scripts": {
"test": "mocha"
}
}
已编辑 Karthikeyan
index.html
<script data-main="test/test.js" src="require.js"></script>
require.js:168 未捕获错误:尚未为上下文加载模块名称 "assert":_。使用 require([])
Require.js 缺失。在导入 mocha.js
之前将 require.js 导入体内