Third party library mocked with Jest still tries to access internals
I have a function as such:
<span style="color:#323232;">export type SendMessageParams = {
</span><span style="color:#323232;"> chatSession?: ChatSession,
</span><span style="color:#323232;"> // ... other params ...
</span><span style="color:#323232;">};
</span><span style="color:#323232;">
</span><span style="color:#323232;">const sendMessage = async ({
</span><span style="color:#323232;"> chatSession,
</span><span style="color:#323232;"> // ... other params ...
</span><span style="color:#323232;">}: SendMessageParams): Promise<void> => {
</span><span style="color:#323232;"> // await chatSession?.sendMessage()
</span><span style="color:#323232;"> // somewhere in implementation
</span><span style="color:#323232;">};
</span><span style="color:#323232;">
</span><span style="color:#323232;">export default sendMessage;
</span>
ChatSession is from @google/generative-ai
.
I’d like to mock it in my test file as such:
<span style="color:#323232;">let defaultParams: SendMessageParams;
</span><span style="color:#323232;">
</span><span style="color:#323232;">beforeEach(() => {
</span><span style="color:#323232;"> jest.mock('@google/generative-ai', () => ({
</span><span style="color:#323232;"> ChatSession: {
</span><span style="color:#323232;"> sendMessage: async (content: string) => content,
</span><span style="color:#323232;"> },
</span><span style="color:#323232;"> }));
</span><span style="color:#323232;"> defaultParams = {
</span><span style="color:#323232;"> chatSession: new ChatSession('', ''),
</span><span style="color:#323232;"> // ... other params ...
</span><span style="color:#323232;"> };
</span><span style="color:#323232;">});
</span><span style="color:#323232;">
</span><span style="color:#323232;">afterEach(() => {
</span><span style="color:#323232;"> jest.clearAllMocks();
</span><span style="color:#323232;">});
</span><span style="color:#323232;">
</span><span style="color:#323232;">it('should send message', async () => {
</span><span style="color:#323232;"> // await sendMessage();
</span><span style="color:#323232;">});
</span>
When I run npm run test
, I get the error saying:
<span style="color:#323232;"> FAIL tests/logic/actions/sendMessage.test.ts
</span><span style="color:#323232;"> ● should send message
</span><span style="color:#323232;">
</span><span style="color:#323232;"> ReferenceError: fetch is not defined
</span><span style="color:#323232;">
</span><span style="color:#323232;"> 43 | const sendMessageInner = async (messages: Message[]) => {
</span><span style="color:#323232;"> 44 | setMessageListState(messages);
</span><span style="color:#323232;"> > 45 | const result = await chatSession?.sendMessage(content);
</span><span style="color:#323232;"> | ^
</span><span style="color:#323232;"> 46 | const responseText = result?.response.text();
</span><span style="color:#323232;"> 47 | if (responseText) {
</span><span style="color:#323232;"> 48 | const responseMessage: Message = {
</span><span style="color:#323232;">
</span><span style="color:#323232;"> at makeRequest (node_modules/@google/generative-ai/dist/index.js:246:9)
</span><span style="color:#323232;"> at generateContent (node_modules/@google/generative-ai/dist/index.js:655:28)
</span><span style="color:#323232;"> at node_modules/@google/generative-ai/dist/index.js:890:25
</span><span style="color:#323232;"> at ChatSession.sendMessage (node_modules/@google/generative-ai/dist/index.js:909:9)
</span><span style="color:#323232;"> at sendMessageInner (src/logic/actions/sendMessage.ts:45:20)
</span><span style="color:#323232;"> at src/logic/actions/sendMessage.ts:72:7
</span><span style="color:#323232;"> at sendMessage (src/logic/actions/sendMessage.ts:59:3)
</span><span style="color:#323232;"> at Object.<anonymous> (tests/logic/actions/sendMessage.test.ts:44:3)
</span><span style="color:#323232;">
</span>
…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)
Add comment