felixmosh / knex-mock-client

A mock client for knex which allows you to write unit tests with DB interactions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Getting "Tracker not configured for knex mock client" error when running unit test

karenm1895 opened this issue · comments

Hi, we are trying to set up knex mock client in our unit test but unfortunately we are getting the error message "Tracker not configured for knex mock client". To be specific when we run it for the first time, the test would pass then running it for the second time, it would get the error message above. The test result would alternate as we continue to run it.

Here is the error from jest:
image

Here's our knex set up:
image

Here's our sample unit test:
image

Do you have any thoughts with this issue? Thanks!

The code looks ok, probably there is something that resets the setup of the tracker,

Try to move the tracker creation to a beforeEach section

Hello Felix, we tried moving the creation of the tracker inside beforeEach and we got the same error
image

We also tried putting the test setup code inside the test itself, but got the same error.
image

Can you prepare a small repo that reproduces the issue? 🙏🏼

Hi Felix,
Unfortunately, we cannot replicate this behavior in our local repo and we cannot share our client repos.

Our observation in our testing is when the controller runs first before the service we will get a fail and when the service runs first before the controller we get a pass, currently the sequence of how jest run the test in our code is random.
With the observation we have above, we just added a workaround to use a test-sequencer to explicitly tell to run the services test before the controller and other test to have a pass in our unit test.

Let me know if you have any idea why we're encountering that weird behavior or other suggestion we can try to fix it. Thanks!

Sounds like an issue with parallel run, you need to mock your db connection on every test that uses it.

I don't need you to share you real code but a small example that recreates this issue, otherwise, I can't help you.

In my project, I have a createApp function which will initiliaze all express routes and will return an Express app, this createApp gets the db instance.

So in production code I'm connecting to the real DB, in tests i', creating a new Knex instance with the mock client and passing it to the function, this way I don't need to use jest mock modules.

Maybe this will help you to structure your project / tests better.

Thanks for the idea Felix, we'll try that in our project.

Thanks for the help! I'll close this ticket.