From 7937b695368badd5cc498eec8104508bf910f44c Mon Sep 17 00:00:00 2001 From: YCChen Date: Sun, 7 Apr 2024 20:40:11 +0800 Subject: [PATCH] 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. --- test/parallel/test-fetch-mock.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 test/parallel/test-fetch-mock.js diff --git a/test/parallel/test-fetch-mock.js b/test/parallel/test-fetch-mock.js new file mode 100644 index 00000000000000..b457745f1c4c90 --- /dev/null +++ b/test/parallel/test-fetch-mock.js @@ -0,0 +1,20 @@ +'use strict'; +require('../common'); +const { mock, test } = require('node:test'); +const assert = require('node:assert'); + +test('should correctly stub globalThis.fetch', async () => { + const customFetch = async (url) => { + return { + text: async () => 'foo', + }; + }; + + mock.method(globalThis, 'fetch', customFetch); + + const response = await globalThis.fetch('some-url'); + const text = await response.text(); + + assert.strictEqual(text, 'foo'); + mock.restoreAll(); +});