I am trying to understand how to develop stand-alone Javascript code. I want to write Javscript code with tests and modules, running from the command line. So I have installed node.js
and npm
along with the libraries requirejs
, underscore
, and mocha
.
My directory structure looks like this:
> tree .
.
├── node_modules
├── src
│ └── utils.js
└── test
└── utils.js
where src/utils.js
is a little module that I am writing, with the following code:
> cat src/utils.js
define(['underscore'], function () {
"use strict";
if ('function' !== typeof Object.beget) {
Object.beget = function (o) {
var f = function () {
};
f.prototype = o;
return new f();
};
}
});
and test/utils.js
is the test:
> cat test/utils.js
var requirejs = require('requirejs');
requirejs.config({nodeRequire: require});
requirejs(['../src/utils'], function(utils) {
suite('utils', function() {
test('should always work', function() {
assert.equal(1, 1);
})
})
});
which I then try to run from the top level directory (so mocha
sees the test
directory):
> mocha
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: Calling node's require("../src/utils") failed with error: ReferenceError: define is not defined
at /.../node_modules/requirejs/bin/r.js:2276:27
at Function.execCb (/.../node_modules/requirejs/bin/r.js:1872:25)
at execManager (/.../node_modules/requirejs/bin/r.js:541:31)
...
So my questions are:
- Is this the correct way to structure code?
- Why is my test not running?
- What is the best way to learn this kind of thing? I am having a hard time finding good examples with Google.
Thanks...
[sorry - momentarily posted results from wrong code; fixed now]
PS I am using requirejs because I also want to run this code (or some of it) from a browser, later.
Update / Solution
Something that is not in the answers below is that I needed to use mocha -u tdd
for the test style above. Here is the final test (which also requires assert) and its use:
> cat test/utils.js
var requirejs = require('requirejs');
requirejs.config({nodeRequire: require});
requirejs(['../src/utils', 'assert'], function(utils, assert) {
suite('utils', function() {
test('should always work', function() {
assert.equal(1, 1);
})
})
});
> mocha -u tdd
.
✔ 1 tests complete (1ms)
You are trying to run JS modules designed for browsers (AMD), but in the backend it might not work (as modules are loaded the commonjs way). Because of this, you will face two issues:
In the browser
define
will be defined. It will be set when you require something with requirejs. But nodejs loads modules the commonjs way.define
in this case is not defined. But it will be defined when we require with requirejs!This means that now we are requiring code asynchronously, and it brings the second problem, a problem with async execution. https://github.com/mochajs/mocha/issues/362
Here is a full working example. Look that I had to configure requirejs (amd) to load the modules, we are not using require (node/commonjs) to load our modules.
And for the module that you want to test:
Just in case David's answer was not clear enough, I just needed to add this:
To the top of the js file where I use
define
, as described in RequireJS docs ("Building node modules with AMD or RequireJS") and in the same folder add theamdefine
package:This creates the
node_modules
folder with theamdefine
module inside.I don't use
requirejs
so I'm not sure what that syntax looks like, but this is what I do to run code both withinnode
and thebrowser
:For imports, determine if we are running in node or the browser:
Then we can grab any dependencies correctly (they will either be available already if in the browser or we use
require
):And then any functionality that needs to be used by other files, we export it to root:
As for examples,
GitHub
is a great source. Just go and check out the code for your favorite library to see how they did it :) I usedmocha
to test a javascript library that can be used in both the browser and node. The code is available at https://github.com/bunkat/later.The reason your test isn't running is because
src/utils.js
is not a valid Node.js library.According to the RequireJS documentation, in order to co-exist with Node.js and the CommonJS require standard, you need to add a bit of boilerplate to the top of your
src/utils.js
file so RequireJS'sdefine
function is loaded.However, since RequireJS was designed to be able to require "classic" web browser-oriented source code, I tend to use the following pattern with my Node.js libraries that I also want running in the browser:
This has the advantage of not requiring an extra library for other Node.js users and generally works well with RequireJS on the client.
Once you get your code running in Node.js, you can start testing. I personally still prefer expresso over mocha, even though its the successor test framework.
The Mocha documentation is lacking on how to set this stuff up, and it's perplexing to figure out because of all the magic tricks it does under the hood.
I found the keys to getting browser files using require.js to work in Mocha under Node: Mocha has to have the files added to its suites with
addFile
:And second, use
beforeEach
with the optional callback to load your modules asynchronously:I created a bootstrap for how to get this all working: https://github.com/clubajax/mocha-bootstrap