@@ -149,4 +149,44 @@ describe('findPackageJSON', () => { // Throws when no arguments are provided
149
149
} ) ;
150
150
} ) ) ;
151
151
} ) ;
152
+
153
+ it ( 'should work within a loader' , async ( ) => {
154
+ const specifierBase = './packages/root-types-field' ;
155
+ const target = fixtures . fileURL ( specifierBase , 'index.js' ) ;
156
+ const foundPjsonPath = path . toNamespacedPath ( fixtures . path ( specifierBase , 'package.json' ) ) ;
157
+ const { code, stderr, stdout } = await common . spawnPromisified ( process . execPath , [
158
+ '--no-warnings' ,
159
+ '--loader' ,
160
+ [
161
+ 'data:text/javascript,' ,
162
+ 'import fs from "node:fs";' ,
163
+ 'import module from "node:module";' ,
164
+ encodeURIComponent ( `fs.writeSync(1, module.findPackageJSON(${ JSON . stringify ( target ) } ));` ) ,
165
+ 'export const resolve = async (s, c, n) => n(s);' ,
166
+ ] . join ( '' ) ,
167
+ '--eval' ,
168
+ 'import "node:os";' , // Can be anything that triggers the resolve hook chain
169
+ ] ) ;
170
+
171
+ assert . strictEqual ( stderr , '' ) ;
172
+ assert . ok ( stdout . includes ( foundPjsonPath ) , stdout ) ;
173
+ assert . strictEqual ( code , 0 ) ;
174
+ } ) ;
175
+
176
+ it ( 'should work with an async resolve hook registered' , async ( ) => {
177
+ const specifierBase = './packages/root-types-field' ;
178
+ const target = fixtures . fileURL ( specifierBase , 'index.js' ) ;
179
+ const foundPjsonPath = path . toNamespacedPath ( fixtures . path ( specifierBase , 'package.json' ) ) ;
180
+ const { code, stderr, stdout } = await common . spawnPromisified ( process . execPath , [
181
+ '--no-warnings' ,
182
+ '--loader' ,
183
+ 'data:text/javascript,export const resolve = async (s, c, n) => n(s);' ,
184
+ '--print' ,
185
+ `require("node:module").findPackageJSON(${ JSON . stringify ( target ) } )` ,
186
+ ] ) ;
187
+
188
+ assert . strictEqual ( stderr , '' ) ;
189
+ assert . ok ( stdout . includes ( foundPjsonPath ) , stdout ) ;
190
+ assert . strictEqual ( code , 0 ) ;
191
+ } ) ;
152
192
} ) ;
0 commit comments