I'm trying to get rid of jest message "Jest has detected the following 2 open handles" when running my tests. But I've reached a dead-end as of right now.
This is one of my tests I'm attempting to fix:
describe('POST /products', function () {
let agent, server
beforeEach(function (done) {
server = app.listen(3001, (err) => {
if (err) return done(err);
agent = supertest(server)
done();
})
utils.reset()
})
it('Adds a new product', function () {
utils.testCategories().push('Celulares') // This function returns an array of strings
return agent
.post('/products')
.send({
name: 'iPhone 13 Pro',
brand: 'Apple',
category: 'Celulares',
stock: 8
})
.expect(201)
.expect('Content-Type', /json/)
.expect(function (res) {
expect(res.body).toEqual({
name: 'iPhone 13 Pro',
categoryId: 1,
brand: 'Apple',
stock: 8,
available: true,
reviews: [],
rating: 0
})
expect(utils.testProducts()).toHaveLength(1) // This one an array of objects
expect(utils.testProducts()[0].name).toEqual('iPhone 13 Pro')
})
afterEach((done) => {
server.close(done)
})
})
})
I don't see anything wrong with that code, I open the server, then I close it.
Here is the route I'm trying to test:
router.post('/products', async (req, res) => {
const { name, brand, category, stock } = req.body;
addProduct(name, brand, category, stock) // This function makes an async operation with a fake db
.then((results) => {
res.status(201).send(results)
})
.catch(err => res.status(404).send({ error: err.message }))
})
Once the tests are done, jest prints this message on the console
Jest has detected the following 1 open handle potentially keeping Jest from exiting:
● bound-anonymous-fn
6 | let agent, server
7 | beforeEach(function (done) {
> 8 | server = app.listen(3001, (err) => {
| ^
9 | if (err) return done(err);
10 | agent = supertest(server)
11 | done();
at Function.listen (node_modules/express/lib/application.js:635:24)
at Object.<anonymous> (tests/11.test.js:8:18)
at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)
at runJest (node_modules/@jest/core/build/runJest.js:404:19)
at _run10000 (node_modules/@jest/core/build/cli/index.js:320:7)
at runCLI (node_modules/@jest/core/build/cli/index.js:173:3)
Maybe it's worth noting that in the GET requests this message do not have this issue at all.
Also, I've tried using --forceExit when executing the tests, but this is not a proper solution, and it actually keeps printing the message.
Any provided advise would be most apreciated
SOLUTION:
After updating jest to version [email protected] and ts-jest to [email protected] the error has dissapeared.
I'm trying to get rid of jest message "Jest has detected the following 2 open handles" when running my tests. But I've reached a dead-end as of right now.
This is one of my tests I'm attempting to fix:
describe('POST /products', function () {
let agent, server
beforeEach(function (done) {
server = app.listen(3001, (err) => {
if (err) return done(err);
agent = supertest(server)
done();
})
utils.reset()
})
it('Adds a new product', function () {
utils.testCategories().push('Celulares') // This function returns an array of strings
return agent
.post('/products')
.send({
name: 'iPhone 13 Pro',
brand: 'Apple',
category: 'Celulares',
stock: 8
})
.expect(201)
.expect('Content-Type', /json/)
.expect(function (res) {
expect(res.body).toEqual({
name: 'iPhone 13 Pro',
categoryId: 1,
brand: 'Apple',
stock: 8,
available: true,
reviews: [],
rating: 0
})
expect(utils.testProducts()).toHaveLength(1) // This one an array of objects
expect(utils.testProducts()[0].name).toEqual('iPhone 13 Pro')
})
afterEach((done) => {
server.close(done)
})
})
})
I don't see anything wrong with that code, I open the server, then I close it.
Here is the route I'm trying to test:
router.post('/products', async (req, res) => {
const { name, brand, category, stock } = req.body;
addProduct(name, brand, category, stock) // This function makes an async operation with a fake db
.then((results) => {
res.status(201).send(results)
})
.catch(err => res.status(404).send({ error: err.message }))
})
Once the tests are done, jest prints this message on the console
Jest has detected the following 1 open handle potentially keeping Jest from exiting:
● bound-anonymous-fn
6 | let agent, server
7 | beforeEach(function (done) {
> 8 | server = app.listen(3001, (err) => {
| ^
9 | if (err) return done(err);
10 | agent = supertest(server)
11 | done();
at Function.listen (node_modules/express/lib/application.js:635:24)
at Object.<anonymous> (tests/11.test.js:8:18)
at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)
at runJest (node_modules/@jest/core/build/runJest.js:404:19)
at _run10000 (node_modules/@jest/core/build/cli/index.js:320:7)
at runCLI (node_modules/@jest/core/build/cli/index.js:173:3)
Maybe it's worth noting that in the GET requests this message do not have this issue at all.
Also, I've tried using --forceExit when executing the tests, but this is not a proper solution, and it actually keeps printing the message.
Any provided advise would be most apreciated
SOLUTION:
After updating jest to version [email protected] and ts-jest to [email protected] the error has dissapeared.
3 Answers
Reset to default 4It looks like this has been fixed in more recent updates, an upgrade to [email protected]
and [email protected]
resolved it for me.
I encountered the same.
I don't know what is the issue/cause - but more can be seen in github repositories of jest (and supertest maybe).
I downgraded my packages: npm i [email protected] [email protected] -D
, previously i had "jest": "^27.5.1",
and "ts-jest": "^27.1.4",
. With jest/ts-jest versions of 26.x.x
I am not facing warnings anymore.
I did my downgrade based on this ment: https://github./facebook/jest/issues/11649#issuement-992690198
Just for reference, if you're also using body-parser
on your express app, then there's an active bug that also creates this issue: https://github./ladjs/supertest/issues/772