如何使用Mocha测试“正常”(非节点特定)JavaScript函数? [英] How do I test 'normal' (non-Node specific) JavaScript functions with Mocha?

查看:61
本文介绍了如何使用Mocha测试“正常”(非节点特定)JavaScript函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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!

我正在尝试使用摩卡使用 Should.js 来测试一些JavaScript函数,但我遇到了范围问题。我已将其简化为最基本的测试用例,但我无法使其正常工作。

I'm trying to use Mocha with Should.js to 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.

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

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

function testFunction() {
    return 1;
}

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

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

require('./functions.js')

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

此测试失败并带有 ReferenceError:未定义testFunction

我明白为什么,因为我发现的大多数例子都是将对象和函数附加到Node global 对象,或者使用 module.exports -but使用这些方法中的任何一种意味着我的功能代码会在标准浏览器情况下抛出错误,而这些对象不存在。

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-sp ecific语法?

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

推荐答案

require('./functions.js')

由于您没有输出任何内容,因此无法执行任何操作。您期望的是 testFunction 是全球可用的,基本上与

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

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

你只是无法绕过导出/全局机制。这是节点设计的方式。浏览器上没有隐式的全局共享上下文(如窗口)。模块中的每个全局变量都被困在其上下文中。

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

您应该使用 module.exports 。如果您打算与浏览器环境共享该文件,可以使其兼容。快速破解只需 window.module = {};浏览器中的jQuery.extend(window,module.exports),或 if(typeof exports!=='undefined'){exports.testFunction = testFunction} for node。

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.

这篇关于如何使用Mocha测试“正常”(非节点特定)JavaScript函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆