Full-Stack Web Development with Vue.js and Node

(singke) #1
Testing an MEVN Application Chapter 9

Let's go ahead and write just enough code to pass the test. Create a file called add.js in the


root of the test_js project and run the code again, which should pass:


Let's go ahead and add the logic to the test code to check our add function. In


add.spec.js, add the following lines of code:


var addUtility = require('./../add.js');

describe('Add', function(){
describe('addUtility', function(){
it('should have a sum method', function(){
assert.equal(typeof addUtility, 'object');
assert.equal(typeof addUtility.sum, 'function');
})
})
});

Now comes the assert library. The assert library helps to check whether the passed


expression is right or wrong. Here, we will use the built-in assertion library for Node.js.


To include the assert library, let's add the following lines of code in add.spec.js:


var assert = require("assert")
var addUtility = require("./../add.js");

describe('Add', function(){
describe('addUtility', function(){
it('should have a sum method', function(){
assert.equal(typeof addUtility, 'object');
assert.equal(typeof addUtility.sum, 'function');
})
})
});
Free download pdf