简体   繁体   English

使用Intern Js测试节点模块-无法解决“要求”

[英]Testing a Node Module with Intern Js- Can't resolve 'require'

I am trying to test a simple node Module with intern.The module is an AMD module. 我正在尝试使用实习生测试简单的节点模块。该模块是AMD模块。 I am running into a couple of issues 我遇到了两个问题

  1. If the module is defined as below, I get the error "moduleName" has no method 'map' " 如果模块定义如下,则会出现错误“ moduleName”没有方法“ map”

    define('moduleName',[]function(require){ var r= require('request'); })

  2. If the module is defined as below without a moduleName, I see this error "undefined is not a function" - I guess its not able to resolve 'require' 如果模块定义如下但没有moduleName,我会看到此错误“未定义不是函数”-我猜它无法解析“ require”

    define([]function(require){ var r= require('request'); })

Here is how my Test Looks 这是我的测试外观

define([
'intern!object',
'intern/chai!assert',
'/src/api/nameApi'
], function (registerSuite, assert,nameApi) {
    registerSuite({
    name: 'GetName Test',
    getName: function () {
        var nameFromApi = nameApi.getName();
        assert( nameFromApi!= null,'name is not null');
    }
});

}); });

Providing an explicit module ID as the first argument to define destroys module portability and shouldn't be done. 提供一个明确的模块ID作为define的第一个参数会破坏模块的可移植性,因此不应该这样做。 It is not currently supported by the loader used by the master branch because it is such a bad idea. 目前,master分支所使用的加载器不支持它,因为这是一个坏主意。 It is supported by the geezer branch, but again, I strongly advise you to never use this pattern. 由奇特的分支支持,但同样,我强烈建议你从来没有使用这种模式。

The second module definition you have provided is just wrong; 您提供的第二个模块定义是错误的。 you need to put 'require' in your dependencies array if you expect to load the special require function. 如果您希望加载特殊的require函数,则'require'在依赖项数组中放入'require' You also can't use a variable to pass a module ID to the require function if you are expecting it to be pre-loaded as a dependency. 如果您希望将变量作为依赖进行预加载,则也不能使用变量将模块ID传递给require函数。 So, it should look like this: 因此,它应如下所示:

define(['require', 'foo'], function (require) {
    var foo = require('foo');
});

or, using the CommonJS compatibility wrapper syntax, where require is implicitly provided: 或者,使用CommonJS兼容性包装器语法,隐式提供了require

define(function (require) {
    var r = require('foo');
});

EDIT: Also, now that you have added the test module: within your test's define , '/src/api/nameApi' is not a valid AMD module identifier . 编辑:此外,现在您已经添加了测试模块:在测试的define ,'/ src / api / nameApi'不是有效的AMD模块标识符

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

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