1- import * as fs from 'fs' ;
2- import * as path from 'path' ;
1+ import { bockfs } from '@aws-cdk/cdk-build-tools' ;
32import { Template , Match } from '../../assertions' ;
43import { Vpc } from '../../aws-ec2' ;
54import { CodeConfig , Runtime } from '../../aws-lambda' ;
@@ -25,12 +24,45 @@ jest.mock('../lib/bundling', () => {
2524 } ;
2625} ) ;
2726
27+ const mockCallsites = jest . fn ( ) ;
28+ jest . mock ( '../lib/util' , ( ) => ( {
29+ ...jest . requireActual ( '../lib/util' ) ,
30+ callsites : ( ) => mockCallsites ( ) ,
31+ } ) ) ;
32+
2833let stack : Stack ;
2934beforeEach ( ( ) => {
3035 stack = new Stack ( ) ;
3136 jest . clearAllMocks ( ) ;
3237} ) ;
3338
39+ // We MUST use a fake file system here.
40+ // Using the real filesystem causes the tests to be flaky and fail at random.
41+ // This way we are guaranteed to have the fake files setup on each test run.
42+ bockfs ( {
43+ '/home/project/package.json' : '{}' ,
44+ '/home/project/package-lock.json' : '{}' ,
45+ '/home/project/handler.tsx' : '// nothing' ,
46+ '/home/project/function.test.handler1.ts' : '// nothing' ,
47+ '/home/project/function.test.handler2.js' : '// nothing' ,
48+ '/home/project/function.test.handler3.mjs' : '// nothing' ,
49+ '/home/project/function.test.handler4.mts' : '// nothing' ,
50+ '/home/project/function.test.handler5.cts' : '// nothing' ,
51+ '/home/project/function.test.handler6.cjs' : '// nothing' ,
52+ '/home/project/aws-lambda-nodejs/lib/index.ts' : '// nothing' ,
53+ } ) ;
54+ const bockPath = bockfs . workingDirectory ( '/home/project' ) ;
55+
56+ // pretend the calling file is in a fake file path
57+ mockCallsites . mockImplementation ( ( ) => [
58+ { getFunctionName : ( ) => 'NodejsFunction' } ,
59+ { getFileName : ( ) => bockPath `function.test.ts` } ,
60+ ] ) ;
61+
62+ afterAll ( ( ) => {
63+ bockfs . restore ( ) ;
64+ } ) ;
65+
3466test ( 'NodejsFunction with .ts handler' , ( ) => {
3567 // WHEN
3668 new NodejsFunction ( stack , 'handler1' ) ;
@@ -151,15 +183,11 @@ test('throws when entry is not js/ts', () => {
151183} ) ;
152184
153185test ( 'accepts tsx' , ( ) => {
154- const entry = path . join ( __dirname , 'handler.tsx' ) ;
155-
156- fs . symlinkSync ( path . join ( __dirname , 'function.test.handler1.ts' ) , entry ) ;
186+ const entry = bockPath `handler.tsx` ;
157187
158188 expect ( ( ) => new NodejsFunction ( stack , 'Fn' , {
159189 entry,
160190 } ) ) . not . toThrow ( ) ;
161-
162- fs . unlinkSync ( entry ) ;
163191} ) ;
164192
165193test ( 'throws when entry does not exist' , ( ) => {
@@ -196,7 +224,7 @@ test('resolves depsLockFilePath to an absolute path', () => {
196224 } ) ;
197225
198226 expect ( Bundling . bundle ) . toHaveBeenCalledWith ( expect . objectContaining ( {
199- depsLockFilePath : expect . stringMatching ( / a w s - c d k - l i b \/ p a c k a g e .j s o n $ / ) ,
227+ depsLockFilePath : bockPath `/home/project/ package.json` ,
200228 } ) ) ;
201229} ) ;
202230
@@ -207,7 +235,7 @@ test('resolves entry to an absolute path', () => {
207235 } ) ;
208236
209237 expect ( Bundling . bundle ) . toHaveBeenCalledWith ( expect . objectContaining ( {
210- entry : expect . stringMatching ( / a w s - c d k - l i b \/ a w s - l a m b d a - n o d e j s \ /l i b \ /i n d e x .t s $ / ) ,
238+ entry : bockPath `/home/project/ aws-lambda-nodejs/lib/index.ts` ,
211239 } ) ) ;
212240} ) ;
213241
0 commit comments