如何使用嵌套在 try { } catch { } 中的 Jest on call 编写测试

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

我正在 Node.js 快速服务器上使用 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/Whosebug/55986832/server.test.js
  Test the root path
    ✓ GET method returns status code 200 (45ms)

  console.log src/Whosebug/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/Whosebug/55986832