首页 > 解决方案 > chai 和 mocha 在错误后显示测试成功

问题描述

我正在使用 chai 和 mocha 来测试我在 NodeJS 中使用 typescript 开发的 REST API。我已经用 mockgoose 库编写了我的测试用例来模拟 mongo db。当我运行第一个测试用例时,它成功地在数据库中添加了一些数据,但是当我运行第二个和第三个测试用例时,它首先显示Cannot set headers after they are sent to the client,然后显示测试通过。我没有得到这个执行工作流程它是如何发生的。谁能解释一下。我的测试用例文件如下所示:

process.env.NODE_ENV = 'TEST';

import { expect } from 'chai';
import request from 'supertest';

import app from '../app';
import * as mongodb from '../mongo/connection';

describe('Testing the API', () => {

    before((done) => {
        mongodb.connectMock()
            .then(() => done())
            .catch((err:any) => done(err))
    })

    it('OK, adding new employee', (done) => {
        request(app).put('/add')
            .send(<some JSON data>)
            .then(res => {
                ...some matcher
                done();
            })
            .catch(err => done(err));
    })

   it('OK, getting all employees', (done) => {
        request(app).get('/all')
            .then(res => {
                ...some matcher
                done();
            })
            .catch(err => { 
                done(err)
            });
    })

   it('OK, getting employee by ID', (done) => {

        request(app)
            .get(`/create/${id}`)
            .then(res => {
                ...some matcher
                done();
            })
            .catch(err => done(err));
    }) 
})

产生错误的控制器文件是:

import { Request, Response } from 'express';
import Employee from '../models/Employee'
import { v4 as uuidv4 } from 'uuid';

export let allEmployee = (req: Request, res: Response) => {
    Employee.find({})
            .then(allEmployee => {
                console.log('Getting all employee');
                if(allEmployee.length > 0)
                    res.status(200).json(allEmployee);
                console.log('Empty set of employees, please create');
                res.status(404).json({ error: 'No employee found, please create', employee: allEmployee });
            })
            .catch((err:any) => {
                console.log(err)
                res.status(400).json({ error: err }); ****** I GET THE ERROR HERE ******
            })
}

export let getEmployeeById = (req: Request, res: Response) => {
    const employeeId: string = req.params.id;
    Employee.find({ employeeId: employeeId })
            .then(employee => {
                console.log(`Getting employee with employee ID: ${employeeId}`);
                if(employee.length > 0)
                    res.status(200).json(employee);
                console.log(`No employee with employee ID: ${employeeId} found`);
                res.status(404).json({ error: 'No employee found', employee: employee });
            })
            .catch(err => {
                res.status(400).json({ error: err }); ****** I GET THE ERROR HERE ******
            })
}

export let addEmployee = (req: Request, res: Response) => {

    let employee = new Employee(<Some employee data in JSON>)

    employee.save()
            .then(employeeSaved => {
                res.status(201).json({ message: 'New employee created!', employee: employeeSaved });
            })
            .catch(err => {
                res.status(400).json({error:err}); 
            })
}

将员工添加到数据库的第一个测试用例工作得很好,但是当它用于第二个和第三个测试用例时,它首先显示错误,然后成功通过测试用例。看起来像 :

Mock MongoDB Connected
    ✓ OK, adding new employee (69ms)
Getting all employees
Empty set of employees, please create
(node:16761) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:526:11)
    at ServerResponse.header (/Users/abc/Downloads/some/git-test/some/Docker_Solution/node_modules/express/lib/response.js:771:10
)
    at ServerResponse.send (/Users/abc/Downloads/some/git-test/some/Docker_Solution/node_modules/express/lib/response.js:170:12)
    at ServerResponse.json (/Users/abc/Downloads/some/git-test/some/Docker_Solution/node_modules/express/lib/response.js:267:15)
    at /Users/abc/Downloads/some/git-test/some/Docker_Solution/src/controllers/employeeController.ts:16:33
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:16761) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch bl
ock, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rej
ections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 9)
(node:16761) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
    ✓ OK, getting all employees
Getting employee with employee ID: 2b1e419e-57a7-4785-a3d7-96a1c786676b
No employee with employee ID: 2b1e419e-57a7-4785-a3d7-96a1c786676b found
(node:16761) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:526:11)
    at ServerResponse.header (/Users/abc/Downloads/some/git-test/some/Docker_Solution/node_modules/express/lib/response.js:771:10)
    at ServerResponse.send (/Users/abc/Downloads/some/git-test/some/Docker_Solution/node_modules/express/lib/response.js:170:12)
    at ServerResponse.json (/Users/abc/Downloads/some/git-test/some/Docker_Solution/node_modules/express/lib/response.js:267:15)
    at /Users/abc/Downloads/some/git-test/some/Docker_Solution/src/controllers/employeeController.ts:31:33
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:16761) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 10)
    ✓ OK, getting employee by ID


  3 passing (1s)

标签: node.jstypescriptmocha.jschai

解决方案


您的问题似乎是您遇到问题的两条路线或两条路线的if条件。res.status(200)res.status(400)

if(employee.length > 0)
   res.status(200).json(employee);
   console.log(`No employee with employee ID: ${employeeId} found`);
   res.status(404).json({ error: 'No employee found', employee: employee });

应该是,if(){} else{}因为您正尝试再次发送/更改响应以及您已经发送的 200

if(employee.length > 0) {
    res.status(200).json(employee);
} else {
    console.log(`No employee with employee ID: ${employeeId} found`);
    res.status(404).json({ error: 'No employee found', employee: employee });
}

推荐阅读