Skip to content

Commit 2eddeda

Browse files
committed
lib: test for fetch mock without prior invocation
The purpose of this test is to verify the correct behavior of stubbing the globalThis.fetch method with custom implementation. It checks whether the stubbed fetch function correctly returns the expected response without calling fetch iteself first.
1 parent 5c6a889 commit 2eddeda

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

test/parallel/test-fetch-mock.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
require('../common');
3+
const { mock, test } = require('node:test');
4+
const assert = require('node:assert');
5+
6+
test('should correctly stub globalThis.fetch', async () => {
7+
const customFetch = async (url) => {
8+
return {
9+
text: async () => 'foo',
10+
};
11+
};
12+
13+
mock.method(globalThis, 'fetch', customFetch);
14+
15+
const response = await globalThis.fetch('some-url');
16+
const text = await response.text();
17+
18+
assert.strictEqual(text, 'foo');
19+
});

0 commit comments

Comments
 (0)