Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

node.js
javascript
testing

How to Test Node.js functions in the console

Educative Answers Team

Writing tests

Module tests can be written to test certain Node.js functions:

// Defining function to be tested
function grade(marks) {
  if (marks >= 90){
    return "A"
  }
  else if (marks >= 80){
    return "B"
  }
  else if (marks >= 70){
    return "C"
  }
  return "F"
}
// Calling function
test0 = grade(95)
test1 = grade(90)
test2 = grade(80)
test3 = grade(70)
test4 = grade(69)

// Displaying test results
console.log('grade(95) = A: ' + (test0 === 'A'))
console.log('grade(90) = A: ' + (test1 === 'A'))
console.log('grade(80) = B: ' + (test2 === 'B'))
console.log('grade(70) = C: ' + (test3 === 'C'))
console.log('grade(69) = F: ' + (test4 === 'F'))

This example tests the grade() function. The grade() function takes marks as an integer and returns the corresponding grade. To test the return values, the expected results of normal cases and border cases are compared with actual results for equivalence.

Normal cases are situations where parameters are comfortably within the bounds. The only normal case being tested here is grade(95).

Border cases are situations where the parameters are on the edge of giving different results. Other than the one normal case mentioned above, all tests are border cases. Take grade(80), for example, it’s on the border of a B and a C but is expected to return a B.

Running Node.js in a console

To run Node.js in a terminal, the following command is used:

Node <filename>

Here, <filename> is the name of the Node.js file. If the filename was grade.js, the command would look like:

Node grade.js

As seen in the example, the file extension, .js, is also expected in the console command.

Automated testing

Another way to test functions is through specialized testing libraries. In Node.js, there is a simple, fast testing library called Mocha.

To install Mocha globally, the following script is used:

npm i --global mocha

Mocha has an expectation library called Chai. To install it, use the following script:

npm install --save mocha chai

After installing Chai, some changes need to be made to the code. Also, the test file should be inside a directory called test that inside the root folder:

var expect    = require("chai").expect;
// Defining function to be tested
function grade(marks) {
  if (marks >= 90){
    return "A"
  }
  else if (marks >= 80){
    return "B"
  }
  else if (marks >= 70){
    return "C"
  }
  return "F"
}
// describe() describes what the test is testing
describe("Grade function", function() {
    describe("Testing A", function(){
        // it describes what is expected of the function
        it("Returns an A grade", function(){
            // Running the function
            test0 = grade(95)
            test1 = grade(90)
            // Comparing the results to expectations
            expect(test0).to.equal("A");
            expect(test1).to.equal("A");
        })
    })
    describe("Testing B", function(){
        it("Returns an B grade", function(){
            test2 = grade(80)

            expect(test0).to.equal("B");
        })
    })
    describe("Testing C", function(){
        it("Returns an C grade", function(){
            test0 = grade(70)

            expect(test0).to.equal("C");
        })
    })
    describe("Testing F", function(){
        it("Returns an F grade", function(){
            test0 = grade(69)

            expect(test0).to.equal("F");
        })
    })
})

This example demonstrates how Mocha is used. The describe() function uses its first argument to describe the code block or lambda function in its second argument. Here, we described that the Grade function is being tested and a nested describe function described that we were testing the A condition. it() functions describe what is expected from the tests being run inside it. Its parameters are similar to those of describe(), but the actual test is run inside the it() function. Comparisons of the results are made with the expectations using the expect() function. Here, simple equivalence checks were used with expect() functions.

The test can be run using the following script in the console:

npm test

Once you follow this command, the results of the test will be displayed on the terminal.

RELATED TAGS

node.js
javascript
testing
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring