🍵

jest + supertest + Typescript 設定

2023/04/03に公開

概要:

jest + supertest, Typescript の準備となります

  • 前のjestの関連となります。
  • 設定に、難航したので。作業メモです

構成

  • jest
  • supertest
  • express

参考


npm add

npm install --save-dev jest ts-jest @types/jest
npm install --save-dev babel-jest @babel/core @babel/preset-env
npm install --save-dev @babel/preset-typescript

jest --init
#

The following questions will help Jest to create a suitable configuration for your project

✔ Would you like to use Jest when running "test" script in "package.json"? … yes
✔ Would you like to use Typescript for the configuration file? … no
✔ Choose the test environment that will be used for testing › node
✔ Do you want Jest to add coverage reports? … no
✔ Which provider should be used to instrument code for coverage? › v8
✔ Automatically clear mock calls and instances between every test? … no


  • babel.config.js
module.exports = {
  presets: [['@babel/preset-env', {targets: {node: 'current'}}],
  '@babel/preset-typescript'
  ],
};

  • package.json
  "scripts": {
    "test": "jest"
  },

  • app.test.ts
app.test.ts
const request = require('supertest')
import {app}  from '../../index'

describe("GET /", () => {
  test("check: running string", async () => {
    const response = await request(app).get("/");
    expect(response.statusCode).toBe(200);
  });
});


  • test
yarn test

....

Discussion