为什么我看到“define not defined”"使用RequireJS运行Mocha测试时? [英] Why do I see "define not defined" when running a Mocha test with RequireJS?

查看:314
本文介绍了为什么我看到“define not defined”"使用RequireJS运行Mocha测试时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解如何开发独立的Javascript代码。我想编写带有测试和模块的Javscript代码,从命令行运行。所以我已经安装 node.js npm 以及库 requirejs 下划线 mocha

I am trying to understand how to develop stand-alone Javascript code. I want to write Javscript code with tests and modules, running from the command line. So I have installed node.js and npm along with the libraries requirejs, underscore, and mocha.

我的目录结构如下所示:

My directory structure looks like this:

> tree .
.
├── node_modules
├── src
│   └── utils.js
└── test
    └── utils.js

其中 src / utils.js 是我的一个小模块使用以下代码撰写:

where src/utils.js is a little module that I am writing, with the following code:

> cat src/utils.js 
define(['underscore'], function () {

    "use strict";

    if ('function' !== typeof Object.beget) {
        Object.beget = function (o) {
            var f = function () {
            };
            f.prototype = o;
            return new f();
        };
    }

});

test / utils.js 是测试:

> cat test/utils.js 
var requirejs = require('requirejs');
requirejs.config({nodeRequire: require});

requirejs(['../src/utils'], function(utils) {

    suite('utils', function() {
        test('should always work', function() {
            assert.equal(1, 1);
        })
    })

});

然后我尝试从顶级目录运行(所以 mocha 看到测试目录):

which I then try to run from the top level directory (so mocha sees the test directory):

> mocha

node.js:201
        throw e; // process.nextTick error, or 'error' event on first tick
              ^
Error: Calling node's require("../src/utils") failed with error: ReferenceError: define is not defined
    at /.../node_modules/requirejs/bin/r.js:2276:27
    at Function.execCb (/.../node_modules/requirejs/bin/r.js:1872:25)
    at execManager (/.../node_modules/requirejs/bin/r.js:541:31)
    ...

所以我的问题是:


  • 这是构造代码的正确方法吗?

  • 为什么我的测试没有运行?

  • 学习这类东西的最佳方法是什么?我很难在谷歌找到好的例子。

谢谢......

[抱歉 - 暂时发布错误代码的结果;现在修复了]

[sorry - momentarily posted results from wrong code; fixed now]

PS我正在使用requirejs,因为我也希望稍后从浏览器运行此代码(或其中一些代码)。

PS I am using requirejs because I also want to run this code (or some of it) from a browser, later.

更新/解决方案

以下答案中没有的内容是我需要使用 mocha -u tdd 用于上面的测试样式。这是最终测试(也需要断言)及其使用:

Something that is not in the answers below is that I needed to use mocha -u tdd for the test style above. Here is the final test (which also requires assert) and its use:

> cat test/utils.js 

var requirejs = require('requirejs');
requirejs.config({nodeRequire: require});

requirejs(['../src/utils', 'assert'], function(utils, assert) {

    suite('utils', function() {
        test('should always work', function() {
            assert.equal(1, 1);
        })
    })

});
> mocha -u tdd

  .

  ✔ 1 tests complete (1ms)


推荐答案

您的测试未运行的原因是因为 src / utils.js 不是有效的Node.js库。

The reason your test isn't running is because src/utils.js is not a valid Node.js library.

根据RequireJS文档,为了与Node.js和CommonJS require标准共存,你需要在您的 src / utils.js 文件的顶部添加一些样板文件,以便RequireJS的定义函数已加载。

According to the RequireJS documentation, in order to co-exist with Node.js and the CommonJS require standard, you need to add a bit of boilerplate to the top of your src/utils.js file so RequireJS's define function is loaded.

但是,由于RequireJS旨在能够要求经典面向Web浏览器的源代码,因此我倾向于使用以下模式:我想在浏览器中运行的Node.js库:

However, since RequireJS was designed to be able to require "classic" web browser-oriented source code, I tend to use the following pattern with my Node.js libraries that I also want running in the browser:

if(typeof require != 'undefined') {
    // Require server-side-specific modules
}

// Insert code here

if(typeof module != 'undefined') {
    module.exports = whateverImExporting;
}

这样做的好处是不需要为其他Node.js用户提供额外的库并且通常在客户端上与RequireJS一起使用。

This has the advantage of not requiring an extra library for other Node.js users and generally works well with RequireJS on the client.

一旦在Node.js中运行代码,就可以开始测试了。我个人仍然喜欢使用Expresso而不是mocha,即使它是后继测试框架。

Once you get your code running in Node.js, you can start testing. I personally still prefer expresso over mocha, even though its the successor test framework.

这篇关于为什么我看到“define not defined”"使用RequireJS运行Mocha测试时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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