سؤال

Just starting out with mocha and cannot for the life of me figure out why it thinks Helper is undefined at the indicated line/columns below:

test.js

var assert = require('assert'),
    helper = require('../src/js/helper.js');
describe('helper', function() {
    describe('#parseValue', function() {
        it('should return number of minutes for a properly formatted string', function() {
            assert.equal(1501, (new Helper()).parseValue('1d 1h 1m', 'when'));
                                ^^^^^^^^^^^^
        });
    });
});

helper.js

(function(exports) {

    'use strict';

    function Helper(opts) {

        this.opts = opts || {};

        /**
         *  Parse a value based on its type and return a sortable version of the original value
         *
         *  @param      {string}    val     input value
         *  @param      {string}    type    type of input value
         *  @returns    {mixed}     sortable value corresponding to the input value
         */
        this.parseValue = function(val, type) {

            switch (type) {

                case 'when':
                    var d = val.match(/\d+(?=d)/),
                        h = val.match(/\d+(?=h)/),
                        m = val.match(/\d+(?=m)/);
                    if (m)
                        m = parseInt(m, 10);
                    if (h)
                        m += parseInt(h, 10) * 60;
                    if (d)
                        m += parseInt(d, 10) * 1440;
                    val = m;
                    break;

                default:
                    break;

            }

            return val;

        };

    }

    exports.helper = Helper;

})(this);

I wrote a quick test in the browser without mocha to ensure my helper.js functions were accessible and it worked fine, so I really am at a loss. I am running this directly on my server by calling mocha from the command line in my directory.

هل كانت مفيدة؟

المحلول

You never define Helper in test.js—only helper on this line:

var helper = require('../src/js/helper.js');

Use the lower case helper that you defined.


By the way, you might want to change your exports line in helper.js from this:

exports.helper = Helper;

To this:

exports.Helper = Helper;

Then use helper in test.js like so:

assert.equal(1501, (new helper.Helper()).parseValue('1d 1h 1m', 'when'));

Or just do something like this:

var Helper = require('../src/js/helper.js').Helper;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top