Write unit test with Jest and Typescript
- Step 1: Simplest start set
- Step 2: Add Babel for ES2015
- Step 3: Add Typescript
* Each steps can be seen in brances
Create a directory and install jest
mkdir hello-jest
cd hello-jest
yarn add -D jest
Create a js file to be tested named sum.js
// sum.js
function sum(a, b) {
return a + b;
}
module.exports = sum;
Create a test js file to test sum.js
// sum.test.js
const sum = require("./sum");
it("add 3 + 4 to be 7", () => {
expect(sum(3, 4)).toBe(7);
});
Add scripts to package.json
~~
"scripts": {
"test": "jest --watchAll"
}
~~
Open terminal and run test
yarn test
Add babel dev moduels follow
- @babel/core
- @babel/polyfill
- @babel/preset-env
yarn add -D @babel/core @babel/polyfill @babel/preset-env
Create a .babelrc
file
{
"presets": ["@babel/preset-env"]
}
Updates js files with ES2015 syntax
// sum.js
function sum(a, b) {
return a + b;
}
module.exports = sum;
// sum.test.js
const sum = require("./sum");
it("add 3 + 4 to be 7", () => {
expect(sum(3, 4)).toBe(7);
});
Add typescript dev moduels follow
- @babel/preset-typescript
- @types/jest
- babel-jest
yarn add -D @babel/preset-typescript @types/jest babel-jest
Update .babelrc
file
// .babelrc
{
"presets": ["@babel/preset-env", "@babel/preset-typescript"]
}
Change js files to typescritp files
- sum.js --> sum.ts
- sum.test.ts --> sum.test.ts
Update sum.ts files with typescript
// sum.ts
function sum(a: number, b: number): number {
return a + b;
}
export { sum };