1

I am new to unit testing and I am trying to understand how to unit test node.js project but I am unsure why my current test is not working.

I have 2 files fed.js (src) and fed.spec.js (spec)

This is the error I am getting when I run this...

Error:

Snap should return 142
Expected undefined to be 142.
Error: Expected undefined to be 142.

The Jist:

I am trying to run a test to ensure that the variable AllowanceAdditional (located in fed.js) is equal to 142.

So I ran a jasmine unit test (located in fed.spec.js) and stated that I expected AllowanceAdditional to equal (==) or to be (===) 142. But The error I get returned is that AllowanceAdditional is undefined...

Can someone educate me or point me in the right direction, I don't understand why this is not working. What am I doing wrong? I hope my question makes sense, if not please tell me and I will further clarify

1
  • Sorry, added the additional information. I am expecting the variable AllowanceAdditional to return 142, but it says it returns "undefined" Commented Sep 2, 2014 at 0:11

1 Answer 1

1

You must export it:

exports.AllowanceAdditional = 142;

In JavaScript, there are other ways to express this.

In my practice, I typically use:

function () {
    this.AllowanceAdditional = 142;
}.call(this);

The point is to attach your global variables and functions to the to the proper scope so Node.js can find them.

Inside the browser, since .call(this) will always pass in the window object. It gets attached to the global scope.

Sign up to request clarification or add additional context in comments.

3 Comments

I am exporting it, I'm using module.exports = snap to export the whole snap object. My question is really more about how to make my test pass?
Do you mean I should add thie exports.AllowanceAdditional = 142; in the src file or the spec file?
The src file, node needs to be able to find it

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.