简体   繁体   English

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

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

This seems like it should be 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 Mocha with Should.js to test some JavaScript functions, but I'm running into scoping issues. 我正在尝试使用MochaShould.js来测试一些JavaScript函数,但我遇到了范围问题。 I've simplified it down to the most basic of test cases, but I cannot get it working. 我已将其简化为最基本的测试用例,但我无法使其正常工作。

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失败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 global object 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. 我可以看到原因,因为我发现大多数示例都是将对象和函数附加到Node global对象或使用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的语法的情况下访问在我的测试的单独脚本文件中声明的独立函数?

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 require call 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);
        })
    })
})
require('./functions.js')

That doesn't do anything since you're not exporting anything. 这不会做任何事情,因为你没有出口任何东西。 What you're expecting is that testFunction is globally available, essentially the same as 您期望的是testFunction是全局可用的,基本上testFunction相同

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

You just can't bypass the export/globals mechanism. 无法绕过export / globals机制。 It's the way node has been designed. 这是节点设计的方式。 There is no implicit global shared context (like window on a browser). 没有隐式的全局共享上下文(如浏览器上的window )。 Every "global" variable in a module is trapped in it's context. 模块中的每个“全局”变量都被困在其上下文中。

You should use module.exports . 你应该使用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) 对于快速破解,只需要做window.module = {}; jQuery.extend(window, module.exports) window.module = {}; jQuery.extend(window, module.exports) in the browser, or if (typeof exports !== 'undefined'){ exports.testFunction = testFunction } for node. 浏览器中的window.module = {}; jQuery.extend(window, module.exports) ,或节点的if (typeof exports !== 'undefined'){ exports.testFunction = testFunction }

If you want to make any module available through require you should use 如果您想要通过要求使用任何模块,您应该使用

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 通过这样做,您将能够在浏览器和节点环境中使用该文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM