felixmosh/knex-mock-client

Mock handler not found error - when using jest fake timers

Closed this issue · 8 comments

I have the following setup -

beforeAll(() => {
        jest.useFakeTimers().setSystemTime(new Date('2022-04-23'));
    });
    afterAll(() => {
        jest.runOnlyPendingTimers(); // >> #
        jest.useRealTimers(); 
    });

I am using # as mentioned here https://testing-library.com/docs/using-fake-timers/ in the last paragraph.
If I dont use this, I get simply jest timeout error

But if I use this, I get an error message as below :

 Error: select * from "pqr" where "def" = $1  - Mock handler not found
        at abc-user/node_modules/knex-mock-client/dist/Tracker.js:45:24
        at callTimer (abc-user/node_modules/@sinonjs/fake-timers/src/fake-timers-src.js:745:24)
        at doTickInner (abc-user/node_modules/@sinonjs/fake-timers/src/fake-timers-src.js:1307:29)
        at doTick (abc-user/node_modules/@sinonjs/fake-timers/src/fake-timers-src.js:1388:20)
        at Object.tick (abc-user/node_modules/@sinonjs/fake-timers/src/fake-timers-src.js:1396:20)
        at Object.runToLast (abc-user/node_modules/@sinonjs/fake-timers/src/fake-timers-src.js:1552:26)
        at FakeTimers.runOnlyPendingTimers (abc-user/node_modules/@jest/fake-timers/build/modernFakeTimers.js:66:19)
        at Object.runOnlyPendingTimers (abc-user/node_modules/jest-runtime/build/index.js:2406:52)
        at Object.<anonymous> (abc-user/tests/unit-tests/file.test.ts:825:14)
        at Promise.then.completed (abc-user/node_modules/jest-circus/build/utils.js:333:28)

I am using fake timers of jest to set a specific date for another library used in flow - dayjs.
If I dont use fake timers, the test works correctly.

I dont think, the error is due to dayjs-faketimer as that is giving correct output of past specific date.

I think global timers manipulated by fake timers is not going well with knex-mock-client.

Any suggestions to make them work together ?

Thanks.

runAllPendingTimers should be inside the test it self.

Can you share the test it self? the mock setup?

runAllPendingTimers should be inside the test it self.

Tried this, it is giving same jest timeout error which is happening when not adding runAllPendingTimers at all.

Can you share the test it self? the mock setup?

Same as this #22 (comment) . Just used faketimers in beforeAll

I think global timers manipulated by fake timers is not going well with knex-mock-client.

I strongly suspect this. Let me try to modify/fake only current time in jest & not setTimeout as is happening with jest.useFakeTimers

This lib is using setTimeout inside it, if you are taking the control of timers into your hands, you should "release" the timeout manually.

I can add a test that will pass when using fakeTimers,

Anyhow, I can't help you without a full reproduction example / code snippet.

This lib is using setTimeout inside it, if you are taking the control of timers into your hands, you should "release" the timeout manually.

Releasing when ? At afterAll or after service function is called ?
Tried both ways.

I can add a test that will pass when using fakeTimers,

You write this please, I will check what is different from my setup. Please include one knex query in the test.

Anyhow, I can't help you without a full reproduction example / code snippet.

Will work on this in near future so that all issues can be reported with examples on this test repo.

Can you try v1.8.3 it should work like in the test

Tested. It works now with v1.8.3.
Thanks!