1- const { match, rejects } = require ( 'node:assert' ) ;
1+ const { deepStrictEqual , match, rejects } = require ( 'node:assert' ) ;
22const { describe, it } = require ( 'node:test' ) ;
3- const undici = require ( 'undici' ) ;
3+ const {
4+ fetch : undiciFetch ,
5+ MockAgent,
6+ setGlobalDispatcher,
7+ } = require ( 'undici' ) ;
48const schedule = require ( '../util/prod/schedule' ) ;
59const optionsParser = require ( '../util/prod/optionsParser' ) ;
6-
7- // undici's types only take its own Request class, so they don't match the
8- // global fetch's type even though it works as one
9- const undiciFetch = /** @type {typeof fetch } */ (
10- /** @type {unknown } */ ( undici . fetch )
11- ) ;
12-
13- /** @typedef {import('../util/prod/optionsParser').ParsedOptions } ParsedOptions */
10+ const { at } = require ( '@nodevu/fixtures' ) ;
11+ const scheduleSnapshot = require ( '@nodevu/fixtures/data/schedule.json' ) ;
12+ const now = require ( '@nodevu/fixtures/data/now.json' ) ;
1413
1514// checks that verify the result of data returned
1615/** @param {import('../util/prod/schedule').Schedule } data */
@@ -27,43 +26,65 @@ function check(data) {
2726 }
2827}
2928
30- // set up options object that would normally be passed to the module. the
31- // module only reads fetch and its own URL
32- const options = /** @type {ParsedOptions } */ ( {
33- fetch : globalThis . fetch ,
34- urls : {
35- schedule :
36- 'https://raw.githubusercontent.com/nodejs/Release/master/schedule.json' ,
37- } ,
38- } ) ;
29+ const url =
30+ 'https://raw.githubusercontent.com/nodejs/Release/master/schedule.json' ;
31+
32+ // undici's fetch goes through this mock instead of the network: it serves
33+ // each intercept() once and refuses any other request
34+ const agent = new MockAgent ( ) ;
35+
36+ agent . disableNetConnect ( ) ;
37+
38+ setGlobalDispatcher ( agent ) ;
39+
40+ describe ( 'under normal conditions, schedule should work' , async ( ) => {
41+ it ( 'should work with default options' , async ( t ) => {
42+ const fetch = t . mock . method ( globalThis , 'fetch' , at ( now ) . fetch ) ;
43+
44+ const data = await schedule ( {
45+ fetch : globalThis . fetch ,
46+ urls : { schedule : url } ,
47+ } ) ;
3948
40- describe ( 'under normal condiditons, schedule should work' , async ( ) => {
41- it ( 'should work with default options' , async ( ) => {
42- const data = await schedule ( options ) ;
4349 check ( data ) ;
50+ deepStrictEqual ( requested ( fetch ) , [ url ] ) ;
4451 } ) ;
4552
4653 it ( 'should work with Undici fetch' , async ( ) => {
47- options . fetch = undiciFetch ;
48- const data = await schedule ( options ) ;
54+ intercept ( ) ;
55+
56+ const data = await schedule ( {
57+ fetch : undiciFetch ,
58+ urls : { schedule : url } ,
59+ } ) ;
60+
4961 check ( data ) ;
62+ agent . assertNoPendingInterceptors ( ) ;
5063 } ) ;
5164} ) ;
5265
5366describe ( 'schedule should work with optionsParser' , async ( ) => {
54- it ( 'should work with the default output of optionsParser' , async ( ) => {
67+ it ( 'should work with the default output of optionsParser' , async ( t ) => {
68+ const fetch = t . mock . method ( globalThis , 'fetch' , at ( now ) . fetch ) ;
69+
5570 const parsedOptions = optionsParser ( { } ) ;
5671 const data = await schedule ( parsedOptions ) ;
72+
5773 check ( data ) ;
74+ deepStrictEqual ( requested ( fetch ) , [ url ] ) ;
5875 } ) ;
5976
6077 it ( 'should work with a different fetch passed to optionsParser' , async ( ) => {
78+ intercept ( ) ;
79+
6180 const parsedOptions = optionsParser ( {
6281 fetch : undiciFetch ,
6382 } ) ;
6483
6584 const data = await schedule ( parsedOptions ) ;
85+
6686 check ( data ) ;
87+ agent . assertNoPendingInterceptors ( ) ;
6788 } ) ;
6889} ) ;
6990
@@ -74,12 +95,7 @@ describe('schedule', async () => {
7495 'https://raw.githubusercontent.com/nodejs/Release/master/schedule.json' ;
7596
7697 // Act
77- const result = schedule (
78- /** @type {ParsedOptions } */ ( {
79- fetch : notFound ,
80- urls : { schedule : url } ,
81- } ) ,
82- ) ;
98+ const result = schedule ( { fetch : notFound , urls : { schedule : url } } ) ;
8399
84100 // Assert
85101 await rejects ( result , {
@@ -93,3 +109,17 @@ describe('schedule', async () => {
93109/** @type {typeof fetch } */
94110const notFound = async ( ) =>
95111 new Response ( 'Not Found' , { status : 404 , statusText : 'Not Found' } ) ;
112+
113+ // the URLs a mocked `fetch` was called with
114+ /** @param {import('node:test').Mock<typeof globalThis.fetch> } mock */
115+ function requested ( mock ) {
116+ return mock . mock . calls . map ( ( call ) => call . arguments [ 0 ] ) ;
117+ }
118+
119+ // serves the schedule.json snapshot to undici's fetch once
120+ function intercept ( ) {
121+ agent
122+ . get ( 'https://raw.githubusercontent.com' )
123+ . intercept ( { path : '/nodejs/Release/master/schedule.json' } )
124+ . reply ( 200 , scheduleSnapshot ) ;
125+ }
0 commit comments