简体   繁体   中英

Unit Testing node.js using jasmine unit testing framework

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM