Introduction
Writing test cases helps check if your code works correctly. It finds mistakes early so your program runs smoothly.
Writing test cases helps check if your code works correctly. It finds mistakes early so your program runs smoothly.
import assert from 'assert'; describe('Feature or function name', () => { it('should do something expected', () => { const result = functionToTest(); assert.strictEqual(result, expectedValue); }); });
describe groups related tests.
it defines a single test case with a clear goal.
import assert from 'assert'; describe('Addition function', () => { it('adds two numbers correctly', () => { const sum = 2 + 3; assert.strictEqual(sum, 5); }); });
import assert from 'assert'; function greet(name) { return `Hello, ${name}!`; } describe('Greet function', () => { it('returns greeting with name', () => { assert.strictEqual(greet('Alice'), 'Hello, Alice!'); }); });
This test suite checks the multiply function with different inputs: positive, zero, and negative numbers.
import assert from 'assert'; function multiply(a, b) { return a * b; } describe('multiply function', () => { it('multiplies positive numbers', () => { assert.strictEqual(multiply(3, 4), 12); }); it('multiplies by zero', () => { assert.strictEqual(multiply(5, 0), 0); }); it('multiplies negative numbers', () => { assert.strictEqual(multiply(-2, 3), -6); }); });
Use clear test names to understand what each test checks.
Run tests often to catch errors early.
Keep tests small and focused on one thing.
Test cases check if code works as expected.
Use describe and it to organize tests.
Write simple tests for different input cases.
describe to group tests and it to define individual test cases.describe('add function', () => { it('adds numbers', () => { /* test code */ }) }) correctly uses describe and it with arrow functions.describe('sum function', () => {
it('adds 2 and 3', () => {
expect(2 + 3).toBe(5);
});
it('adds 2 and 2', () => {
expect(2 + 2).toBe(5);
});
});describe('multiply function', () => {
it('multiplies 2 and 3', () => {
const result = 2 * 3
expect(result).to.equal(6);
}
});it function is missing a closing parenthesis after the arrow function block.to.equal is valid in Chai, and arrow functions are allowed in Mocha tests.filterPositive correctly returns only positive numbers from an array. Which test case correctly applies this in Jest?toEqual compares array contents deeply, while toBe checks reference equality, which fails for arrays.it('filters positive numbers', () => { expect(filterPositive([-1,0,2,3])).toEqual([2,3]); }); uses toEqual correctly to check array contents. it('filters positive numbers', () => { expect(filterPositive([-1,0,2,3])).toBe([2,3]); }); uses toBe which fails for arrays. it('filters positive numbers', () => { expect(filterPositive([-1,0,2,3])).toContain(2,3); }); uses toContain incorrectly with multiple values. it('filters positive numbers', () => { expect(filterPositive([-1,0,2,3])).toBeTruthy(); }); only checks truthiness, not exact output.Pass the test and this pattern moves unknown