Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top