1
- /**
2
- * Test LayerPublisherStack class
3
- *
4
- * @group e2e/layers/all
5
- */
6
1
import { join } from 'node:path' ;
7
2
import {
8
3
TestInvocationLogs ,
@@ -13,23 +8,14 @@ import {
13
8
import { TestNodejsFunction } from '@aws-lambda-powertools/testing-utils/resources/lambda' ;
14
9
import { App } from 'aws-cdk-lib' ;
15
10
import { LayerVersion } from 'aws-cdk-lib/aws-lambda' ;
11
+ import { afterAll , beforeAll , describe , expect , it , vi } from 'vitest' ;
16
12
import packageJson from '../../package.json' ;
17
- import { LayerPublisherStack } from '../../src/layer-publisher-stack' ;
13
+ import { LayerPublisherStack } from '../../src/layer-publisher-stack.js ' ;
18
14
import {
19
15
RESOURCE_NAME_PREFIX ,
20
16
SETUP_TIMEOUT ,
21
17
TEARDOWN_TIMEOUT ,
22
- } from './constants' ;
23
-
24
- jest . spyOn ( console , 'log' ) . mockImplementation ( ) ;
25
-
26
- function assertLogs (
27
- logs : TestInvocationLogs | undefined
28
- ) : asserts logs is TestInvocationLogs {
29
- if ( ! logs ) {
30
- throw new Error ( 'Function logs are not available' ) ;
31
- }
32
- }
18
+ } from './constants.js' ;
33
19
34
20
/**
35
21
* This test has two stacks:
@@ -137,70 +123,78 @@ describe('Layers E2E tests', () => {
137
123
}
138
124
} , SETUP_TIMEOUT ) ;
139
125
140
- describe . each ( cases ) (
141
- 'utilities tests for %s output format' ,
126
+ it . each ( cases ) (
127
+ 'imports and instantiates all utilities (%s)' ,
128
+ ( outputFormat ) => {
129
+ const invocationLogs = invocationLogsMap . get (
130
+ outputFormat
131
+ ) as TestInvocationLogs ;
132
+
133
+ expect ( invocationLogs . doesAnyFunctionLogsContains ( 'ERROR' ) ) . toBe ( false ) ;
134
+ }
135
+ ) ;
136
+
137
+ it . each ( cases ) (
138
+ 'emits a warning log for missing Metrics namespace (%s)' ,
142
139
( outputFormat ) => {
143
- it ( 'should have no errors in the logs, which indicates the pacakges version matches the expected one' , ( ) => {
144
- const maybeInvocationLogs = invocationLogsMap . get ( outputFormat ) ;
145
- assertLogs ( maybeInvocationLogs ) ;
146
- const invocationLogs = maybeInvocationLogs ;
147
- const logs = invocationLogs . getFunctionLogs ( 'ERROR' ) ;
148
-
149
- expect ( logs . length ) . toBe ( 0 ) ;
150
- } ) ;
151
-
152
- it ( 'should have one warning related to missing Metrics namespace' , ( ) => {
153
- const maybeInvocationLogs = invocationLogsMap . get ( outputFormat ) ;
154
- assertLogs ( maybeInvocationLogs ) ;
155
- const invocationLogs = maybeInvocationLogs ;
156
- const logs = invocationLogs . getFunctionLogs ( 'WARN' ) ;
157
-
158
- expect ( logs . length ) . toBe ( 1 ) ;
159
- expect ( logs [ 0 ] ) . toContain ( 'Namespace should be defined, default used' ) ;
160
- } ) ;
161
-
162
- it ( 'should have one info log related to coldstart metric' , ( ) => {
163
- const maybeInvocationLogs = invocationLogsMap . get ( outputFormat ) ;
164
- assertLogs ( maybeInvocationLogs ) ;
165
- const invocationLogs = maybeInvocationLogs ;
166
- const logs = invocationLogs . getFunctionLogs ( ) ;
167
-
168
- const emfLogEntry = logs . find ( ( log ) =>
169
- log . match (
170
- / { " _ a w s " : { " T i m e s t a m p " : \d + , " C l o u d W a t c h M e t r i c s " : \[ \{ " N a m e s p a c e " : " \S + " , " D i m e n s i o n s " : \[ \[ " s e r v i c e " \] \] , " M e t r i c s " : \[ \{ " N a m e " : " C o l d S t a r t " , " U n i t " : " C o u n t " \} \] \} \] } , " s e r v i c e " : " \S + " , " C o l d S t a r t " : 1 } /
171
- )
172
- ) ;
173
-
174
- expect ( emfLogEntry ) . toBeDefined ( ) ;
175
- } ) ;
176
-
177
- it ( 'should have one debug log with tracer subsegment info' , ( ) => {
178
- const maybeInvocationLogs = invocationLogsMap . get ( outputFormat ) ;
179
- assertLogs ( maybeInvocationLogs ) ;
180
- const invocationLogs = maybeInvocationLogs ;
181
- const logs = invocationLogs . getFunctionLogs ( 'DEBUG' ) ;
182
-
183
- expect ( logs . length ) . toBe ( 1 ) ;
184
- const logEntry = TestInvocationLogs . parseFunctionLog ( logs [ 0 ] ) ;
185
- expect ( logEntry . message ) . toContain ( 'subsegment' ) ;
186
- expect ( logEntry . subsegment ) . toBeDefined ( ) ;
187
- const subsegment = JSON . parse ( logEntry . subsegment as string ) ;
188
- const traceIdFromLog = subsegment . trace_id ;
189
- expect ( subsegment ) . toEqual (
190
- expect . objectContaining ( {
191
- id : expect . any ( String ) ,
192
- name : '### index.handler' ,
193
- start_time : expect . any ( Number ) ,
194
- end_time : expect . any ( Number ) ,
195
- type : 'subsegment' ,
196
- annotations : {
197
- ColdStart : true ,
198
- } ,
199
- parent_id : expect . any ( String ) ,
200
- trace_id : traceIdFromLog ,
201
- } )
202
- ) ;
203
- } ) ;
140
+ const invocationLogs = invocationLogsMap . get (
141
+ outputFormat
142
+ ) as TestInvocationLogs ;
143
+ const logs = invocationLogs . getFunctionLogs ( 'WARN' ) ;
144
+
145
+ expect ( logs . length ) . toBe ( 1 ) ;
146
+ expect (
147
+ invocationLogs . doesAnyFunctionLogsContains (
148
+ / N a m e s p a c e s h o u l d b e d e f i n e d , d e f a u l t u s e d / ,
149
+ 'WARN'
150
+ )
151
+ ) . toBe ( true ) ;
152
+ /* expect(logEntry.message).toEqual(
153
+ 'Namespace should be defined, default used'
154
+ ); */
155
+ }
156
+ ) ;
157
+
158
+ it . each ( cases ) ( 'emits an EMF log (%s)' , ( outputFormat ) => {
159
+ const invocationLogs = invocationLogsMap . get (
160
+ outputFormat
161
+ ) as TestInvocationLogs ;
162
+
163
+ expect (
164
+ invocationLogs . doesAnyFunctionLogsContains (
165
+ / { " _ a w s " : { " T i m e s t a m p " : \d + , " C l o u d W a t c h M e t r i c s " : \[ \{ " N a m e s p a c e " : " \S + " , " D i m e n s i o n s " : \[ \[ " s e r v i c e " \] \] , " M e t r i c s " : \[ \{ " N a m e " : " C o l d S t a r t " , " U n i t " : " C o u n t " \} \] \} \] } , " s e r v i c e " : " \S + " , " C o l d S t a r t " : 1 } /
166
+ )
167
+ ) . toBe ( true ) ;
168
+ } ) ;
169
+
170
+ it . each ( cases ) (
171
+ 'emits a debug log with tracer subsegment info (%s)' ,
172
+ ( outputFormat ) => {
173
+ const invocationLogs = invocationLogsMap . get (
174
+ outputFormat
175
+ ) as TestInvocationLogs ;
176
+ const logs = invocationLogs . getFunctionLogs ( 'DEBUG' ) ;
177
+
178
+ expect ( logs . length ) . toBe ( 1 ) ;
179
+ const logEntry = TestInvocationLogs . parseFunctionLog ( logs [ 0 ] ) ;
180
+ expect ( logEntry . message ) . toContain ( 'subsegment' ) ;
181
+ expect ( logEntry . subsegment ) . toBeDefined ( ) ;
182
+ const subsegment = JSON . parse ( logEntry . subsegment as string ) ;
183
+ const traceIdFromLog = subsegment . trace_id ;
184
+ expect ( subsegment ) . toEqual (
185
+ expect . objectContaining ( {
186
+ id : expect . any ( String ) ,
187
+ name : '### index.handler' ,
188
+ start_time : expect . any ( Number ) ,
189
+ end_time : expect . any ( Number ) ,
190
+ type : 'subsegment' ,
191
+ annotations : {
192
+ ColdStart : true ,
193
+ } ,
194
+ parent_id : expect . any ( String ) ,
195
+ trace_id : traceIdFromLog ,
196
+ } )
197
+ ) ;
204
198
}
205
199
) ;
206
200
0 commit comments