JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionstesting
PrevNext

Learn the concept

Integration Tests vs Unit Tests

testing
mid
integration

What is the difference between unit tests and integration tests?

integration-testing
unit-testing
test-pyramid
msw
Quick Answer

Unit tests test individual functions/components in isolation with mocked dependencies. Integration tests verify multiple units work together correctly, testing real interactions between components, APIs, or databases with fewer mocks.

Detailed Explanation

Unit Tests:

  • Test one unit in isolation
  • Mock all dependencies
  • Fast execution
  • Pinpoint exact failures
  • Many tests needed

Integration Tests:

  • Test multiple units together
  • Real or realistic dependencies
  • Slower execution
  • Catch interaction bugs
  • Fewer tests needed

Testing Pyramid:

  • Many unit tests (base)
  • Some integration tests (middle)
  • Few E2E tests (top)

When to Use Each:

  • Unit: Business logic, pure functions
  • Integration: API routes, component trees
  • E2E: Critical user flows

Code Examples

Unit test (isolated)JavaScript
// Unit test - pure function, no dependencies
function calculateTotal(items) {
  return items.reduce((sum, item) => sum + item.price * item.quantity, 0);
}

describe('calculateTotal', () => {
  test('calculates sum of items', () => {
    const items = [
      { price: 10, quantity: 2 },
      { price: 5, quantity: 3 }
    ];
    expect(calculateTotal(items)).toBe(35);
  });

  test('returns 0 for empty array', () => {
    expect(calculateTotal([])).toBe(0);
  });
});

// Unit test - component with mocked hook
jest.mock('./useUser', () => ({
  useUser: () => ({ user: { name: 'Alice' }, loading: false })
}));

test('displays user name', () => {
  render(<UserProfile />);
  expect(screen.getByText('Alice')).toBeInTheDocument();
});

Real-World Applications

Use Cases

Verifying the end-to-end functionality of a user signup process involving a UI form, an API call, and database persistence

Writing an integration test that simulates user input in the UI, mocks only the external payment gateway, and then asserts that the API receives the correct payload and the user record is created in a test database.

Ensuring that a React component correctly interacts with a Redux store or a Context API for state management

Developing integration tests that render the component within a real Redux provider or Context provider, dispatch actions, and assert that the component's UI updates correctly based on the shared state.

Testing a complex data pipeline where multiple microservices interact to process and transform data

Creating integration tests that bring up a subset of the actual services, send a test message through the pipeline, and assert on the final processed output, ensuring the entire flow works as expected.

Mini Projects

React Component with API Integration Test

intermediate

Build a React component that fetches data from an API. Write an integration test using MSW (Mock Service Worker) to mock the API and verify that the component fetches and displays the data correctly.

User Profile Page Integration Test

intermediate

Create a user profile page with editable fields. Write an integration test that simulates editing the profile, saving it via an API call (mocked), and verifying the UI updates to reflect the changes.

Industry Examples

Amazon (AWS)

AWS services are often tested with extensive integration tests to ensure that different components (e.g., EC2, S3, Lambda) work seamlessly together. These tests simulate real-world scenarios across multiple services to guarantee platform reliability.

Google (Gmail)

Gmail, a massive web application, uses integration tests to verify that its various features like email composition, contact management, and calendar integration function correctly when combined, even if individual units work in isolation.

Resources

The Testing Trophy

article

MSW - Mock Service Worker

docs

Related Questions

How do you mock functions, modules, and API calls in Jest?

mid
mocking

What are E2E tests and when should you use Cypress vs Playwright?

senior
e2e
Previous
How do you test React components with React Testing Library?
Next
What is code coverage and how do you interpret coverage reports?
PrevNext