I have a function as such:
export type SendMessageParams = {
chatSession?: ChatSession,
// ... other params ...
};
const sendMessage = async ({
chatSession,
// ... other params ...
}: SendMessageParams): Promise<void> => {
// await chatSession?.sendMessage()
// somewhere in implementation
};
export default sendMessage;
ChatSession is from @google/generative-ai
.
I’d like to mock it in my test file as such:
let defaultParams: SendMessageParams;
beforeEach(() => {
jest.mock('@google/generative-ai', () => ({
ChatSession: {
sendMessage: async (content: string) => content,
},
}));
defaultParams = {
chatSession: new ChatSession('', ''),
// ... other params ...
};
});
afterEach(() => {
jest.clearAllMocks();
});
it('should send message', async () => {
// await sendMessage();
});
When I run npm run test
, I get the error saying:
FAIL tests/logic/actions/sendMessage.test.ts
● should send message
ReferenceError: fetch is not defined
43 | const sendMessageInner = async (messages: Message[]) => {
44 | setMessageListState(messages);
> 45 | const result = await chatSession?.sendMessage(content);
| ^
46 | const responseText = result?.response.text();
47 | if (responseText) {
48 | const responseMessage: Message = {
at makeRequest (node_modules/@google/generative-ai/dist/index.js:246:9)
at generateContent (node_modules/@google/generative-ai/dist/index.js:655:28)
at node_modules/@google/generative-ai/dist/index.js:890:25
at ChatSession.sendMessage (node_modules/@google/generative-ai/dist/index.js:909:9)
at sendMessageInner (src/logic/actions/sendMessage.ts:45:20)
at src/logic/actions/sendMessage.ts:72:7
at sendMessage (src/logic/actions/sendMessage.ts:59:3)
at Object.<anonymous> (tests/logic/actions/sendMessage.test.ts:44:3)
…which hints that chatSession.sendMessage
method still uses the real implementation instead of mock.
I’d like to know why this happens and what the solution would be.
Thanks in advance.
Environment
- Node 20.11.0 (lts/iron)
- Jest 29.7.0
@google/generative-ai
0.5.0 (if relevant)
You must log in or register to comment.