@@ -30,6 +30,7 @@ describe('packExternalModules', () => {
3030 let fsExtraMock ;
3131 // Serverless stubs
3232 let writeFileSyncStub ;
33+ let readFileSyncStub ;
3334
3435 before ( ( ) => {
3536 sandbox = sinon . sandbox . create ( ) ;
@@ -60,6 +61,7 @@ describe('packExternalModules', () => {
6061 _ . set ( serverless , 'service.service' , 'test-service' ) ;
6162
6263 writeFileSyncStub = sandbox . stub ( serverless . utils , 'writeFileSync' ) ;
64+ readFileSyncStub = sandbox . stub ( serverless . utils , 'readFileSync' ) ;
6365 _ . set ( serverless , 'service.custom.webpackIncludeModules' , true ) ;
6466
6567 module = _ . assign ( {
@@ -73,6 +75,7 @@ describe('packExternalModules', () => {
7375 afterEach ( ( ) => {
7476 // Reset all counters and restore all stubbed functions
7577 writeFileSyncStub . reset ( ) ;
78+ readFileSyncStub . reset ( ) ;
7679 childProcessMock . exec . reset ( ) ;
7780 fsExtraMock . pathExists . reset ( ) ;
7881 fsExtraMock . copy . reset ( ) ;
@@ -258,6 +261,7 @@ describe('packExternalModules', () => {
258261 } ) ;
259262
260263 it ( 'should rebase file references' , ( ) => {
264+ const expectedLocalModule = 'file:../../locals/../../mymodule' ;
261265 const expectedCompositePackageJSON = {
262266 name : 'test-service' ,
263267 version : '1.0.0' ,
@@ -274,14 +278,52 @@ describe('packExternalModules', () => {
274278 dependencies : {
275279 '@scoped/vendor' : '1.0.0' ,
276280 uuid : '^5.4.1' ,
277- localmodule : 'file:../../locals/../../mymodule' ,
281+ localmodule : expectedLocalModule ,
278282 bluebird : '^3.4.0'
279283 }
280284 } ;
281285
286+ const fakePackageLockJSON = {
287+ name : 'test-service' ,
288+ version : '1.0.0' ,
289+ description : 'Packaged externals for test-service' ,
290+ private : true ,
291+ dependencies : {
292+ '@scoped/vendor' : '1.0.0' ,
293+ uuid : {
294+ version : '^5.4.1'
295+ } ,
296+ bluebird : {
297+ version : '^3.4.0'
298+ } ,
299+ localmodule : {
300+ version : 'file:../../mymodule'
301+ }
302+ }
303+ } ;
304+ const expectedPackageLockJSON = {
305+ name : 'test-service' ,
306+ version : '1.0.0' ,
307+ description : 'Packaged externals for test-service' ,
308+ private : true ,
309+ dependencies : {
310+ '@scoped/vendor' : '1.0.0' ,
311+ uuid : {
312+ version : '^5.4.1'
313+ } ,
314+ bluebird : {
315+ version : '^3.4.0'
316+ } ,
317+ localmodule : {
318+ version : expectedLocalModule
319+ }
320+ }
321+ } ;
322+
282323 _ . set ( serverless , 'service.custom.webpackIncludeModules.packagePath' , path . join ( 'locals' , 'package.json' ) ) ;
283324 module . webpackOutputPath = 'outputPath' ;
284- fsExtraMock . pathExists . yields ( null , false ) ;
325+ readFileSyncStub . returns ( fakePackageLockJSON ) ;
326+ fsExtraMock . pathExists . yields ( null , true ) ;
285327 fsExtraMock . copy . yields ( ) ;
286328 childProcessMock . exec . onFirstCall ( ) . yields ( null , '{}' , '' ) ;
287329 childProcessMock . exec . onSecondCall ( ) . yields ( null , '' , '' ) ;
@@ -294,9 +336,10 @@ describe('packExternalModules', () => {
294336 return expect ( module . packExternalModules ( ) ) . to . be . fulfilled
295337 . then ( ( ) => BbPromise . all ( [
296338 // The module package JSON and the composite one should have been stored
297- expect ( writeFileSyncStub ) . to . have . been . calledTwice ,
339+ expect ( writeFileSyncStub ) . to . have . been . calledThrice ,
298340 expect ( writeFileSyncStub . firstCall . args [ 1 ] ) . to . equal ( JSON . stringify ( expectedCompositePackageJSON , null , 2 ) ) ,
299- expect ( writeFileSyncStub . secondCall . args [ 1 ] ) . to . equal ( JSON . stringify ( expectedPackageJSON , null , 2 ) ) ,
341+ expect ( writeFileSyncStub . secondCall . args [ 1 ] ) . to . equal ( JSON . stringify ( expectedPackageLockJSON , null , 2 ) ) ,
342+ expect ( writeFileSyncStub . thirdCall . args [ 1 ] ) . to . equal ( JSON . stringify ( expectedPackageJSON , null , 2 ) ) ,
300343 // The modules should have been copied
301344 expect ( fsExtraMock . copy ) . to . have . been . calledOnce ,
302345 // npm ls and npm prune should have been called
@@ -702,10 +745,7 @@ describe('packExternalModules', () => {
702745 expect ( writeFileSyncStub . firstCall . args [ 1 ] ) . to . equal ( JSON . stringify ( expectedCompositePackageJSON , null , 2 ) ) ,
703746 expect ( writeFileSyncStub . secondCall . args [ 1 ] ) . to . equal ( JSON . stringify ( expectedPackageJSON , null , 2 ) ) ,
704747 // The modules should have been copied
705- expect ( fsExtraMock . copy ) . to . have . been . calledTwice ,
706- expect ( fsExtraMock . copy . firstCall ) . to . have . been . calledWith (
707- sinon . match ( / p a c k a g e - l o c k .j s o n $ / )
708- ) ,
748+ expect ( fsExtraMock . copy ) . to . have . been . calledOnce ,
709749 // npm ls and npm prune should have been called
710750 expect ( childProcessMock . exec ) . to . have . been . calledThrice ,
711751 expect ( childProcessMock . exec . firstCall ) . to . have . been . calledWith (
@@ -741,9 +781,9 @@ describe('packExternalModules', () => {
741781 } ;
742782
743783 module . webpackOutputPath = 'outputPath' ;
784+ readFileSyncStub . throws ( new Error ( 'Failed to read package-lock.json' ) ) ;
744785 fsExtraMock . pathExists . yields ( null , true ) ;
745- fsExtraMock . copy . onFirstCall ( ) . yields ( new Error ( 'Failed to read package-lock.json' ) ) ;
746- fsExtraMock . copy . onSecondCall ( ) . yields ( ) ;
786+ fsExtraMock . copy . onFirstCall ( ) . yields ( ) ;
747787 childProcessMock . exec . onFirstCall ( ) . yields ( null , '{}' , '' ) ;
748788 childProcessMock . exec . onSecondCall ( ) . yields ( null , '' , '' ) ;
749789 childProcessMock . exec . onThirdCall ( ) . yields ( ) ;
@@ -755,10 +795,7 @@ describe('packExternalModules', () => {
755795 expect ( writeFileSyncStub . firstCall . args [ 1 ] ) . to . equal ( JSON . stringify ( expectedCompositePackageJSON , null , 2 ) ) ,
756796 expect ( writeFileSyncStub . secondCall . args [ 1 ] ) . to . equal ( JSON . stringify ( expectedPackageJSON , null , 2 ) ) ,
757797 // The modules should have been copied
758- expect ( fsExtraMock . copy ) . to . have . been . calledTwice ,
759- expect ( fsExtraMock . copy . firstCall ) . to . have . been . calledWith (
760- sinon . match ( / p a c k a g e - l o c k .j s o n $ / )
761- ) ,
798+ expect ( fsExtraMock . copy ) . to . have . been . calledOnce ,
762799 // npm ls and npm prune should have been called
763800 expect ( childProcessMock . exec ) . to . have . been . calledThrice ,
764801 expect ( childProcessMock . exec . firstCall ) . to . have . been . calledWith (
0 commit comments