0

I am running unit tests on a javascript class using Mocha using the follow methodology, firstly the test:

var base = require('../moduleone.js');

describe("some test", function() {
    it("description", function() {
    var check = base.toBeTested(dummyValue)
    //test is here ...
    });
});

the moduleone.js containing function to be tested:

function toBeTested(category){
    //below I calling an assert function defined in moduletwo
    //works fine when running in browser
    assert(type(category)=='string','category is string type');
    //more code..
    return something

module.exports.toBeTested = toBeTested;

moduletwo.js:

function assert(outcome, description) {
    //see code.tutsplus.com quick and easy javascript testing with assert
    var li = outcome ? 'pass' : 'fail';
    if (li == 'fail') {
        console.log('FAIL: '+description);
    }
    else {
        console.log('PASS: '+description);
    }
}

The issue I have is mocha doesn't know anything about moduletwo and when the moduleone function calles the function in moduletwo mocha throws a ReferenceError: assert is not defined. How can I link all my dependencies so that mocha can see them?

1 Answer 1

1

In your moduleone.js be sure that you are requireing moduletwo.js to bring your assert function into scope for moduleone.js. Otherwise, you get a ReferenceError, not for any reasons with mocha, but because moduleone does not have access to assert.

// moduletwo.js
function assert(outcome, description) { /* your functionality */ }

module.exports = assert


// moduleone.js
var assert = require('./moduletwo')

function toBeTested(category) { /* your functionality that uses assert */ }

module.exports.toBeTested = toBeTested

Now, with regards to that tutorial. If you are following it to learn how to make an easy assert module, that is fine. But if you are trying to test some code that does something else, consider using an existing assertion library like chai. For example:

// example.test.js
var expect = require('chai').expect
var isValidCategory = require('./is-valid-category')

describe('isValidCategory(category)', function () {

  it('validates string categories', function () {

    expect(isValidCategory('A String Category')).to.be.true

  })

  it('does not validate non-string categories', function () {

    expect(isValidCategory(['an', 'array', 'somehow'])).to.be.false

  })

})

// is-valid-category.js
module.exports = function isValidCategory(category) {

  return typeof category === 'string'
}
Sign up to request clarification or add additional context in comments.

4 Comments

for the case given when I exported one function from a module your solution works perfectly. However I require tens of functions from several files so I exported each function from moduletwo eg module.exports.assert = assert; module.exports.type = type; then added to module1 the code: var utils = require('./moduletwo.js') and called each function using: utils.assert(utils.type(category)...); The problem is my app is client side only so exposing functions through require won't work. Of course I could add everything to one big file but is there another way?
Your requirement of having multiple properties on module.exports is ok. Check out browserify for bundling CommonJS modules for the client.
Great, note my use of 'assert' was to use java-like assertion in the client side code during development. For unit-test assertions I am using chai.
Ah, I see, I will have to keep that approach in mind. Kent C. Dodds has an interesting module called api-check that I have been meaning to look at.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.