I’m relatively new to using unit testing and jest, trying to mock postgresql (pg) and I don’t understand what is going on with the mocking.
My code is extremely similar, if not the same to the answer in this post: How to Mock postgresql (pg) in node.js using jest
except that I’m using “Pool” instead of Client. I’ve spent a lot of time trying to google and understand the problem such as maybe it’s due to jest ‘hoisting’ if that’s relevant, but I couldn’t understand nor figure it out.
My main issue is that I have 2 tests. My test1 passes completely fine, no issues at all and I think it is mocking correctly, but the second time I call pool.query.mockResolvedValue
, which is in a separate test2, it errors out giving me “TypeError: pool.query.mockResolvedValue is not a function”
In test1, I’d like to clarify my function calls query multiple times, and so needs that to have a mocked value several times. Same for test2. I could just move all the code I want under test1 and that DOES since of course test1 always worked, but I think that’d ruin the readability a lot and wouldn’t really be clean to have everything under a singular it('test1').
To my understanding this should work as it does in the stack overflow link I sent above without using any other methods like spyOn… right?
I put it in the comments of the code below, but I’ve tried removing import, and removing the ‘clearAllMocks’ in the afterEach but it still errors out the same way. Any help and advice is appreciated! Thanks
index.test.ts:
import { Pool } from "pg";
// I read somewhere that importing jest doesn't really matter in a jest file so even without this line, the same error occurred
import { jest, describe, it, beforeEach, afterEach } from "@jest/globals"
// Also using sinon but I don't think that's relevant
import * as sinon from "sinon"
jest.mock("pg", () => {
const mPool= {
connect: jest.fn(),
query: jest.fn(),
end: jest.fn(),
};
return { Pool: jest.fn(() => mPool) };
});
describe('test', () => {
let pool;
beforeEach(() => {
pool= new Pool();
});
afterEach(() => {
// I've tried to remove this clearAllMocks line too, but it did nothing and still same error.
jest.clearAllMocks();
});
it('test1', async () => {
const expectedQueryResult = [{"test": 5 }]
const mockedQuery = pool.query.mockResolvedValue({ rows: [], expectedQueryResult });
// assertions for testing function expecting to pass, they do pass fine
mockedQuery .mockImplementationOnce((a,b) => {
throw new Error('test error')
});
try {
await testFunction()
sinon.assert.fail()
} catch (error) {
sinon.assert.match(error, 'test error')
}
// This whole test passes
});
it('test2', async () => {
const expectedQueryResult = [{"test": 5 }]
pool.query.mockResolvedValue({ rows: [], expectedQueryResult });
// more code for this test below, but I error out with the above line.
// TypeError: pool.query.mockResolvedValue is not a function
});
});
The Dabber is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.