如何使用 Mocha 测试“正常”(非节点特定)JavaScript 函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10204021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 09:01:30  来源:igfitidea点击:

How do I test 'normal' (non-Node specific) JavaScript functions with Mocha?

javascriptunit-testingnode.jsmocha

提问by Mark Bell

This seems like it shouldbe extremely simple; however, after two hours of reading and trial-and-error without success, I'm admitting defeat and asking you guys!

这看起来应该非常简单;然而,经过两个小时的阅读和反复试验没有成功,我认输并问你们!

I'm trying to use Mochawith Should.jsto test some JavaScript functions, but I'm running into scoping issues. I've simplified it down to the most basic of test cases, but I cannot get it working.

我正在尝试使用MochaShould.js来测试一些 JavaScript 函数,但我遇到了范围界定问题。我已经将它简化为最基本的测试用例,但我无法让它工作。

I have a file named functions.js, which just contains the following:

我有一个名为 的文件functions.js,其中仅包含以下内容:

function testFunction() {
    return 1;
}

And my tests.js(located in the same folder) contents:

我的tests.js(位于同一文件夹中)内容:

require('./functions.js')

describe('tests', function(){
    describe('testFunction', function(){
        it('should return 1', function(){
            testFunction().should.equal(1);
        })
    })
})

This test fails with a ReferenceError: testFunction is not defined.

此测试失败并显示ReferenceError: testFunction is not defined.

I can see why, because most of the examples I've found either attach objects and functions to the Node globalobject or export them using module.exports—but using either of these approaches means my function code would throw errors in a standard browser situation, where those objects don't exist.

我可以理解为什么,因为我发现的大多数示例要么将对象和函数附加到 Nodeglobal对象module.exports,要么使用- 但使用这两种方法中的任何一种都意味着我的函数代码会在标准浏览器情况下抛出错误,其中这些对象不存在。

So how can I access standalone functions which are declared in a separate script file from my tests, without using Node-specific syntax?

那么如何在不使用 Node 特定语法的情况下访问在我的测试的单独脚本文件中声明的独立函数?

采纳答案by Ricardo Tomasi

require('./functions.js')

That doesn't do anything since you're not exporting anything. What you're expecting is that testFunctionis globally available, essentially the same as

这没有任何作用,因为您没有出口任何东西。你所期待的是它testFunction是全球可用的,本质上与

global.testFunction = function() {
    return 1;
}

You just can'tbypass the export/globals mechanism. It's the way node has been designed. There is no implicit global shared context (like windowon a browser). Every "global" variable in a module is trapped in it's context.

无法绕过导出/全局机制。这就是节点的设计方式。没有隐式的全局共享上下文(就像window在浏览器上一样)。模块中的每个“全局”变量都被困在它的上下文中。

You should use module.exports. If you intend to share that file with a browser environments, there are ways to make it compatible. For a quick hack just do window.module = {}; jQuery.extend(window, module.exports)in the browser, or if (typeof exports !== 'undefined'){ exports.testFunction = testFunction }for node.

你应该使用module.exports. 如果您打算与浏览器环境共享该文件,则可以通过多种方法使其兼容。要快速破解,只需window.module = {}; jQuery.extend(window, module.exports)在浏览器中执行,或if (typeof exports !== 'undefined'){ exports.testFunction = testFunction }在 node.js 中执行。

回答by Mark Bell

Thanks to the other answers here, I've got things working.

感谢这里的其他答案,我已经开始工作了。

One thing which wasn't mentioned though—perhaps because it's common knowledge among Noders—was that you need to assign the result of the requirecall to a variable, so that you can refer to it when calling your exported functions from within the test suite.

但是没有提到的一件事——也许是因为 Noders 之间的常识——是你需要将require调用的结果分配给一个变量,这样你就可以在从测试套件中调用导出的函数时引用它。

Here's my complete code, for future reference:

这是我的完整代码,以供将来参考:

functions.js:

functions.js

function testFunction () {
    return 1;
}

// If we're running under Node, 
if(typeof exports !== 'undefined') {
    exports.testFunction = testFunction;
}

tests.js:

tests.js

var myCode = require('./functions')

describe('tests', function(){
    describe('testFunction', function(){
        it('should return 1', function(){
            // Call the exported function from the module
            myCode.testFunction().should.equal(1);
        })
    })
})

回答by drinchev

If you want to make any module available through require you should use

如果你想通过 require 使任何模块可用,你应该使用

module.exports

as you know ;)

如你所知 ;)

there is a solution if you want to use a module in Node and in browser by doing this

如果您想通过执行此操作在 Node 和浏览器中使用模块,则有一个解决方案

function testFunction() { /* code */ }

if (typeof exports !== 'undefined') {
   exports.testFunction = testFunction
}

by doing this you will be able to use the file in browser and in node environment

通过这样做,您将能够在浏览器和节点环境中使用该文件