@@ -260,4 +260,67 @@ describe("getPackageInfo", () => {
260260
261261 expect ( result ) . toEqual ( packageData ) ;
262262 } ) ;
263+
264+ it ( "uses yarn npm info when packageManager field specifies yarn berry" , async ( ) => {
265+ const cwd = await testdir ( {
266+ "package.json" : JSON . stringify ( {
267+ private : true ,
268+ packageManager :
"[email protected] " , 269+ } ) ,
270+ } ) ;
271+
272+ mockSpawn . mockImplementation ( ( ( cmd : string , args : string [ ] ) =>
273+ Promise . resolve (
274+ cmd === "yarn" && args ?. [ 0 ] === "npm" && args ?. [ 1 ] === "info"
275+ ? spawnResult (
276+ JSON . stringify ( {
277+ name : "pkg-a" ,
278+ version : "1.0.0" ,
279+ versions : [ "1.0.0" ] ,
280+ } )
281+ )
282+ : spawnResult ( "" , 1 )
283+ ) ) as any ) ;
284+
285+ const result = await getPackageInfo (
286+ { name : "pkg-a" , version : "1.0.0" } ,
287+ cwd
288+ ) ;
289+
290+ expect ( result ) . toMatchObject ( { name : "pkg-a" } ) ;
291+ expect (
292+ mockSpawn . mock . calls . some (
293+ ( [ cmd , args ] ) =>
294+ cmd === "yarn" && args ?. [ 0 ] === "npm" && args ?. [ 1 ] === "info"
295+ )
296+ ) . toBe ( true ) ;
297+ expect (
298+ mockSpawn . mock . calls . some (
299+ ( [ cmd , args ] ) => cmd === "npm" && args ?. [ 0 ] === "info"
300+ )
301+ ) . toBe ( false ) ;
302+ } ) ;
303+
304+ it ( "throws when yarn classic returns unparseable non-JSON output" , async ( ) => {
305+ // isYarnClassicError's JSON.parse throws on non-JSON output, returning false
306+ // from the catch block. The output is treated as non-empty, so jsonParse
307+ // is called next and propagates the SyntaxError.
308+ const cwd = await testdir ( {
309+ "package.json" : JSON . stringify ( {
310+ private : true ,
311+ packageManager :
"[email protected] " , 312+ } ) ,
313+ } ) ;
314+
315+ mockSpawn . mockImplementation ( ( ( cmd : string , args : string [ ] ) =>
316+ Promise . resolve (
317+ cmd === "yarn" && args ?. [ 0 ] === "info"
318+ ? spawnResult ( "error: network timeout" )
319+ : spawnResult ( "" , 1 )
320+ ) ) as any ) ;
321+
322+ await expect (
323+ getPackageInfo ( { name : "pkg-a" , version : "1.0.0" } , cwd )
324+ ) . rejects . toThrow ( SyntaxError ) ;
325+ } ) ;
263326} ) ;
0 commit comments