@@ -349,5 +349,107 @@ describe('packExternalModules', () => {
349349 expect ( childProcessMock . exec ) . to . have . been . calledOnce ,
350350 ] ) ) ;
351351 } ) ;
352+
353+ it ( 'should install external modules when forced' , ( ) => {
354+ const expectedPackageJSON = {
355+ dependencies : {
356+ '@scoped/vendor' : '1.0.0' ,
357+ uuid : '^5.4.1' ,
358+ bluebird : '^3.4.0' ,
359+ pg : '^4.3.5'
360+ }
361+ } ;
362+ serverless . service . custom = {
363+ webpackIncludeModules : {
364+ forceInclude : [ 'pg' ]
365+ }
366+ } ;
367+ module . webpackOutputPath = 'outputPath' ;
368+ npmMock . install . returns ( BbPromise . resolve ( ) ) ;
369+ fsExtraMock . copy . yields ( ) ;
370+ childProcessMock . exec . onFirstCall ( ) . yields ( null , '{}' , '' ) ;
371+ childProcessMock . exec . onSecondCall ( ) . yields ( ) ;
372+ return expect ( module . packExternalModules ( stats ) ) . to . be . fulfilled
373+ . then ( ( ) => BbPromise . all ( [
374+ // npm install should have been called with all externals from the package mock
375+ expect ( npmMock . install ) . to . have . been . calledOnce ,
376+ expect ( npmMock . install ) . to . have . been . calledWithExactly ( [
377+ 378+ 'uuid@^5.4.1' ,
379+ 'bluebird@^3.4.0' ,
380+ 'pg@^4.3.5'
381+ ] ,
382+ {
383+ cwd : path . join ( 'outputPath' , 'dependencies' ) ,
384+ maxBuffer : 204800 ,
385+ save : true
386+ } ) ,
387+ // The module package JSON and the composite one should have been stored
388+ expect ( writeFileSyncStub ) . to . have . been . calledTwice ,
389+ expect ( writeFileSyncStub . firstCall . args [ 1 ] ) . to . equal ( '{}' ) ,
390+ expect ( writeFileSyncStub . secondCall . args [ 1 ] ) . to . equal ( JSON . stringify ( expectedPackageJSON , null , 2 ) ) ,
391+ // The modules should have been copied
392+ expect ( fsExtraMock . copy ) . to . have . been . calledOnce ,
393+ // npm ls and npm prune should have been called
394+ expect ( childProcessMock . exec ) . to . have . been . calledTwice ,
395+ expect ( childProcessMock . exec . firstCall ) . to . have . been . calledWith (
396+ 'npm ls -prod -json -depth=1'
397+ ) ,
398+ expect ( childProcessMock . exec . secondCall ) . to . have . been . calledWith (
399+ 'npm prune'
400+ )
401+ ] ) ) ;
402+ } ) ;
403+
404+ it ( 'should add forced external modules without version when not in production dependencies' , ( ) => {
405+ const expectedPackageJSON = {
406+ dependencies : {
407+ '@scoped/vendor' : '1.0.0' ,
408+ uuid : '^5.4.1' ,
409+ bluebird : '^3.4.0' ,
410+ 'not-in-prod-deps' : ''
411+ }
412+ } ;
413+ serverless . service . custom = {
414+ webpackIncludeModules : {
415+ forceInclude : [ 'not-in-prod-deps' ]
416+ }
417+ } ;
418+ module . webpackOutputPath = 'outputPath' ;
419+ npmMock . install . returns ( BbPromise . resolve ( ) ) ;
420+ fsExtraMock . copy . yields ( ) ;
421+ childProcessMock . exec . onFirstCall ( ) . yields ( null , '{}' , '' ) ;
422+ childProcessMock . exec . onSecondCall ( ) . yields ( ) ;
423+ return expect ( module . packExternalModules ( stats ) ) . to . be . fulfilled
424+ . then ( ( ) => BbPromise . all ( [
425+ // npm install should have been called with all externals from the package mock
426+ expect ( npmMock . install ) . to . have . been . calledOnce ,
427+ expect ( npmMock . install ) . to . have . been . calledWithExactly ( [
428+ 429+ 'uuid@^5.4.1' ,
430+ 'bluebird@^3.4.0' ,
431+ 'not-in-prod-deps'
432+ ] ,
433+ {
434+ cwd : path . join ( 'outputPath' , 'dependencies' ) ,
435+ maxBuffer : 204800 ,
436+ save : true
437+ } ) ,
438+ // The module package JSON and the composite one should have been stored
439+ expect ( writeFileSyncStub ) . to . have . been . calledTwice ,
440+ expect ( writeFileSyncStub . firstCall . args [ 1 ] ) . to . equal ( '{}' ) ,
441+ expect ( writeFileSyncStub . secondCall . args [ 1 ] ) . to . equal ( JSON . stringify ( expectedPackageJSON , null , 2 ) ) ,
442+ // The modules should have been copied
443+ expect ( fsExtraMock . copy ) . to . have . been . calledOnce ,
444+ // npm ls and npm prune should have been called
445+ expect ( childProcessMock . exec ) . to . have . been . calledTwice ,
446+ expect ( childProcessMock . exec . firstCall ) . to . have . been . calledWith (
447+ 'npm ls -prod -json -depth=1'
448+ ) ,
449+ expect ( childProcessMock . exec . secondCall ) . to . have . been . calledWith (
450+ 'npm prune'
451+ )
452+ ] ) ) ;
453+ } ) ;
352454 } ) ;
353455} ) ;
0 commit comments