@@ -23,6 +23,13 @@ import {
23
23
import { setupDecoratorLambdaHandler } from '../helpers/metricsUtils.js' ;
24
24
import { EnvironmentVariablesService } from '../../src/config/EnvironmentVariablesService.js' ;
25
25
26
+ jest . mock ( 'node:console' , ( ) => ( {
27
+ ...jest . requireActual ( 'node:console' ) ,
28
+ Console : jest . fn ( ) . mockImplementation ( ( ) => ( {
29
+ log : jest . fn ( ) ,
30
+ } ) ) ,
31
+ } ) ) ;
32
+ jest . spyOn ( console , 'warn' ) . mockImplementation ( ( ) => ( { } ) ) ;
26
33
const mockDate = new Date ( 1466424490000 ) ;
27
34
const dateSpy = jest . spyOn ( global , 'Date' ) . mockImplementation ( ( ) => mockDate ) ;
28
35
jest . spyOn ( console , 'log' ) . mockImplementation ( ) ;
@@ -235,6 +242,9 @@ describe('Class: Metrics', () => {
235
242
getServiceName ( ) : string {
236
243
return 'test-service' ;
237
244
} ,
245
+ isDevMode ( ) : boolean {
246
+ return false ;
247
+ } ,
238
248
} ;
239
249
const metricsOptions : MetricsOptions = {
240
250
customConfigService : configService ,
@@ -699,7 +709,7 @@ describe('Class: Metrics', () => {
699
709
test ( 'it should publish metrics when the array of values reaches the maximum size' , ( ) => {
700
710
// Prepare
701
711
const metrics : Metrics = new Metrics ( { namespace : TEST_NAMESPACE } ) ;
702
- const consoleSpy = jest . spyOn ( console , 'log' ) ;
712
+ const consoleSpy = jest . spyOn ( metrics [ ' console' ] , 'log' ) ;
703
713
const metricName = 'test-metric' ;
704
714
705
715
// Act
@@ -1242,7 +1252,9 @@ describe('Class: Metrics', () => {
1242
1252
// Prepare
1243
1253
const metrics : Metrics = new Metrics ( { namespace : TEST_NAMESPACE } ) ;
1244
1254
metrics . addMetric ( 'test-metric' , MetricUnit . Count , 10 ) ;
1245
- const consoleLogSpy = jest . spyOn ( console , 'log' ) . mockImplementation ( ) ;
1255
+ const consoleLogSpy = jest
1256
+ . spyOn ( metrics [ 'console' ] , 'log' )
1257
+ . mockImplementation ( ) ;
1246
1258
const mockData : EmfOutput = {
1247
1259
_aws : {
1248
1260
Timestamp : mockDate . getTime ( ) ,
@@ -1512,7 +1524,7 @@ describe('Class: Metrics', () => {
1512
1524
} ) ;
1513
1525
1514
1526
// Act
1515
- metrics . addMetric ( testMetric , MetricUnits . Count , 10 ) ;
1527
+ metrics . addMetric ( testMetric , MetricUnit . Count , 10 ) ;
1516
1528
metrics . addDimension ( 'foo' , 'baz' ) ;
1517
1529
const loggedData = metrics . serializeMetrics ( ) ;
1518
1530
@@ -1531,7 +1543,7 @@ describe('Class: Metrics', () => {
1531
1543
Metrics : [
1532
1544
{
1533
1545
Name : testMetric ,
1534
- Unit : MetricUnits . Count ,
1546
+ Unit : MetricUnit . Count ,
1535
1547
} ,
1536
1548
] ,
1537
1549
Namespace : TEST_NAMESPACE ,
@@ -2179,4 +2191,15 @@ describe('Class: Metrics', () => {
2179
2191
) ;
2180
2192
} ) ;
2181
2193
} ) ;
2194
+
2195
+ describe ( 'Feature: POWERTOOLS_DEV' , ( ) => {
2196
+ it ( 'uses the global console object when the environment variable is set' , ( ) => {
2197
+ // Prepare
2198
+ process . env . POWERTOOLS_DEV = 'true' ;
2199
+ const metrics : Metrics = new Metrics ( { namespace : TEST_NAMESPACE } ) ;
2200
+
2201
+ // Act & Assess
2202
+ expect ( metrics [ 'console' ] ) . toEqual ( console ) ;
2203
+ } ) ;
2204
+ } ) ;
2182
2205
} ) ;
0 commit comments