3

I’m trying to setup some JS Unit tests using Mocha, and Ideally, I'd like to run this via the command line oppose to a web page. (TL:DR; at the bottom)

First I did some bullshit test to confirm that Array worked as expected which I pulled directly from Mocha's page http://visionmedia.github.io/mocha/#getting-started and this worked as expected.

At this point, to up the stakes I then created a new file /src/cow.js :

/** This example is taken from https://nicolas.perriault.net/code/2013/testing-frontend-javascript-code-using-mocha-chai-and-sinon/**/
(function(exports) {
    "use strict";

    function Cow(name) {
        this.name = name || "Anon cow";
    }
    exports.Cow = Cow;

    Cow.prototype = {
        greets: function(target) {
            if (!target)
                throw new Error("missing target");
            return this.name + " greets " + target;
        }
    };
})(this);

along with my test file /test/test.js:

var chai = require("chai"),
    expect = chai.expect;

require( "../src/cow.js");

describe( "Cow", function(){

    describe( "constructor", function(){
        it( "should have a default name", function(){
            var cow = new Cow();
            expect( cow.name).to.equal( "Anon cow");
        });

        it( "show use provided name", function(){
            var name = "duck Cow",
                cow = new Cow( name );
            expect( cow.name).to.equal( name );
        })
    });
});

and ran 'mocha'. At which point I receive the following errors:

    1) Cow constructor should have a default name:
        ReferenceError: Cow is not defined
    at Context.<anonymous> (C:\Users\myUserName\\jsUnitTesting\helloWorld\test\test.js:10:27)
    at Test.Runnable.run (C:\Users\myUserName\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:211:32)
    at Runner.runTest (C:\Users\myUserName\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:358:10)
    at C:\Users\myUserName\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:404:12
    at next (C:\Users\myUserName\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:284:14)
    at C:\Users\myUserName\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:293:7
    at next (C:\Users\myUserName\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:237:23)
    at Object._onImmediate (C:\Users\myUserName\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:261:5)
    at processImmediate [as _immediateCallback] (timers.js:330:15)

    2) Cow constructor show use provided name:
        ReferenceError: Cow is not defined
    at Context.<anonymous> (C:\Users\myUserName\\jsUnitTesting\helloWorld\test\test.js:16:27)
    at Test.Runnable.run (C:\Users\myUserName\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:211:32)
    at Runner.runTest (C:\Users\myUserName\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:358:10)
    at C:\Users\myUserName\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:404:12
    at next (C:\Users\myUserName\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:284:14)
    at C:\Users\myUserName\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:293:7
    at next (C:\Users\myUserName\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:237:23)
    at Object._onImmediate (C:\Users\myUserName\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:261:5)
    at processImmediate [as _immediateCallback] (timers.js:330:15)

This is obviously less than ideal. I figure this is because test.js has no idea about cow.js. As you can see I've used the require, however this does not seem to work.

TL:DR;

So, what is the proper way to add this file (cow.js) to my test file (test.js)? Currently, I'm not using any libs aside from just mocha and nodeJS

Thanks!

whomba
  • 387
  • 5
  • 13

2 Answers2

6

The only thing missing is making a reference to the Cow export from the require().

You'll just have to change this line:

require( "../src/cow.js" );

To this:

var Cow = require( "../src/cow.js" ).Cow;
quixote
  • 116
  • 2
3

You can always specify files at runtime

-r, --require <name>            require the given module

mocha -r ../src/cow.js  yourcode.js
Alice
  • 3,958
  • 2
  • 24
  • 28
sachin.ph
  • 1,078
  • 12
  • 24