codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

Ā© 2026 codingstuff.io. All rights reserved.

Built with ā¤ļø for developers everywhere

/
/
All Tutorials
šŸ”·

TypeScript

51 / 60 topics
49Testing TypeScript Applications50Unit Testing51Integration Testing52End-to-End Testing
Tutorials/TypeScript/Integration Testing
šŸ”·TypeScript

Integration Testing

Updated 2026-04-20
3 min read

Introduction

Integration testing is a critical phase in software development that focuses on verifying the interaction between different modules or components of an application. In this tutorial, we will explore how to perform integration testing in TypeScript using popular testing frameworks like Jest and Mocha. We'll cover setting up test environments, writing tests, and best practices for effective integration testing.

Prerequisites

Before diving into integration testing, ensure you have the following:

  • Basic knowledge of TypeScript.
  • Node.js installed on your machine.
  • A code editor or IDE (e.g., Visual Studio Code).

Setting Up Your Project

To begin, let's set up a basic TypeScript project. If you already have a project, skip to the next section.

  1. Initialize a new Node.js project:

    mkdir integration-testing-ts
    cd integration-testing-ts
    npm init -y
    
  2. Install TypeScript and necessary dependencies:

    npm install typescript ts-node @types/node --save-dev
    
  3. Create a tsconfig.json file:

    {
      "compilerOptions": {
        "target": "ES6",
        "module": "commonjs",
        "strict": true,
        "esModuleInterop": true,
        "skipLibCheck": true,
        "forceConsistentCasingInFileNames": true
      },
      "include": ["src"]
    }
    
  4. Create the project structure:

    integration-testing-ts/
    ā”œā”€ā”€ src/
    │   └── index.ts
    ā”œā”€ā”€ test/
    │   └── index.test.ts
    ā”œā”€ā”€ tsconfig.json
    └── package.json
    
  5. Add a simple function in src/index.ts:

    export const add = (a: number, b: number): number => {
      return a + b;
    };
    

Choosing a Testing Framework

For this tutorial, we'll use Jest and Mocha as popular TypeScript testing frameworks. You can choose either based on your preference.

Using Jest

  1. Install Jest and its TypeScript types:

    npm install jest @types/jest ts-jest --save-dev
    
  2. Configure Jest:

    Create a jest.config.js file in the root of your project:

    module.exports = {
      preset: 'ts-jest',
      testEnvironment: 'node',
    };
    
  3. Write an integration test using Jest:

    In test/index.test.ts, write the following test:

    import { add } from '../src';
    
    describe('Integration Tests', () => {
      it('should correctly integrate two numbers', () => {
        const result = add(5, 3);
        expect(result).toBe(8);
      });
    });
    
  4. Run the tests:

    Add a script to your package.json:

    "scripts": {
      "test": "jest"
    }
    

    Run the tests using:

    npm test
    

Using Mocha

  1. Install Mocha, Chai, and their TypeScript types:

    npm install mocha chai @types/mocha @types/chai --save-dev
    
  2. Write an integration test using Mocha and Chai:

    In test/index.test.ts, write the following test:

    import { expect } from 'chai';
    import { add } from '../src';
    
    describe('Integration Tests', () => {
      it('should correctly integrate two numbers', () => {
        const result = add(5, 3);
        expect(result).to.equal(8);
      });
    });
    
  3. Run the tests:

    Add a script to your package.json:

    "scripts": {
      "test": "mocha --require ts-node/register test/**/*.ts"
    }
    

    Run the tests using:

    npm test
    

Writing Effective Integration Tests

Test Structure

A good integration test should follow a clear structure:

  1. Setup: Prepare any necessary data or state.
  2. Execution: Call the function or method under test.
  3. Assertion: Verify that the output matches the expected result.

Mocking Dependencies

In real-world applications, components often depend on external services or libraries. It's crucial to mock these dependencies to isolate the component being tested.

Using Jest for Mocking

import { add } from '../src';
import axios from 'axios';

jest.mock('axios');

describe('Integration Tests with Mocks', () => {
  it('should handle external API calls correctly', async () => {
    const mockData = { data: { result: 10 } };
    (axios.get as jest.Mock).mockResolvedValue(mockData);

    // Assume there's a function that uses axios
    const fetchData = async () => {
      const response = await axios.get('https://api.example.com/data');
      return add(response.data.result, 5);
    };

    const result = await fetchData();
    expect(result).toBe(15);
  });
});

Using Mocha for Mocking

import { expect } from 'chai';
import axios from 'axios';
import sinon from 'sinon';

describe('Integration Tests with Mocks', () => {
  let stub: sinon.SinonStub;

  beforeEach(() => {
    stub = sinon.stub(axios, 'get');
  });

  afterEach(() => {
    stub.restore();
  });

  it('should handle external API calls correctly', async () => {
    const mockData = { data: { result: 10 } };
    stub.resolves(mockData);

    // Assume there's a function that uses axios
    const fetchData = async () => {
      const response = await axios.get('https://api.example.com/data');
      return add(response.data.result, 5);
    };

    const result = await fetchData();
    expect(result).to.equal(15);
  });
});

Test Coverage

Ensure your tests cover all critical paths and edge cases. Use tools like Istanbul (for Jest) or NYC (for Mocha) to measure test coverage.

Using Istanbul with Jest

Install Istanbul:

npm install --save-dev istanbul

Modify the test script in package.json:

"scripts": {
  "test": "jest --coverage"
}

Run tests with coverage:

npm test

Using NYC with Mocha

Install NYC:

npm install --save-dev nyc

Modify the test script in package.json:

"scripts": {
  "test": "nyc mocha --require ts-node/register test/**/*.ts"
}

Run tests with coverage:

npm test

Best Practices

  1. Keep Tests Independent: Each test should be independent of others and not rely on shared state.
  2. Use Descriptive Test Names: Clearly describe what each test is verifying.
  3. Avoid Over-Mocking: Only mock dependencies that are necessary to isolate the component being tested.
  4. Regularly Update Tests: Keep your tests up-to-date with changes in the codebase.
  5. Automate Testing: Integrate testing into your CI/CD pipeline for continuous feedback.

Conclusion

Integration testing is essential for ensuring that different parts of your application work together seamlessly. By following this guide, you should be able to set up and perform integration tests in TypeScript using Jest or Mocha. Remember to write effective tests, mock dependencies when necessary, and maintain high test coverage to build robust applications.


PreviousUnit TestingNext End-to-End Testing

Recommended Gear

Unit TestingEnd-to-End Testing