簡體   English   中英

如何使用嵌套在 try { } catch { } 中的 Jest on call 編寫測試

[英]How to write test with Jest on call nested in a try { } catch { }

我正在 Node.js express 服務器上使用 Jest 設置一些測試,但我不知道如何測試嵌套在try/catch塊中的調用。

這是我的 server.js 的一部分:

const start = async () => {
  try {
    if (process.env.NODE_ENV) {
      await db.sync({ force: false });
    } 

    ...

    app.get("/", (request, response) => {
      response.send("Please feel free to use our api with /api");
    });

    ...

    app.listen(port, () => {
        console.log(`Server running on port ${port}`);
        return app;
    });

  } catch (err) {
    console.log(err.message);
  }
};

export default new Promise ( async () => {
  return await start();
});

這里我想測試一下什么是app.listen()狀態碼,但是我對測試還是不太熟悉。

有什么建議嗎?

這是我寫的測試:

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

describe('Test the root path', ()=>{
  test("GET method returns status code 200", ()=>{
    request(app).get('/').then( response =>{
      expect(response.statusCode).toBe(200);
    });
  });
})

我認為app不是我所期望的,因為 Jest 告訴我app.address不是函數,所以我的export default new Promise不是正確的解決方案。

對不起,如果這看起來很亂,希望你能幫忙!

這是解決方案:

server.js

const express = require('express');
const app = express();
const port = 3000;
const db = {
  async sync(options) {},
};

const start = async () => {
  try {
    if (process.env.NODE_ENV) {
      await db.sync({ force: false });
    }

    app.get('/', (request, response) => {
      response.send('Please feel free to use our api with /api');
    });

    return app.listen(port, () => {
      console.log(`Server running on port ${port}`);
    });
  } catch (err) {
    console.log(err.message);
  }
};

export default start;

server.test.js

import request from 'supertest';
import start from './server';

describe('Test the root path', () => {
  let server;
  beforeAll(async () => {
    server = await start();
  });
  afterAll((done) => {
    server.close(done);
  });

  test('GET method returns status code 200', () => {
    expect.assertions(1);
    return request(server)
      .get('/')
      .then((response) => {
        expect(response.statusCode).toBe(200);
      });
  });
});

帶有覆蓋率報告的集成測試結果:

 PASS  src/stackoverflow/55986832/server.test.js
  Test the root path
    ✓ GET method returns status code 200 (45ms)

  console.log src/stackoverflow/55986832/server.js:3342
    Server running on port 3000

-----------|----------|----------|----------|----------|-------------------|
File       |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
-----------|----------|----------|----------|----------|-------------------|
All files  |    92.31 |       50 |      100 |    92.31 |                   |
 server.js |    92.31 |       50 |      100 |    92.31 |                22 |
-----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        5.075s, estimated 10s

源代碼: https : //github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/55986832

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM