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.
Before diving into integration testing, ensure you have the following:
To begin, let's set up a basic TypeScript project. If you already have a project, skip to the next section.
Initialize a new Node.js project:
mkdir integration-testing-ts
cd integration-testing-ts
npm init -y
Install TypeScript and necessary dependencies:
npm install typescript ts-node @types/node --save-dev
Create a tsconfig.json file:
{
"compilerOptions": {
"target": "ES6",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src"]
}
Create the project structure:
integration-testing-ts/
āāā src/
ā āāā index.ts
āāā test/
ā āāā index.test.ts
āāā tsconfig.json
āāā package.json
Add a simple function in src/index.ts:
export const add = (a: number, b: number): number => {
return a + b;
};
For this tutorial, we'll use Jest and Mocha as popular TypeScript testing frameworks. You can choose either based on your preference.
Install Jest and its TypeScript types:
npm install jest @types/jest ts-jest --save-dev
Configure Jest:
Create a jest.config.js file in the root of your project:
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
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);
});
});
Run the tests:
Add a script to your package.json:
"scripts": {
"test": "jest"
}
Run the tests using:
npm test
Install Mocha, Chai, and their TypeScript types:
npm install mocha chai @types/mocha @types/chai --save-dev
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);
});
});
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
A good integration test should follow a clear structure:
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.
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);
});
});
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);
});
});
Ensure your tests cover all critical paths and edge cases. Use tools like Istanbul (for Jest) or NYC (for Mocha) to measure test coverage.
Install Istanbul:
npm install --save-dev istanbul
Modify the test script in package.json:
"scripts": {
"test": "jest --coverage"
}
Run tests with coverage:
npm test
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
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.