<tfoot id='jr6tL'></tfoot>
<i id='jr6tL'><tr id='jr6tL'><dt id='jr6tL'><q id='jr6tL'><span id='jr6tL'><b id='jr6tL'><form id='jr6tL'><ins id='jr6tL'></ins><ul id='jr6tL'></ul><sub id='jr6tL'></sub></form><legend id='jr6tL'></legend><bdo id='jr6tL'><pre id='jr6tL'><center id='jr6tL'></center></pre></bdo></b><th id='jr6tL'></th></span></q></dt></tr></i><div id='jr6tL'><tfoot id='jr6tL'></tfoot><dl id='jr6tL'><fieldset id='jr6tL'></fieldset></dl></div>

      <bdo id='jr6tL'></bdo><ul id='jr6tL'></ul>

    <small id='jr6tL'></small><noframes id='jr6tL'>

    1. <legend id='jr6tL'><style id='jr6tL'><dir id='jr6tL'><q id='jr6tL'></q></dir></style></legend>

    2. 如何从 mocha.opts 文件中正确地要求模块

      时间:2023-05-27
      <i id='OlxNL'><tr id='OlxNL'><dt id='OlxNL'><q id='OlxNL'><span id='OlxNL'><b id='OlxNL'><form id='OlxNL'><ins id='OlxNL'></ins><ul id='OlxNL'></ul><sub id='OlxNL'></sub></form><legend id='OlxNL'></legend><bdo id='OlxNL'><pre id='OlxNL'><center id='OlxNL'></center></pre></bdo></b><th id='OlxNL'></th></span></q></dt></tr></i><div id='OlxNL'><tfoot id='OlxNL'></tfoot><dl id='OlxNL'><fieldset id='OlxNL'></fieldset></dl></div>

              <bdo id='OlxNL'></bdo><ul id='OlxNL'></ul>
            • <small id='OlxNL'></small><noframes id='OlxNL'>

                <tbody id='OlxNL'></tbody>
              • <tfoot id='OlxNL'></tfoot>

                <legend id='OlxNL'><style id='OlxNL'><dir id='OlxNL'><q id='OlxNL'></q></dir></style></legend>
                本文介绍了如何从 mocha.opts 文件中正确地要求模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                我在 mocha 单元测试中使用了 expect.js 库.目前,我需要在每个文件的第一行使用该库,如下所示:

                I'm using the expect.js library with my mocha unit tests. Currently, I'm requiring the library on the first line of each file, like this:

                var expect = require('expect.js');
                
                describe('something', function () {
                    it('should pass', function () {
                        expect(true).to.be(true); // works
                    });
                });
                

                如果可能,我想从每个文件的第一行删除样板要求代码,并让我的单元测试神奇地了解 expect.我想我可以使用 mocha.opts 文件来做到这一点:

                If possible, I'd like to remove the boilerplate require code from the first line of each file, and have my unit tests magically know about expect. I thought I might be able to do this using the mocha.opts file:

                --require ./node_modules/expect.js/index.js
                

                但现在我在运行测试时收到以下错误:

                But now I get the following error when running my test:

                ReferenceError: 未定义期望

                ReferenceError: expect is not defined

                这似乎是有道理的——它怎么知道我的测试中对 expect 的引用指的是由 expect.js 库导出的内容?

                This seems to make sense - how can it know that the reference to expect in my tests refers to what is exported by the expect.js library?

                expect 库肯定会被加载,就好像我将路径更改为不存在的东西然后 mocha 说:

                The expect library is definitely getting loaded, as if I change the path to something non-existent then mocha says:

                错误:找不到模块'./does-not-exist.js'"

                "Error: Cannot find module './does-not-exist.js'"

                有什么方法可以完成我想要的吗?如果可能有帮助的话,我正在从一个 gulp 任务中运行我的测试.

                Is there any way to accomplish what I want? I'm running my tests from a gulp task if perhaps that could help.

                推荐答案

                您正确地需要模块,但正如您所发现的,模块导出的符号不会自动找到自己进入全局空间.您可以使用自己的帮助模块来解决此问题.

                You are requiring the module properly but as you figured out, the symbols that the module export won't automatically find themselves into the global space. You can remedy this with your own helper module.

                创建test/helper.js:

                var expect = require("expect.js")
                
                global.expect = expect;
                

                并将您的 test/mocha.opts 设置为:

                --require test/helper
                

                这篇关于如何从 mocha.opts 文件中正确地要求模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:增量 gulp 少构建 下一篇:ES6 的 Visual Studio 任务运行程序错误

                相关文章

                最新文章

                <legend id='svADk'><style id='svADk'><dir id='svADk'><q id='svADk'></q></dir></style></legend>

                • <bdo id='svADk'></bdo><ul id='svADk'></ul>
                <i id='svADk'><tr id='svADk'><dt id='svADk'><q id='svADk'><span id='svADk'><b id='svADk'><form id='svADk'><ins id='svADk'></ins><ul id='svADk'></ul><sub id='svADk'></sub></form><legend id='svADk'></legend><bdo id='svADk'><pre id='svADk'><center id='svADk'></center></pre></bdo></b><th id='svADk'></th></span></q></dt></tr></i><div id='svADk'><tfoot id='svADk'></tfoot><dl id='svADk'><fieldset id='svADk'></fieldset></dl></div>

                <tfoot id='svADk'></tfoot>

                    <small id='svADk'></small><noframes id='svADk'>