Unit Testing

Install Jest

In VSCode from a terminal;

npm install --save-dev jest ts-jest @types/jest

jest.config.js

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

module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
};

tsconfig.json

Update tsconfig.json:

Ensure your tsconfig.json is set up to handle Jest and TypeScript. Add the following configuration if it's not already present:

{
  "compilerOptions": {
    "target": "ES6",
    "module": "commonjs",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "types": ["jest"]
  }
}

package.json

Add a script to your package.json to run the tests:

# Setup Tests

Create Tests directory

Create a tests directory in your project in the project root, adjacent to your src directory.

Create a Test

Tests are TypeScript files that end in .test.ts.

Here's an example;

Run Tests

Last updated