Reputation: 59336
I'm getting started with JS testing, and I decided to go with Mocha.
The modules I want to test are AMD/RequireJS. However, it seems that Mocha is only compatible with CommonJS modules. So, when I run it, define is not defined.
So I've seen this question that suggests this document.
If this is really the way to go, I'd define my modules like this:
if (typeof define !== 'function') {
var define = require('amdefine')(module);
}
define(function(require) {
var dep = require('dependency');
//The value returned from the function is
//used as the module export visible to Node.
return function () {};
});
But now the amddefine
module is not defined when I run Mocha. I'm not used to Node.js, so my question is: Is this the recommended way to test AMD modules with Mocha? If so, how do I define the amdefine
in my Mocha tests?
Upvotes: 3
Views: 2536
Reputation: 151380
For what you are trying to do to work, you have to install the amdefine
package:
npm install amdefine
If you don't like amdefine
or you don't want to put the snippet it requires in all your modules, I would recommend that you just use this loader. You do:
npm install amd-loader
and before you try loading any AMD module you do:
require("amd-loader");
This require
call could be the first thing in your Mocha test file, for instance. This installs a loader that is able to understand the AMD format. I've used this with dozens of tests without any issue. And I prefer this to putting the code snippet that amdefine
requires in all my modules.
Upvotes: 6