Closed Gotusso closed 5 years ago
It's been over a year. Any sort of response would be welcome.
@Gotusso @jsumners my apologies for missing this, we currently only support a single tracker, https://github.com/colonyamerican/mock-knex/blob/master/src/tracker.js#L38, you can mock multiple connections but they will all be reported to the same tracker.
@jbrumwell Any plans to change this? Would be super helpful for my workflow as well.
Obviously it's been a year since the previous comment, but I just spent an entire day banging my head against the same problem and wanted to share the workaround I came up with.
We have mock-knex
wrapped in another class that also handles setting up a GraphQL schema, resolvers, etc, so we can easily test whether the correct SQL requests are being made. Switching from import mockKnex from 'mock-knex'
to a dynamic require allows us to use Jest's resetModules
function to wipe the module cache:
class KnexTester {
constructor() {
this.knex = knex(...knex config here...);
jest.resetModules();
const mockKnex = require(`${process.cwd()}/node_modules/mock-knex`);
mockKnex.mock(this.knex);
const tracker = mockKnex.getTracker();
tracker.install();
...other setup...
}
}
describe('a test block', () => {
const tester = new KnexTester();
test(...
});
describe('another test block', () => {
const tester = new KnexTester();
test(...
});
Hi there,
I think I found an issue while debugging a test case. I noticed that we have multiple tests where each one sets up a new mocked knex connection, but then the results are mixed up. It more or less comes to this:
I had a quick look and I think mock.getTracker() should not return a singleton, is that right? Or am I misunderstanding the API in any way?