How do I write unit tests in JavaScript

Unit testing in JavaScript is a crucial part of the software development process that ensures each part of your application is working correctly. A unit test is a way to test individual pieces of your code (often functions) to verify that they behave as expected. Below is a basic example of how to write unit tests in JavaScript using the Jest framework.

// First, install Jest by running: npm install --save-dev jest // Example function to be tested function add(a, b) { return a + b; } // Example unit test test('adds 1 + 2 to equal 3', () => { expect(add(1, 2)).toBe(3); }); // To run the tests, add the following script to your package.json: // "scripts": { // "test": "jest" // } // Then run: npm test

unit tests JavaScript testing Jest framework test-driven development