Interview Questions

Get ready for your next interview with our comprehensive question library

Jest Interview Questions

Filter by Difficulty

1.

How do you install and set up Jest in a project?

beginner

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.

2.

What is the difference between `test()` and `it()` in Jest?

beginner

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.

3.

What is `describe()` and when should you use it?

beginner

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():

  • Better test organization and readability
  • Scoped setup and teardown hooks
  • Grouped test output in reports
  • Easier to run specific test suites
4.

How do you structure a typical Jest test file?

beginner

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();
    });
  });
});
5.

What are the different ways to run Jest tests?

beginner

Jest can be run in several ways:

  1. Basic test run: npm test or jest
  2. Watch mode: jest --watch (watches for file changes)
  3. Watch all: jest --watchAll (watches all files)
  4. Coverage: jest --coverage (generates coverage report)
  5. Specific file: jest mytest.test.js
  6. Pattern matching: jest --testNamePattern="should add"
  7. Verbose output: jest --verbose

Common package.json scripts:

{
  "scripts": {
    "test": "jest",
    "test:watch": "jest --watch",
    "test:coverage": "jest --coverage",
    "test:debug": "jest --verbose"
  }
}
6.

What are the most commonly used Jest matchers?

intermediate

Jest provides many built-in matchers for different assertion types:

Equality matchers:

  • toBe(): Exact equality (Object.is)
  • toEqual(): Deep equality
  • toStrictEqual(): Strict deep equality

Truthiness matchers:

  • toBeTruthy(): Truthy values
  • toBeFalsy(): Falsy values
  • toBeNull(): Specifically null
  • toBeUndefined(): Specifically undefined

Number matchers:

  • toBeGreaterThan(): Greater than
  • toBeCloseTo(): Floating point numbers

String matchers:

  • toMatch(): Regular expressions
  • toContain(): Substring matching

Array/Object matchers:

  • toContain(): Array contains item
  • toHaveProperty(): Object has property
7.

What's the difference between `toBe()` and `toEqual()`?

intermediate

toBe() 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); // ✓
});
8.

How do you test for exceptions in Jest?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
9.

How do you test arrays and objects with Jest?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
10.

What is mocking in Jest and why is it important?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
11.

How do you create and use mock functions in Jest?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
12.

What's the difference between `jest.mock()` and `jest.spyOn()`?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
13.

How do you mock modules and external dependencies?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
14.

How do you test asynchronous code in Jest?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
15.

How do you test setTimeout and setInterval functions?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
16.

What are Jest lifecycle hooks and when should you use them?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
17.

How do you share setup code across multiple test files?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
18.

How do you configure Jest for different environments?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
19.

What is the purpose of `setupFilesAfterEnv` in Jest configuration?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
20.

How do you test React components with Jest and React Testing Library?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
Showing 1 to 20 of 49 results

Premium Plan

$10.00 /monthly
  • Access 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