最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Running Mocha on the command line and Including a file - Stack Overflow

programmeradmin2浏览0评论

I’m trying to setup some JS Unit tests using Mocha, and Ideally, I'd like to run this via the mand 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 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 /**/
(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!

I’m trying to setup some JS Unit tests using Mocha, and Ideally, I'd like to run this via the mand 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/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!

Share Improve this question asked Nov 19, 2013 at 23:29 whombawhomba 3776 silver badges13 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

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;

You can always specify files at runtime

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

mocha -r ../src/cow.js  yourcode.js
发布评论

评论列表(0)

  1. 暂无评论