1- import { structUtils } from '@yarnpkg/core' ;
1+ jest . mock ( `../sources/npmHttpUtils` , ( ) => ( {
2+ getPackageMetadata : jest . fn ( ) ,
3+ } ) ) ;
24
3- import { NpmSemverResolver } from '../sources/NpmSemverResolver' ;
5+ import { structUtils } from '@yarnpkg/core' ;
6+
7+ const { NpmSemverResolver} : typeof import ( '../sources/NpmSemverResolver' ) = require ( `../sources/NpmSemverResolver` ) ;
8+ const npmHttpUtils : typeof import ( '../sources/npmHttpUtils' ) = require ( `../sources/npmHttpUtils` ) ;
9+
10+ afterEach ( ( ) => {
11+ jest . clearAllMocks ( ) ;
12+ } ) ;
413
514describe ( `NpmSemverResolver` , ( ) => {
615 describe ( `getSatisfying` , ( ) => {
@@ -22,4 +31,57 @@ describe(`NpmSemverResolver`, () => {
2231 expect ( results . locators [ 0 ] . locatorHash ) . toEqual ( locator . locatorHash ) ;
2332 } ) ;
2433 } ) ;
34+
35+ describe ( `resolve` , ( ) => {
36+ const ident = structUtils . makeIdent ( null , `native-package` ) ;
37+ const nodeGypIdent = structUtils . makeIdent ( null , `node-gyp` ) ;
38+
39+ const makeResolveOptions = ( ) => ( {
40+ project : {
41+ configuration : {
42+ normalizeDependencyMap : ( dependencies : Map < any , any > ) => dependencies ,
43+ } ,
44+ } ,
45+ } as any ) ;
46+
47+ const mockPackageMetadata = ( scripts : Record < string , string > ) => {
48+ const getPackageMetadata = npmHttpUtils . getPackageMetadata as jest . MockedFunction < typeof npmHttpUtils . getPackageMetadata > ;
49+
50+ getPackageMetadata . mockResolvedValue ( {
51+ versions : {
52+ [ `1.0.0` ] : {
53+ name : structUtils . stringifyIdent ( ident ) ,
54+ version : `1.0.0` ,
55+ scripts,
56+ } ,
57+ } ,
58+ } as any ) ;
59+ } ;
60+
61+ it ( `shouldn't inject node-gyp when only a non-build script uses it` , async ( ) => {
62+ mockPackageMetadata ( {
63+ test : `node-gyp rebuild` ,
64+ } ) ;
65+
66+ const resolver = new NpmSemverResolver ( ) ;
67+ const locator = structUtils . makeLocator ( ident , `npm:1.0.0` ) ;
68+
69+ const pkg = await resolver . resolve ( locator , makeResolveOptions ( ) ) ;
70+
71+ expect ( pkg . dependencies . has ( nodeGypIdent . identHash ) ) . toEqual ( false ) ;
72+ } ) ;
73+
74+ it ( `should inject node-gyp when an install script uses it` , async ( ) => {
75+ mockPackageMetadata ( {
76+ install : `node-gyp rebuild` ,
77+ } ) ;
78+
79+ const resolver = new NpmSemverResolver ( ) ;
80+ const locator = structUtils . makeLocator ( ident , `npm:1.0.0` ) ;
81+
82+ const pkg = await resolver . resolve ( locator , makeResolveOptions ( ) ) ;
83+
84+ expect ( pkg . dependencies . has ( nodeGypIdent . identHash ) ) . toEqual ( true ) ;
85+ } ) ;
86+ } ) ;
2587} ) ;
0 commit comments