Я использую chai и mocha для тестирования моего REST API, разработанного в NodeJS с машинописью. Я написал свои тесты с библиотекой mock goose для mock mon go 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)