@@ -2,32 +2,84 @@ jest.mock('esbuild');
2
2
3
3
import '@aws-cdk/assert/jest' ;
4
4
5
+ import { Runtime , RuntimeFamily } from '@aws-cdk/aws-lambda' ;
5
6
import { Stack } from '@aws-cdk/core' ;
6
7
import { buildSync } from 'esbuild' ;
7
8
import mockfs from 'mock-fs' ;
9
+ import path from 'path' ;
8
10
9
11
import { NodejsFunction } from '../src' ;
10
12
13
+
11
14
describe ( 'NodejsFunction tests' , ( ) => {
12
- beforeAll ( ( ) => {
13
- mockfs ( {
14
- 'index.ts' : '' ,
15
- 'package-lock.json' : '' ,
16
- '.build' : { }
15
+ describe ( 'with valid folder structure' , ( ) => {
16
+ beforeAll ( ( ) => {
17
+ mockfs ( {
18
+ 'index.ts' : '' ,
19
+ 'source/index.ts' : '' ,
20
+ 'main.ts' : '' ,
21
+ 'a/b/c.ts' : '' ,
22
+ 'src' : {
23
+ 'index.ts' : '' ,
24
+ '.build' : { }
25
+ } ,
26
+ 'package-lock.json' : '' ,
27
+ '.build' : { }
28
+ } ) ;
17
29
} ) ;
18
- } ) ;
19
30
20
- it ( 'Should not class constructor be thrown' , async ( ) => {
21
- const stack = new Stack ( ) ;
22
- const factory = ( ) => new NodejsFunction ( stack , 'lambda-function' , { } ) ;
23
- expect ( factory ) . not . toThrow ( ) ;
24
- expect ( buildSync ) . toBeCalledTimes ( 1 ) ;
25
- expect ( stack ) . toHaveResource ( 'AWS::Lambda::Function' , {
26
- Handler : 'index.handler' ,
31
+ beforeEach ( ( ) => {
32
+ ( buildSync as jest . Mock ) . mockReset ( ) ;
33
+ } ) ;
34
+
35
+ it . each ( Runtime . ALL . filter ( r => r . family !== RuntimeFamily . NODEJS ) ) ( 'Should be thrown due to not supported runtime' , ( runtime ) => {
36
+ const constructor = ( ) => new NodejsFunction ( new Stack ( ) , 'lambda-function' , { runtime } ) ;
37
+ expect ( constructor ) . toThrowError ( / ^ O n l y ` N O D E J S ` r u n t i m e s a r e s u p p o r t e d .$ / ) ;
38
+ } ) ;
39
+
40
+ it ( 'Should not be thrown' , ( ) => {
41
+ const stack = new Stack ( ) ;
42
+ const constructor = ( ) => new NodejsFunction ( stack , 'lambda-function' ) ;
43
+ expect ( constructor ) . not . toThrow ( ) ;
44
+ expect ( buildSync ) . toBeCalledTimes ( 1 ) ;
45
+ expect ( stack ) . toHaveResource ( 'AWS::Lambda::Function' , {
46
+ Handler : 'index.handler' ,
47
+ } ) ;
48
+ } ) ;
49
+
50
+ it . each `
51
+ handler | entry
52
+ ${ null } | ${ 'index.ts' }
53
+ ${ 'source/index.handler' } | ${ 'source/index.ts' }
54
+ ${ 'main.func' } | ${ 'main.ts' }
55
+ ${ 'a/b/c.h' } | ${ 'a/b/c.ts' }
56
+ ` ( 'Should be valid entry with default rootDir' , ( { handler, entry } ) => {
57
+ new NodejsFunction ( new Stack ( ) , 'lambda-function' , { handler } ) ;
58
+ expect ( buildSync ) . toHaveBeenCalledWith ( expect . objectContaining ( { entryPoints : [ entry ] } ) ) ;
59
+ } ) ;
60
+
61
+ it ( 'Should be valid outdir with custom rootDir' , ( ) => {
62
+ new NodejsFunction ( new Stack ( ) , 'lambda-function' , { rootDir : path . join ( __dirname , '../src' ) } ) ;
63
+ expect ( buildSync ) . toHaveBeenCalledWith ( expect . objectContaining ( { outdir : path . join ( __dirname , '../src' , '.build' ) } ) ) ;
64
+ } ) ;
65
+
66
+ afterAll ( ( ) => {
67
+ mockfs . restore ( ) ;
27
68
} ) ;
28
69
} ) ;
29
70
30
- afterAll ( ( ) => {
31
- mockfs . restore ( ) ;
71
+ describe ( 'with invalid folder structure' , ( ) => {
72
+ beforeAll ( ( ) => {
73
+ mockfs ( ) ;
74
+ } ) ;
75
+
76
+ it ( 'Should be thrown due to unrecognised root directory' , ( ) => {
77
+ const constructor = ( ) => new NodejsFunction ( new Stack ( ) , 'lambda-function' ) ;
78
+ expect ( constructor ) . toThrowError ( / ^ C a n n o t f i n d r o o t d i r e c t o r y ./ ) ;
79
+ } ) ;
80
+
81
+ afterAll ( ( ) => {
82
+ mockfs . restore ( ) ;
83
+ } ) ;
32
84
} ) ;
33
85
} ) ;
0 commit comments