Here's a function I have:
export type SendMessageParameters = {
chatInstance?: ChatSession,
// ... other parameters ...
};
const sendMessageFunction = async ({
chatInstance,
// ... other parameters ...
}: SendMessageParameters): Promise<void> => {
// await chatInstance?.sendMessage()
// implementation details here
};
export default sendMessageFunction;
ChatSession comes from @google/generative-ai
.
In my testing file, I want to create a mock like this:
let defaultParams: SendMessageParameters;
beforeEach(() => {
jest.mock('@google/generative-ai', () => ({
ChatSession: {
sendMessage: async (content: string) => content,
},
}));
defaultParams = {
chatInstance: new ChatSession('', ''),
// ... other params ...
};
});
afterEach(() => {
jest.clearAllMocks();
});
it('should send message', async () => {
// await sendMessage();
});
Upon running npm run test
, an error occurs:
FAIL tests/logic/actions/sendMessage.test.ts
● should send message
ReferenceError: fetch is not defined
...
This error indicates that the chatInstance.sendMessage
method is using the actual implementation instead of the mock. I'm curious about why this is happening and how to resolve it.
Your help is appreciated.
Environment
- Node 20.11.0 (lts/iron)
- Jest 29.7.0
@google/generative-ai
0.5.0 (if relevant)