简体   繁体   English

摩卡,在浏览器中测试时“未定义需求”

[英]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.

相关问题 使用 mocha webdriver 在浏览器中测试时出现“ReferenceError: document is not defined” - "ReferenceError: document is not defined" when testing in-browser with mocha webdriver 当我尝试运行mocha测试时,工作人员没有定义 - Worker not defined when I try to run mocha test 摩卡测试跑步者(grunt-mocha-test)需要计时问题 - Mocha test runner (grunt-mocha-test) require timing issues $ 未在 mocha chai 测试脚本中定义 - $ is not defined in mocha chai test script Mocha测试以及对“ require”模块的后续调用 - Mocha test and subsequent calls to 'require' modules 如何在Mocha测试中要求相同的文件 - How to require same file in Mocha test 使用节点实现 Mapbox 时,在浏览器中接收“要求未定义” - Receiving 'require not defined' in browser when implementing Mapbox with node 引用或需要其他文件时,Visual Studio 2019 中的节点 js 应用程序的 Mocha 未在测试资源管理器中显示测试 - Mocha for node js app in Visual Studio 2019 is not showing Tests in Test Explorer when reference or require another file 尝试使用Chai&Mocha测试旧版jQuery插件时未定义jQuery - jQuery is not defined when trying to test legacy jQuery plugin using Chai & Mocha 使用RequireJS运行Mocha测试时,为什么会看到“define not defined”? - Why do I see “define not defined” when running a Mocha test with RequireJS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM