You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This was a breaking change for tests mocking global fetch, for ex:
import{describe,it}from'node:test';importassertfrom'node:assert/strict';import{fetchSomething}from'../lib/index.mjs';describe('my test suite',()=>{it('fetch stuff',async(t)=>{constmockValue={key: 'value'};constmockFetch=async()=>({json: async()=>mockValue,status: 200,});t.mock.method(global,'fetch',mockFetch);assert.deepStrictEqual(awaitfetchSomething(),mockValue);t.mock.restoreAll();});});
which produces:
✖ fetch stuff (0.894292ms)
TypeError [ERR_INVALID_ARG_VALUE]: The argument 'methodName' must be a method. Received undefined
at MockTracker.method (node:internal/test_runner/mock/mock:241:13)
at TestContext.<anonymous> (my.test.mjs:13:12)
at Test.runInAsyncScope (node:async_hooks:206:9)
at Test.run (node:internal/test_runner/test:641:25)
at Suite.processPendingSubtests (node:internal/test_runner/test:382:18)
at Test.postRun (node:internal/test_runner/test:732:19)
at Test.run (node:internal/test_runner/test:690:12)
at async Promise.all (index 0)
at async Suite.run (node:internal/test_runner/test:966:7)
at async startSubtest (node:internal/test_runner/harness:218:3) {
code: 'ERR_INVALID_ARG_VALUE'
}
to fix the break, i had to reference global.fetch before mocking it...ie:
fetch;t.mock.method(global,'fetch',mockFetch);
The text was updated successfully, but these errors were encountered:
Thank you for linking up the associated Node issues. Lots of good stuff there, including your own contributions. I think it's wise to hold off for a little while in order to see if this partly resolves itself within Node (my take after reading the discussion).
We can't do much about this until nodejs/node#52275 (created 5 days ago) is merged and then subsequently released and possibly backmerged (into version 20).
Describe the bug
With Node.js v21.7.0, it's no longer possible to stub the
fetch()
function.Edit:
To Reproduce
package.json
testcase.js
Steps to reproduce the behavior:
npm install
node testcase.js
Expected behavior
Context (please complete the following information):
Additional context
It's work with Node.js v21.6.2.
The problem seems to come from Node.js and the pull request nodejs/node#51598 (comment)
The text was updated successfully, but these errors were encountered: