-1

With Node.js installed and testing with mocha,

I have two files, numbers.js and test.js in the same directory

Following the top answer: What is the purpose of Node.js module.exports and how do you use it?

numbers.js

var myFunc = function myFunc() {
      return 10;
};

exports.myFunc = myFunc;

test.js

var assert = require('assert');
var numbers = require('./numbers');

describe('numbers', function() {
    it('first function returns 10', function() {
            var result = numbers.myFunc;
            assert.equal(result, 10);
     });
});

But when running $ mocha it returns the error:

AssertionError: [Function: myFunc] == 10

What am I doing wrong?

Community
  • 1
  • 1
Jimmie
  • 397
  • 2
  • 5
  • 20

2 Answers2

2

You need to call your myFunc function. Add the parens

describe('numbers', function() {
    it('first function returns 10', function() {
                  var result = numbers.myFunc(); <<<<<<

                      assert.equal(result, 10);
                        });
});
Matt
  • 4,462
  • 5
  • 25
  • 35
  • I didn't try that, but it did get me further! However I get a new assertion error It says `AssertionError: undefined == 1` – Jimmie Feb 16 '16 at 16:26
  • I see, I had removed the () after changing myfunc to myFunc and that caused another problem. – Jimmie Feb 16 '16 at 17:19
-1

First of all, Inspect your numbers module is properly required or not.

var numbers = require('../numbers'); // Check path of file numbers.js

If yes, write :

var result = numbers.myFunc(); // your module exports 'myFunc' property not 'myfunc'
assert.equal(result, 10); // your function return '10' instead of '1'
Piyush Sagar
  • 2,931
  • 23
  • 26
  • I updated the code, now I put numbers.js and test.js in the same place so I think its properly required, I also fixed numbers.myFunc. That helped and I got further, but a new problem. Mocha gives the error `AssertionError: [Function: myFunc] == 10` – Jimmie Feb 16 '16 at 17:12
  • Yes , It was my mistake, I forgot to add '()' after myFunc.Basically its a function so you need to call it. – Piyush Sagar Feb 17 '16 at 13:33