简体   繁体   English

使用Browserify时无法加载模块进行实习测试

[英]Can't load modules for Intern testing while using Browserify

I am trying to use Intern to unit test one of my modules, tested.js , which has a dependency on another module, dependency.js . 我正在尝试使用Intern来测试我的一个模块tested.js ,它依赖于另一个模块dependency.js Because I am using Browserify, tested.js includes the line: 因为我使用的是Browserify, tested.js包含以下内容:

var dep = require('./lib/dependency.js');

Intern throws the following error: 实习生抛出以下错误:

Warning: Error: Attempt to require unloaded module lib/dependency.js

This is the start of my test file: 这是我的测试文件的开头:

define([

    'intern!object',

    'intern/chai!assert',

    'src/js/tested'

], function (registerSuite, assert, tested) {

    registerSuite({
            // ... 

My Intern config file uses the default Dojo loader. 我的实习配置文件使用默认的Dojo加载器。 I have tried using RequireJS instead but couldn't make it work properly (it seems that Intern has some ongoing issues with using alternative AMD loaders: https://github.com/theintern/intern/issues/147 , https://github.com/theintern/intern/pull/132#issuecomment-33403157 ). 我尝试使用RequireJS,但无法使其正常工作(似乎实习生在使用替代AMD加载器方面存在一些问题: https: //github.com/theintern/intern/issues/147,https :// github .com / theintern / intern / pull / 132#issuecomment-33403157 )。

How can I get Intern to properly load the required dependency? 如何让Intern正确加载所需的依赖项?

EDIT: I'm using grunt-browserify, but I'm unit-testing the un-Browserified module- I only mentioned Browserify to explain why I am using "require". 编辑:我正在使用grunt-browserify,但我正在测试非浏览器模块 - 我只提到了Browserify来解释我使用“require”的原因。

You can't load CJS/Node.js modules in a browser without running them in Browserify; 您无法在浏览器中加载CJS / Node.js模块而无需在Browserify中运行它们。 that is the entire purpose of Browserify. 这就是Browserify的全部目的。 You need to pre-Browserify the modules, switch to using AMD modules instead of CJS modules for your application, or introduce a proxy that converts the modules on the fly. 您需要对模块进行预浏览,切换到使用AMD模块而不是为您的应用程序使用CJS模块,或者引入代理来动态转换模块。

If you are trying to load CJS modules in Node.js through Intern, you need to use the intern/dojo/node module to facilitate that load: 如果您尝试通过Intern在Node.js中加载CJS模块,则需要使用intern/dojo/node模块来促进该加载:

define([

    'intern!object',

    'intern/chai!assert',

    'intern/dojo/node!src/js/tested'

], function (registerSuite, assert, tested) {

    registerSuite({
            // ... 

More information about this in the Testing Non-AMD code section of the documentation. 有关此内容的更多信息,请参阅文档的“ 测试非AMD代码”部分。

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

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