Unit Testing Via Jest in Node.js

INTRO IMAGE JEST

Introduction to Jest

Jest is a powerful testing framework developed by Facebook. It provides a simple and intuitive API, a built-in test runner, code coverage reports, and mocking capabilities. Jest is widely used in the React ecosystem but can be used to test any JavaScript code.

Advantages of Jest:

  • Simplicity: Offers a straightforward API for writing and running tests.
  • Built-in Features: Includes a built-in test runner, code coverage reports, and mocking capabilities.
  • Popular in React: Widely used in the React ecosystem.

How to Setup Jest in Node.js Application

To set up Jest in your Node.js application for writing unit tests, follow these steps:

1. Installation

Install Jest as the dev-dependency in your Node.js application by running the following command

npm install - save-dev jest

2. Jest Configuration:

Create a jest.config.js for basic Jest configuration.


module.exports = {
testEnvironment: 'node',
testMatch: ['**/__tests__/**/*.js', '**/tests/**/*.js'],
moduleFileExtensions: ['js', 'jsx', 'ts', 'tsx'],
coverageThreshold: {
global: {
branches: 80,
functions: 80,
lines: 80,
statements: 80,
},
},
};

3. Write a Test Case

Create a test file (e.g., <file_name>.test.js) for each module you want to test, and replace file_name with your file name.


const sum = require('./sum');

test('adds 1 + 2 to equal 3', () => {
expect(sum.add(1, 2)).toBe(3);
});

4. Run Test

Run your test by running npx jest or writing a script package.json and run npm test

// package.json
"scripts": {
"test": "jest"
}

Understanding Jest Configuration

  • testEnvironment: 'node': This specifies that the tests will be run in a Node.js environment. This is crucial for testing Node.js specific code.
  • testMatch: ['**/__tests__/**/*.js', '**/tests/**/*.js']: This option defines the pattern for locating test files. Jest will search for files matching these patterns in your project directory.
  • moduleFileExtensions: ['js', 'jsx', 'ts', 'tsx']: This option tells Jest which file extensions to look for when resolving modules. It's useful for projects using TypeScript or JSX.
  • coverageThreshold: { global: { ... } }: This option sets the minimum coverage thresholds for your tests. It ensures that a certain percentage of your code is covered by tests. The specific thresholds are:

Conclusion

In conclusion, by setting up Jest in your project, you can write and execute unit tests efficiently. Jest’s powerful features β€” such as its easy setup, built-in mocking capabilities, and test coverage reporting β€” make it an excellent choice for ensuring code reliability and catching bugs early in the development process.

Through consistent unit testing, you can improve code quality, reduce the risk of regressions, and build a more maintainable and stable application.

Queries and Doubts

Thanks for the read :) Hope you have enjoyed reading it 🀩 and learnt something new today.

If you have any doubts or queries feel free to drop a comment or

⁍ Connect with me on my πŸ”—Topmate πŸ’¬.

⁍ You can also reach out to me on my πŸ”—LinkedIn.

⁍ Please clap for this post if you enjoyed reading it πŸ“— and follow for more interesting articles.

⁍ You can also support me and my writings by treating me to a nice virtual cup of coffee β˜•οΈ.