Search code examples
node.jsmocha.jssupertestknex.js

Supertest + Knex.js = There is no pool defined on the current client


i'm using supertest (within Mocha) to test my app, that is currently using knex.js for database purposes.

Everything was working well with and old version of knex, (0.5.x). Now i want to use latest knex version.

In most scenarios, using

request = supertest(app)

works fine for me, but there are other scenarios i need to test several requests without reusing cookies so...

request = supertest.agent(app)

what happens with this?

It ends up with the error described at the subject. I've added some custom logs 'acquireConnection', 'releaseConnection', 'initializePool' and 'destroy' functions to the knex lib to see what's going on and the output is something like:

Adquiring connection from pool {someCustomPoolIdYSet)
Adquiring connection from pool {someCustomPoolIdYSet)
Releasing connection from pool {someCustomPoolIdYSet)
Adquiring connection from pool {someCustomPoolIdYSet)
Adquiring connection from pool undefined
[Error] ........ "There is no pool defined on the current client"

Any help would be appreciated. Thanks in advance.


Solution

  • So, finally we found out!

    The problem was Passport.js, that was always being used as a singleton by default. Each time we bootstrap the app, a new serializer was being injected in Passport, but it was keeping previous ones. Then, those that were injected at the beginning had their pool destroyed... and boom!