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 9, 2024
1 parent 5c6a889 commit b42ee5b
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions test/parallel/test-fetch-mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

Check failure on line 1 in test/parallel/test-fetch-mock.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Mandatory module "common" must be loaded

Check failure on line 1 in test/parallel/test-fetch-mock.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Mandatory module "common" must be loaded before any other modules
const { mock, test } = require('node:test');
const assert = require('node:assert');

test("should correctly stub globalThis.fetch", async () => {

Check failure on line 5 in test/parallel/test-fetch-mock.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Strings must use singlequote
const customFetch = async (url) => {
return {
text: async () => "foo",

Check failure on line 8 in test/parallel/test-fetch-mock.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Strings must use singlequote
};
};

mock.method(globalThis, "fetch", customFetch);

Check failure on line 12 in test/parallel/test-fetch-mock.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Strings must use singlequote

const response = await globalThis.fetch("some-url");

Check failure on line 14 in test/parallel/test-fetch-mock.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Strings must use singlequote
const text = await response.text();

assert.strictEqual(text, "foo");

Check failure on line 17 in test/parallel/test-fetch-mock.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Strings must use singlequote
});

0 comments on commit b42ee5b

Please sign in to comment.