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 2eddeda
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions test/parallel/test-fetch-mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

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

View workflow job for this annotation

GitHub Actions / test-linux

--- stderr --- node:assert:173 throw err; ^ AssertionError [ERR_ASSERTION]: Unexpected global(s) found: fetch at process.<anonymous> (/home/runner/work/node/node/test/common/index.js:400:14) at process.emit (node:events:520:28) { generatedMessage: false, code: 'ERR_ASSERTION', actual: undefined, expected: undefined, operator: 'fail' } Node.js v22.0.0-pre --- stdout --- โœ” should correctly stub globalThis.fetch (1.590596ms) ::debug::starting to run should correctly stub globalThis.fetch ::debug::completed running should correctly stub globalThis.fetch โ„น tests 1 โ„น suites 0 โ„น pass 1 โ„น fail 0 โ„น cancelled 0 โ„น skipped 0 โ„น todo 0 โ„น duration_ms 56.067484 ::group::Test results (1 passed, 0 failed) ::notice::Total Tests: 1 Suites ๐Ÿ“‚: 0 Passed โœ…: 1 Failed โŒ: 0 Canceled ๐Ÿšซ: 0 Skipped โญ๏ธ: 0 Todo ๐Ÿ“: 0 Duration ๐Ÿ•: 56.067ms ::endgroup:: Command: out/Release/node --test-reporter=spec --test-reporter-destination=stdout --test-reporter=./tools/github_reporter/index.js --test-reporter-destination=stdout /home/runner/work/node/node/test/parallel/test-fetch-mock.js

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

View workflow job for this annotation

GitHub Actions / test-asan

--- stderr --- node:assert:173 throw err; ^ AssertionError [ERR_ASSERTION]: Unexpected global(s) found: fetch at process.<anonymous> (/home/runner/work/node/node/test/common/index.js:400:14) at process.emit (node:events:520:28) { generatedMessage: false, code: 'ERR_ASSERTION', actual: undefined, expected: undefined, operator: 'fail' } Node.js v22.0.0-pre --- stdout --- โœ” should correctly stub globalThis.fetch (8.027095ms) ::debug::starting to run should correctly stub globalThis.fetch ::debug::completed running should correctly stub globalThis.fetch โ„น tests 1 โ„น suites 0 โ„น pass 1 โ„น fail 0 โ„น cancelled 0 โ„น skipped 0 โ„น todo 0 โ„น duration_ms 225.312933 ::group::Test results (1 passed, 0 failed) ::notice::Total Tests: 1 Suites ๐Ÿ“‚: 0 Passed โœ…: 1 Failed โŒ: 0 Canceled ๐Ÿšซ: 0 Skipped โญ๏ธ: 0 Todo ๐Ÿ“: 0 Duration ๐Ÿ•: 225.313ms ::endgroup:: Command: out/Release/node --test-reporter=spec --test-reporter-destination=stdout --test-reporter=./tools/github_reporter/index.js --test-reporter-destination=stdout /home/runner/work/node/node/test/parallel/test-fetch-mock.js

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

View workflow job for this annotation

GitHub Actions / test-macOS

--- stderr --- node:assert:173 throw err; ^ AssertionError [ERR_ASSERTION]: Unexpected global(s) found: fetch at process.<anonymous> (/Users/runner/work/node/node/test/common/index.js:400:14) at process.emit (node:events:520:28) { generatedMessage: false, code: 'ERR_ASSERTION', actual: undefined, expected: undefined, operator: 'fail' } Node.js v22.0.0-pre --- stdout --- โœ” should correctly stub globalThis.fetch (0.741625ms) ::debug::starting to run should correctly stub globalThis.fetch ::debug::completed running should correctly stub globalThis.fetch โ„น tests 1 โ„น suites 0 โ„น pass 1 โ„น fail 0 โ„น cancelled 0 โ„น skipped 0 โ„น todo 0 โ„น duration_ms 30.790083 ::group::Test results (1 passed, 0 failed) ::notice::Total Tests: 1 Suites ๐Ÿ“‚: 0 Passed โœ…: 1 Failed โŒ: 0 Canceled ๐Ÿšซ: 0 Skipped โญ๏ธ: 0 Todo ๐Ÿ“: 0 Duration ๐Ÿ•: 30.790ms ::endgroup:: Command: out/Release/node --test-reporter=spec --test-reporter-destination=stdout --test-reporter=./tools/github_reporter/index.js --test-reporter-destination=stdout /Users/runner/work/node/node/test/parallel/test-fetch-mock.js

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

View workflow job for this annotation

GitHub Actions / test-ubsan

--- stderr --- node:assert:173 throw err; ^ AssertionError [ERR_ASSERTION]: Unexpected global(s) found: fetch at process.<anonymous> (/home/runner/work/node/node/test/common/index.js:400:14) at process.emit (node:events:520:28) { generatedMessage: false, code: 'ERR_ASSERTION', actual: undefined, expected: undefined, operator: 'fail' } Node.js v22.0.0-pre --- stdout --- โœ” should correctly stub globalThis.fetch (4.043589ms) ::debug::starting to run should correctly stub globalThis.fetch ::debug::completed running should correctly stub globalThis.fetch โ„น tests 1 โ„น suites 0 โ„น pass 1 โ„น fail 0 โ„น cancelled 0 โ„น skipped 0 โ„น todo 0 โ„น duration_ms 184.19835 ::group::Test results (1 passed, 0 failed) ::notice::Total Tests: 1 Suites ๐Ÿ“‚: 0 Passed โœ…: 1 Failed โŒ: 0 Canceled ๐Ÿšซ: 0 Skipped โญ๏ธ: 0 Todo ๐Ÿ“: 0 Duration ๐Ÿ•: 184.198ms ::endgroup:: Command: out/Release/node --test-reporter=spec --test-reporter-destination=stdout --test-reporter=./tools/github_reporter/index.js --test-reporter-destination=stdout /home/runner/work/node/node/test/parallel/test-fetch-mock.js
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');
});

0 comments on commit 2eddeda

Please sign in to comment.