How to write and run tests in Node.js applications? (project-based approach).

Jude Tech
3 min readJan 17, 2024

--

Project: Building a Node.js Task Manager: Getting Started.

Introduction:

Before we jump into creating our Node.js task manager and testing it, let’s make sure our computer is ready for action. Here’s what you need:\

Hardware Requirements:

You don’t need a supercomputer for this. Any computer that can run Node.js will do the trick. Laptops, desktops, it’s all good!

Now that our computer is all set, let’s build a task manager and make sure it works by testing it. No rocket science, just coding and testing!

Software Requirements:

  1. Node.js:
  • Install Node.js from here. This will also give you npm, which is a tool to manage packages (like apps for your project).

2. Code Editor:

  • Choose a code editor, like Visual Studio Code. It’s free and works well for Node.js development.

Project Setup:

Now, let’s set up our project. Open your terminal and run these commands:

# Create a new project folder
mkdir nodejs-task-manager

# Go into the project folder
cd nodejs-task-manager

# Start a new Node.js project
npm init -y

# Install testing tools
npm install --save-dev mocha chai

Edit your package.json file to include a test script:

"scripts": {
"test": "mocha"
}

Implementing the Task Manager Module:

Create a new file for your task manager module:

// taskManager.js

class TaskManager {
constructor() {
this.tasks = [];
}

addTask(task) {
this.tasks.push(task);
}

getTaskCount() {
return this.tasks.length;
}
}

module.exports = TaskManager;

Writing Your First Test:

Now, let’s create a test suite for the task manager module. Create a file named taskManager.test.js:

// taskManager.test.js

const chai = require('chai');
const TaskManager = require('./taskManager');

const { expect } = chai;

describe('Task Manager', () => {
it('should add a task to the task manager', () => {
const taskManager = new TaskManager();
taskManager.addTask('Complete article on testing');

expect(taskManager.getTaskCount()).to.equal(1);
});
});

Running Your Tests:

Execute the following command to run your tests:

npm test

Your test should pass, confirming the functionality of your task manager module.

Advanced Testing Techniques:

Asynchronous Testing:

Enhance your testing skills by handling asynchronous operations. Modify your test to include an asynchronous task:

it('should handle an asynchronous task', (done) => {
const taskManager = new TaskManager();

// Simulate an asynchronous task, e.g., fetching tasks from an API
setTimeout(() => {
taskManager.addTask('Fetch tasks from API');
expect(taskManager.getTaskCount()).to.equal(1);
done();
}, 1000);
});

Mocking and Stubbing:

Explore more advanced testing techniques by incorporating Sinon.js for mocking and stubbing. Mock an external API call in your application:

const sinon = require('sinon');

it('should mock an external API call', () => {
const apiStub = sinon.stub().resolves(apiResponse);

// Call your function that makes the external API call using apiStub

return expect(apiStub).to.have.been.calledOnceWith(expectedParams);
});

Conclusion:

Congratulations on completing a project-based guide to testing in Node.js! By building and testing a simple task manager application, you’ve gained valuable insights into the testing process. As you continue developing Node.js applications, applying these testing principles will contribute to the reliability and maintainability of your code.

Happy coding and testing!

Thanks for reading

--

--

Jude Tech

IT professional || AWS Technology Architecting || Oracle Cloud Infrastructure Certified Associate||Cybersecurity for Businesses EC-Council || Technical Writer.