DEV Community

badruti94
badruti94

Posted on

Tutorial jest

Pertama kita inisialisasi projek dulu dengan perintah npm init --y.

Inisialisasi jest

Selanjutnya kita install jest dengan perintah npm i jest @types/jest -D. Lalu pada file package.json kita ubah script test dan tambahkan konfigurasi jest.

"scripts": {
    "test": "jest"
  },
"jest": {
    "verbose": true
  },
Enter fullscreen mode Exit fullscreen mode

Melakukan testing

Selanjutnya kita lakukan testing. Kita buat dahulu kode yang akan kita test. Buat file baru src/sum.js. Berikut isi filenya.

exports.sum = (a, b) => a + b;

exports.sumAll = (array) => {
  const result = array.reduce((a, b) => a + b, 0);
  return result;
};

Enter fullscreen mode Exit fullscreen mode

Kemudian kita tulis kode test-nya. Buat file baru test/sum.test.js. Nama file harus berakhiran .test.js.

const { sum, sumAll } = require('../src/sum');

test('should 20', () => {
  const result = sum(10, 10);
  expect(result).toBe(20);
});

test('should 30', () => {
  const result = sumAll([10, 10, 10]);
  expect(result).toBe(30);
});

Enter fullscreen mode Exit fullscreen mode

Ada banyak matcher [doc] yang bisa kita gunakan, kali ini kita gunakan matcher toBe.

Menjalankan testing

Oke kemudian sudah bisa kita jalankan testnya. Bisa kita jalankan dengan perintah npx jest atau npm test.

It function

Kita juga bisa menggunakan fungsi it daripada fungsi test agar lebih enak dibaca.

const { sum, sumAll } = require('../src/sum');

it('should 20', () => {
  const result = sum(10, 10);
  expect(result).toBe(20);
});

it('should 30', () => {
  const result = sumAll([10, 10, 10]);
  expect(result).toBe(30);
});

Enter fullscreen mode Exit fullscreen mode

Coverage

Coverage [doc] berfungsi untuk mengetahui seberapa banyak bagian kode kita yang sudah tercover testing-nya. Pada konfigurasi jest di package.json tambahkan beberapa konfigurasi sehingga kodenya

  "jest": {
    "verbose": true,
    "collectCoverage": true,
    "coverageThreshold": {
      "global": {
        "branches": 80,
        "functions": 80,
        "lines": 80,
        "statements": 80
      }
    }
  },
Enter fullscreen mode Exit fullscreen mode

Hasil coveragenya bisa dilihat di coverage/lcov-report/index.html.

Express

Untuk melakukan testing dengan jest pada express kita harus install library tambahan npm i supertest -D.
Buat file baru src/index.js.

const express = require('express');

const app = express();

app.get('/', (req, res) => {
  res.send('Hello world');
});

module.exports = app;

Enter fullscreen mode Exit fullscreen mode

Kita buat code testnya

const request = require('supertest');
const app = require('../src/index');

it('should Hello world', async () => {
  const response = await request(app).get('/');
  expect(response.text).toBe('Hello world');
});

Enter fullscreen mode Exit fullscreen mode

Kita jalankan lagi testnya npm test

Menggunakan jest bersama eslint

Jika kita menginstall eslint pada projek ini maka akan muncul error pada file test. Untuk mengatasi hal ini kita butuh library tambahan. Kita install librarynya npm i eslint-plugin-jest -D.

Setelah diinstall ubah file .eslintrc.json

{
    "env": {
        "commonjs": true,
        "es2021": true,
        "node": true,
        "jest/globals": true
    },
    "extends": "airbnb-base",
    "parserOptions": {
        "ecmaVersion": "latest"
    },
    "rules": {
        "no-console": "off",
        "linebreak-style": "off"
    },
    "plugins": ["jest"]
}
Enter fullscreen mode Exit fullscreen mode

Oke. Itu saja, tutorial kali ini. Sekian terima kasih.

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay