Skip to content

Commit

Permalink
lib: test for fetch mock without prior invocation
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
YCChenVictor committed Apr 11, 2024
1 parent 5c6a889 commit 7937b69
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions test/parallel/test-fetch-mock.js
Original file line number Diff line number Diff line change
@@ -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();
});

0 comments on commit 7937b69

Please sign in to comment.