A template for using Typescript and Jest
Create a src folder for the source code and a test folder for the unit tests
- mkdir src
- mkdir tests
Use npm to initiate a new project at the root directory
- npm init -y
Add TypeScript as a development dependency
- npm i typescript --save-dev
Generate a tsconfig.json and modify it's setting
- tsc --init
- changes the setting in tsconfig.json to your prefs.
Add Jest as a development dependency
- npm install jest --save-dev
Install the ts-jest package that contains a preprocessor with source map between TypeScript and Jest
- npm install ts-jest --save-dev
Install the type definitions for Jest
- npm install @types/jest --save-dev
Create the entry point file that will be transpiled to js.
- Create an index.ts file in the 'src' directory where you will place your code - in our test a function called add() which also must be exported as a module i.e.,
export function add(a: number, b: number): number {
// return sum of two numbers
return a + b;
}
OR
function sum(a, b) {
return a + b;
}
module.exports = sum;
Create a matching test file.
- Create a file inside tests named index.test.ts
Import the code you will test
- We import the add function from the index.ts file to out jest test file. Then, we write a new test suite that describes the function and inside that, we creates the first test based on our tests/rules i.e.,
const sum = require('./index');
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});
OR
import { add } from "../src/index";
describe("test add function in index file", () => {
test("Adding two numbers", () => {
expect(add(2, 1)).toBe(3);
});
});
Create jest config file | or use jest --init
- Create a “jest.config.js” at the root of the project that contains:
module.exports = {
transform: {'^.+\\.ts?$': 'ts-jest'},
testEnvironment: 'node',
testRegex: '/tests/.*\\.(test|spec)?\\.(ts|tsx)$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node']
};
Set up your test to run
- Add the following section to your package.json:
{
"scripts": {
"test": "jest --watchAll --verbose --coverage"
}
}
Run tsc and Run tests in terminals
- tsc --watch and npm test