I’m trying to write some tests around the API calls and how my application processes the results in Typescript. I’m using Jest as my test framework, and want to mock the OpenAI calls to make the tests reproducible and faster.
My problem with this right now is that I cannot get Jest to mock the openai
module. That is, this code doesn’t actually mock the module at all. It leaves the OpenAI class as its original.
import { OpenAI } from "openai";
jest.mock('openai', () => {
return jest.fn().mockImplementation(() => {
return {
completions: {
create: jest.fn().mockImplementation(async () => { return mockOpenAIresponses.create; })
}
}
})
});
Based on everything I read, the OpenAI class should get replaced with my mocked up class, but it does not. It still calls the original constructor.
I’ve found exactly one article implying that this works with openai, and many articles on Jest mocking in general which imply this should just work. Does anyone have a working example in typescript?