Currently, I am working on writing unit tests for the API endpoints of my very first express app. I am using a data structure as a placeholder for a database and all the tests are passing successfully. However, I encountered an error in the console stating '... can't set headers after they are sent ...'. After investigating, I believe the issue lies within the parameter of the GET method in the 3rd test, specifically when fetching a single todo. Unfortunately, finding a solution has been challenging thus far.
import chai from 'chai';
import { app } from '../app';
import http from 'chai-http';
import db from '../db/db';
let expect = chai.expect;
chai.use(http);
describe('Conducting tests on all todo endpoints at "/api/v1/todos" and "/api/v1/todo/:id" involving (GET, POST, GET/id, PUT)', () => {
before(() => {});
after(() => {});
//Retrieve all todos
it('should fetch all todos at "/ap1/v1/todos" using GET', () => {
return chai
.request(app)
.get('/api/v1/todos/')
.then(res => {
expect(res).to.have.status(200);
expect(res).to.be.json;
expect(res.body).to.be.an('object');
expect(res.body)
.to.have.property('success')
.eql('true');
expect(res.body)
.to.have.property('message')
.eql('todos retrieved successfully');
expect(res.body.todos).to.be.an('array');
expect(
res.body.todos[Math.floor(Math.random() * res.body.todos.length)]
).to.have.property('id' && 'title' && 'description');
});
});
//Add a new todo
it('should include a new todo at "/api/v1/todos" through POST', () => {
return chai
.request(app)
.post('/api/v1/todos')
.send({ title: 'Dinner', description: 'Dinner with bae' })
.then(res => {
expect(res).to.have.status(201);
expect(res).to.be.json;
expect(res.body).to.be.an('object');
expect(res.body)
.to.have.property('success')
.eql('true');
expect(res.body)
.to.have.property('message')
.equal('todo added successfully');
expect(res.body.todo).to.be.an('object');
expect(res.body.todo)
.to.have.property('id')
.equal(db.length);
expect(res.body.todo)
.to.have.property('title')
.equal('Dinner');
expect(res.body.todo)
.to.have.property('description')
.equal('Dinner with bae');
});
});
//The following test passes but triggers a 'can't set headers after they are sent' error
it('should retrieve a single todo at "/api/v1/todos/:id" using GET/id', () => {
return chai
.request(app)
.get('/api/v1/todos/2')
.then(res => {
expect(res).to.have.status(200);
expect(res).to.be.json;
expect(res.body).to.be.an('object');
expect(res.body)
.to.have.property('success')
.eql('true');
expect(res.body)
.to.have.property('message')
.equal('todo retrieved successfully');
expect(res.body.todo).to.be.an('object');
expect(res.body.todo)
.to.have.property('id')
.equal(db.length);
expect(res.body.todo)
.to.have.property('title')
.equal('Dinner');
expect(res.body.todo)
.to.have.property('description')
.equal('Dinner with bae');
});
});
});
//Controllers
import db from '../db/db';
class todosController {
getAllTodos(req, res) {
return res.status(200).send({
success: 'true',
message: 'todos retrieved successfully',
todos: db
});
}
//Controller that results in the 'can't set headers after they are sent' error
getTodo(req, res) {
const id = parseInt(req.params.id, 10);
db.map(todo => {
if (todo.id === id) {
return res.status(200).send({
success: 'true',
message: 'todo retrieved successfully',
todo
});
}
});
return res.status(400).send({
success: 'false',
message: 'todo does not exist'
});
}
createTodo(req, res) {
if (!req.body.title) {
return res.status(400).send({
success: 'false',
message: 'title is required'
});
} else if (!req.body.description) {
return res.status(400).send({
success: 'false',
message: 'description is required'
});
}
const todo = {
id: db.length + 1,
title: req.body.title,
description: req.body.description
};
db.push(todo);
return res.status(201).send({
success: 'true',
message: 'todo added successfully',
todo
});
}
Any advice or suggestions on how to resolve this persistent error would be greatly appreciated. Check out this link