Get ready for your next interview with our comprehensive question library
Jest can be installed via npm or yarn:
npm install --save-dev jest
# or
yarn add --dev jest
Basic setup in package.json:
{
"scripts": {
"test": "jest",
"test:watch": "jest --watch",
"test:coverage": "jest --coverage"
}
}
For projects without Babel, you might need additional configuration for ES6+ syntax.
There is no functional difference between test() and it() - they are aliases of each other. Both are used to define individual test cases:
test('should add two numbers', () => {
expect(add(2, 3)).toBe(5);
});
it('should add two numbers', () => {
expect(add(2, 3)).toBe(5);
});
The choice between them is often stylistic. it() reads more naturally in BDD (Behavior Driven Development) style, while test() is more explicit about its purpose.
describe() is used to group related tests together. It creates a test suite and helps organize tests logically:
describe('Calculator', () => {
test('should add two numbers', () => {
expect(add(2, 3)).toBe(5);
});
test('should subtract two numbers', () => {
expect(subtract(5, 3)).toBe(2);
});
});
Benefits of using describe():
A well-structured Jest test file typically follows this pattern:
// Import dependencies
import { functionToTest } from '../src/utils';
// Describe the module/component being tested
describe('functionToTest', () => {
// Group related tests
describe('when given valid input', () => {
test('should return expected result', () => {
// Arrange
const input = 'test';
// Act
const result = functionToTest(input);
// Assert
expect(result).toBe('expected');
});
});
describe('when given invalid input', () => {
test('should throw an error', () => {
expect(() => functionToTest(null)).toThrow();
});
});
});
Jest can be run in several ways:
npm test or jestjest --watch (watches for file changes)jest --watchAll (watches all files)jest --coverage (generates coverage report)jest mytest.test.jsjest --testNamePattern="should add"jest --verboseCommon package.json scripts:
{
"scripts": {
"test": "jest",
"test:watch": "jest --watch",
"test:coverage": "jest --coverage",
"test:debug": "jest --verbose"
}
}
Jest provides many built-in matchers for different assertion types:
Equality matchers:
toBe(): Exact equality (Object.is)toEqual(): Deep equalitytoStrictEqual(): Strict deep equalityTruthiness matchers:
toBeTruthy(): Truthy valuestoBeFalsy(): Falsy valuestoBeNull(): Specifically nulltoBeUndefined(): Specifically undefinedNumber matchers:
toBeGreaterThan(): Greater thantoBeCloseTo(): Floating point numbersString matchers:
toMatch(): Regular expressionstoContain(): Substring matchingArray/Object matchers:
toContain(): Array contains itemtoHaveProperty(): Object has propertytoBe() uses Object.is() for exact equality (reference equality for objects), while toEqual() performs deep equality checking:
test('toBe vs toEqual', () => {
const obj1 = { name: 'John' };
const obj2 = { name: 'John' };
const obj3 = obj1;
// toBe checks reference equality
expect(obj1).toBe(obj3); // ✓ Same reference
expect(obj1).toBe(obj2); // ✗ Different references
// toEqual checks deep equality
expect(obj1).toEqual(obj2); // ✓ Same content
expect(obj1).toEqual(obj3); // ✓ Same content
// For primitives, they work similarly
expect(5).toBe(5); // ✓
expect(5).toEqual(5); // ✓
});
Upgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumAccess all premium content - interview questions, and other learning resources
We regularly update our features and content, to ensure you get the most relevant and updated premium content.
1000 monthly credits
Cancel anytime