import { hoistDependencies , renderTree } from '../lib/hoisting' ; import { PackageLockPackage } from '../lib/types' ; type DependencyTree = PackageLockPackage ; test ( 'nonconflicting tree gets flattened' , ( ) => { const tree = pkg ( '*' , { stringutil : { version : '1.0.0' , dependencies : { leftpad : { version : '2.0.0' } , } , } , numutil : { version : '3.0.0' , dependencies : { isodd : { version : '4.0.0' } , } , } , } ) ; hoistDependencies ( tree ) ; expect ( renderTree ( tree ) ) . toEqual ( [ 'isodd=4.0.0' , 'leftpad=2.0.0' , 'numutil=3.0.0' , 'stringutil=1.0.0' , ] ) ; } ) ; test ( 'matching versions get deduped' , ( ) => { const tree = pkg ( '*' , { stringutil : { version : '1.0.0' , dependencies : { leftpad : { version : '2.0.0' } , } , } , numutil : { version : '3.0.0' , dependencies : { leftpad : { version : '2.0.0' } , isodd : { version : '4.0.0' } , } , } , } ) ; hoistDependencies ( tree ) ; expect ( renderTree ( tree ) ) . toEqual ( [ 'isodd=4.0.0' , 'leftpad=2.0.0' , 'numutil=3.0.0' , 'stringutil=1.0.0' , ] ) ; } ) ; test ( 'conflicting versions get left in place' , ( ) => { const tree = pkg ( '*' , { stringutil : { version : '1.0.0' , dependencies : { leftpad : { version : '2.0.0' } , } , } , numutil : { version : '3.0.0' , dependencies : { leftpad : { version : '5.0.0' } , isodd : { version : '4.0.0' } , } , } , } ) ; hoistDependencies ( tree ) ; expect ( renderTree ( tree ) ) . toEqual ( [ 'isodd=4.0.0' , 'leftpad=2.0.0' , 'numutil=3.0.0' , 'numutil.leftpad=5.0.0' , 'stringutil=1.0.0' , ] ) ; } ) ; test ( 'dependencies of deduped packages are not hoisted into useless positions' , ( ) => { const tree = pkg ( '*' , { stringutil : pkg ( '1.0.0' , { leftpad : pkg ( '2.0.0' , { spacemaker : pkg ( '3.0.0' ) , } ) , } ) , leftpad : pkg ( '2.0.0' , { spacemaker : pkg ( '3.0.0' ) , } ) , spacemaker : pkg ( '4.0.0' ) , } ) ; hoistDependencies ( tree ) ; expect ( renderTree ( tree ) ) . toEqual ( [ 'leftpad=2.0.0' , 'leftpad.spacemaker=3.0.0' , 'spacemaker=4.0.0' , 'stringutil=1.0.0' , ] ) ; } ) ; test ( 'dont hoist into a parent if it would cause an incorrect version there' , ( ) => { const tree = pkg ( '*' , { stringutil : pkg ( '1.0.0' , { spacemaker : pkg ( '10.0.0' ) , leftPad : pkg ( '2.0.0' , { spacemaker : pkg ( '3.0.0' ) , } ) , } ) , leftPad : pkg ( '1.0.0' ) , } ) ; hoistDependencies ( tree ) ; expect ( renderTree ( tree ) ) . toEqual ( [ 'leftPad=1.0.0' , 'spacemaker=10.0.0' , 'stringutil=1.0.0' , 'stringutil.leftPad=2.0.0' , 'stringutil.leftPad.spacemaker=3.0.0' , ] ) ; } ) ; test ( 'order of hoisting shouldnt produce a broken situation' , ( ) => { const tree = pkg ( '*' , { stringutil : pkg ( '1.0.0' , { wrapper : pkg ( '100.0.0' , { leftPad : pkg ( '2.0.0' , { spacemaker : pkg ( '3.0.0' ) , } ) , } ) , spacemaker : pkg ( '4.0.0' ) , } ) , } ) ; hoistDependencies ( tree ) ; expect ( renderTree ( tree ) ) . toEqual ( [ 'leftPad=2.0.0' , 'leftPad.spacemaker=3.0.0' , 'spacemaker=4.0.0' , 'stringutil=1.0.0' , 'wrapper=100.0.0' , ] ) ; } ) ; function pkg ( version , dependencies ? ) { return { version , ... dependencies ? { dependencies } : undefined , } ; }