3

I am trying to use Intern to unit test one of my modules, tested.js, which has a dependency on another module, dependency.js. Because I am using Browserify, tested.js includes the line:

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. 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).

How can I get Intern to properly load the required dependency?

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".

fblundun
  • 987
  • 7
  • 19
  • +1 I've been having this issue too – Alex Dean Feb 18 '14 at 13:54
  • 1. What flags are you passing to Browserify? 2. Why do you think using RequireJS would do anything differently? 3. The issues you referenced do not affect anything you would use Browserified code with. – C Snover Feb 19 '14 at 01:51
  • @CSnover I've edited my question to hopefully make it a bit clearer. I'm not changing any of grunt-browserify's default options. I tried using RequireJS to see if it helped since the error seemed to involve loading modules. – fblundun Feb 19 '14 at 11:19

1 Answers1

4

You can’t load CJS/Node.js modules in a browser without running them in Browserify; that is the entire purpose of 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.

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:

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.

C Snover
  • 17,908
  • 5
  • 29
  • 39
  • The `intern/dojo/node` module was what I needed, thanks! Do you know if it works with dojo's alias configuration option? I currently have `loader: {aliases: [['lib/dependency: 'dependency]]}` in my `intern.js` file but the alias isn't being recognised. – fblundun Feb 20 '14 at 14:47
  • Use `map`, not `aliases`, if you are trying to remap modules. `map` is part of the [AMD Common Config](https://github.com/amdjs/amdjs-api/wiki/Common-Config); `aliases` is confusing and non-standard. – C Snover Feb 20 '14 at 15:12
  • I've changed to `loader: {map: {'src/js/tested: {'dependency': 'lib/dependency'}}}`, but still no joy. – fblundun Feb 20 '14 at 16:10
  • It’s only possible to remap AMD modules. [Node.js doesn’t do mapping](https://stackoverflow.com/questions/16762322/remap-local-paths-for-require-in-node-js). – C Snover Feb 20 '14 at 16:15
  • Here is a [successful Intern/Browserify integration](https://github.com/mightyiam/edited). – Shahar 'Dawn' Or Feb 13 '15 at 16:05