11import assert from 'node:assert/strict' ;
22import {
33 type Mock ,
4- after ,
54 afterEach ,
65 before ,
76 describe ,
@@ -13,7 +12,8 @@ import { fileURLToPath } from 'node:url';
1312
1413type FSAccess = typeof import ( 'node:fs/promises' ) . access ;
1514type FExists = typeof import ( './fexists.ts' ) . fexists ;
16- type MockModuleContext = ReturnType < typeof mock . module > ;
15+ type ResolveSpecifier = typeof import ( './resolve-specifier.ts' ) . resolveSpecifier ;
16+ // type MockModuleContext = ReturnType<typeof mock.module>;
1717
1818const RESOLVED_SPECIFIER_ERR = 'Resolved specifier did not match expected' ;
1919
@@ -22,21 +22,26 @@ describe('fexists', () => {
2222 const constants = { F_OK : null } ;
2323
2424 let mock__access : Mock < FSAccess > [ 'mock' ] ;
25- let mock__fs : MockModuleContext ;
25+ let mock__resolveSpecifier : Mock < ResolveSpecifier > [ 'mock' ] ;
2626
2727 before ( ( ) => {
2828 const access = mock . fn < FSAccess > ( ) ;
2929 ( { mock : mock__access } = access ) ;
30- mock__fs = mock . module ( 'node:fs/promises' , {
30+ mock . module ( 'node:fs/promises' , {
3131 namedExports : {
3232 access,
3333 constants,
3434 } ,
3535 } ) ;
36- } ) ;
3736
38- after ( ( ) => {
39- mock__fs . restore ( ) ;
37+ const resolveSpecifier = mock . fn < ResolveSpecifier > ( ) ;
38+ ( { mock : mock__resolveSpecifier } = resolveSpecifier ) ;
39+ mock . module ( './resolve-specifier.ts' , {
40+ namedExports : {
41+ resolveSpecifier,
42+ } ,
43+ } ) ;
44+ mock__resolveSpecifier . mockImplementation ( function MOCK__resolveSpecifier ( pp , specifier ) { return specifier } ) ;
4045 } ) ;
4146
4247 describe ( 'when the file exists' , ( ) => {
@@ -52,35 +57,34 @@ describe('fexists', () => {
5257 mock__access . resetCalls ( ) ;
5358 } ) ;
5459
55- after ( ( ) => {
56- mock__fs . restore ( ) ;
57- } ) ;
58-
5960 it ( 'should return `true` for a bare specifier' , async ( ) => {
61+ const specifier = 'foo' ;
6062 const parentUrl = fileURLToPath ( import . meta. resolve ( './fixtures/e2e/test.js' ) ) ;
6163
62- assert . equal ( await fexists ( parentUrl , 'foo' ) , true ) ;
64+ assert . equal ( await fexists ( parentUrl , specifier ) , true ) ;
6365 assert . equal (
6466 mock__access . calls [ 0 ] . arguments [ 0 ] ,
65- fileURLToPath ( import . meta . resolve ( './fixtures/e2e/node_modules/foo/foo.js' ) ) ,
67+ specifier ,
6668 RESOLVED_SPECIFIER_ERR ,
6769 ) ;
6870 } ) ;
6971
7072 it ( 'should return `true` for a relative specifier' , async ( ) => {
71- assert . equal ( await fexists ( parentPath , 'exists.js' ) , true ) ;
73+ const specifier = 'exists.js' ;
74+ assert . equal ( await fexists ( parentPath , specifier ) , true ) ;
7275 assert . equal (
7376 mock__access . calls [ 0 ] . arguments [ 0 ] ,
74- '/tmp/exists.js' ,
77+ specifier ,
7578 RESOLVED_SPECIFIER_ERR ,
7679 ) ;
7780 } ) ;
7881
7982 it ( 'should return `true` for specifier with a query parameter' , async ( ) => {
80- assert . equal ( await fexists ( parentPath , 'exists.js?v=1' ) , true ) ;
83+ const specifier = 'exists.js?v=1' ;
84+ assert . equal ( await fexists ( parentPath , specifier ) , true ) ;
8185 assert . equal (
8286 mock__access . calls [ 0 ] . arguments [ 0 ] ,
83- '/tmp/exists.js' ,
87+ specifier ,
8488 RESOLVED_SPECIFIER_ERR ,
8589 ) ;
8690 } ) ;
@@ -119,42 +123,42 @@ describe('fexists', () => {
119123 mock__access . resetCalls ( ) ;
120124 } ) ;
121125
122- after ( ( ) => {
123- mock__fs . restore ( ) ;
124- } ) ;
125-
126126 it ( 'should return `false` for a relative specifier' , async ( ) => {
127- assert . equal ( await fexists ( parentPath , 'noexists.js' ) , false ) ;
127+ const specifier = 'noexists.js' ;
128+ assert . equal ( await fexists ( parentPath , specifier ) , false ) ;
128129 assert . equal (
129130 mock__access . calls [ 0 ] . arguments [ 0 ] ,
130- '/tmp/noexists.js' ,
131+ specifier ,
131132 RESOLVED_SPECIFIER_ERR ,
132133 ) ;
133134 } ) ;
134135
135136 it ( 'should return `false` for a relative specifier' , async ( ) => {
136- assert . equal ( await fexists ( parentPath , 'noexists.js?v=1' ) , false ) ;
137+ const specifier = 'noexists.js?v=1' ;
138+ assert . equal ( await fexists ( parentPath , specifier ) , false ) ;
137139 assert . equal (
138140 mock__access . calls [ 0 ] . arguments [ 0 ] ,
139- '/tmp/noexists.js' ,
141+ specifier ,
140142 RESOLVED_SPECIFIER_ERR ,
141143 ) ;
142144 } ) ;
143145
144146 it ( 'should return `false` for an absolute specifier' , async ( ) => {
145- assert . equal ( await fexists ( parentPath , '/tmp/foo/noexists.js' ) , false ) ;
147+ const specifier = '/tmp/foo/noexists.js' ;
148+ assert . equal ( await fexists ( parentPath , specifier ) , false ) ;
146149 assert . equal (
147150 mock__access . calls [ 0 ] . arguments [ 0 ] ,
148- '/tmp/foo/noexists.js' ,
151+ specifier ,
149152 RESOLVED_SPECIFIER_ERR ,
150153 ) ;
151154 } ) ;
152155
153156 it ( 'should return `false` for a URL specifier' , async ( ) => {
154- assert . equal ( await fexists ( parentPath , 'file://localhost/foo/noexists.js' ) , false ) ;
157+ const specifier = 'file://localhost/foo/noexists.js' ;
158+ assert . equal ( await fexists ( parentPath , specifier ) , false ) ;
155159 assert . equal (
156160 mock__access . calls [ 0 ] . arguments [ 0 ] ,
157- 'file://localhost/foo/noexists.js' ,
161+ specifier ,
158162 RESOLVED_SPECIFIER_ERR ,
159163 ) ;
160164 } ) ;
0 commit comments