Skip to content

Commit 3584db1

Browse files
committed
test(logger): add tests for POWERTOOLS_DEV env var and for pretty printing logs when var is set to truthy value
1 parent 8b85db1 commit 3584db1

File tree

2 files changed

+154
-1
lines changed

2 files changed

+154
-1
lines changed

packages/logger/tests/unit/Logger.test.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import * as dummyEvent from '../../../../tests/resources/events/custom/hello-wor
1111
import { createLogger, Logger } from '../../src';
1212
import { EnvironmentVariablesService } from '../../src/config';
1313
import { PowertoolLogFormatter } from '../../src/formatter';
14-
import { ClassThatLogs } from '../../src/types';
14+
import { ClassThatLogs, LogJsonIndent } from '../../src/types';
1515
import { Context, Handler } from 'aws-lambda';
1616
import { Console } from 'console';
1717

@@ -1411,4 +1411,49 @@ describe('Class: Logger', () => {
14111411
});
14121412
});
14131413

1414+
describe('Feature: Pretty indentation for a local or non-production environment', () => {
1415+
1416+
test('when the `POWERTOOLS_DEV` env var is SET it makes log output has multiple lines', () => {
1417+
1418+
// Prepare
1419+
process.env.POWERTOOLS_DEV = 'true';
1420+
const INDENTATION = LogJsonIndent.PRETTY;
1421+
const logger = new Logger();
1422+
const consoleSpy = jest.spyOn(logger['console'], 'info').mockImplementation();
1423+
1424+
// Act
1425+
logger.info('Message with pretty identation');
1426+
1427+
// Assess
1428+
expect(consoleSpy).toBeCalledTimes(1);
1429+
expect(consoleSpy).toHaveBeenNthCalledWith(1, JSON.stringify({
1430+
level: 'INFO',
1431+
message: 'Message with pretty identation',
1432+
service: 'hello-world',
1433+
timestamp: '2016-06-20T12:08:10.000Z',
1434+
xray_trace_id: '1-5759e988-bd862e3fe1be46a994272793',
1435+
}, null, INDENTATION));
1436+
});
1437+
1438+
test('when the `POWERTOOLS_DEV` env var is NOT SET it makes log output as one-liner', () => {
1439+
1440+
// Prepare
1441+
const logger = new Logger();
1442+
const consoleSpy = jest.spyOn(logger['console'], 'info').mockImplementation();
1443+
1444+
// Act
1445+
logger.info('Message without pretty identation');
1446+
1447+
// Assess
1448+
expect(consoleSpy).toBeCalledTimes(1);
1449+
expect(consoleSpy).toHaveBeenNthCalledWith(1, JSON.stringify({
1450+
level: 'INFO',
1451+
message: 'Message without pretty identation',
1452+
service: 'hello-world',
1453+
timestamp: '2016-06-20T12:08:10.000Z',
1454+
xray_trace_id: '1-5759e988-bd862e3fe1be46a994272793',
1455+
}));
1456+
});
1457+
});
1458+
14141459
});

packages/logger/tests/unit/config/EnvironmentVariablesService.test.ts

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,112 @@ describe('Class: EnvironmentVariablesService', () => {
220220

221221
});
222222

223+
describe('Method: getDevMode', () => {
224+
225+
test('It returns true if the environment variable POWERTOOLS_DEV is "true"', () => {
226+
227+
// Prepare
228+
process.env.POWERTOOLS_DEV = 'true';
229+
const service = new EnvironmentVariablesService();
230+
231+
// Act
232+
const value = service.getDevMode();
233+
234+
// Assess
235+
expect(value).toEqual(true);
236+
});
237+
238+
test('It returns true if the environment variable POWERTOOLS_DEV is "TRUE"', () => {
239+
240+
// Prepare
241+
process.env.POWERTOOLS_DEV = 'TRUE';
242+
const service = new EnvironmentVariablesService();
243+
244+
// Act
245+
const value = service.getDevMode();
246+
247+
// Assess
248+
expect(value).toEqual(true);
249+
});
250+
251+
test('It returns true if the environment variable POWERTOOLS_DEV is "1"', () => {
252+
253+
// Prepare
254+
process.env.POWERTOOLS_DEV = '1';
255+
const service = new EnvironmentVariablesService();
256+
257+
// Act
258+
const value = service.getDevMode();
259+
260+
// Assess
261+
expect(value).toEqual(true);
262+
});
263+
264+
test('It returns false if the environment variable POWERTOOLS_DEV is "false"', () => {
265+
266+
// Prepare
267+
process.env.POWERTOOLS_DEV = 'false';
268+
const service = new EnvironmentVariablesService();
269+
270+
// Act
271+
const value = service.getDevMode();
272+
273+
// Assess
274+
expect(value).toEqual(false);
275+
});
276+
277+
test('It returns false if the environment variable POWERTOOLS_DEV is "FALSE"', () => {
278+
279+
// Prepare
280+
process.env.POWERTOOLS_DEV = 'FALSE';
281+
const service = new EnvironmentVariablesService();
282+
283+
// Act
284+
const value = service.getDevMode();
285+
286+
// Assess
287+
expect(value).toEqual(false);
288+
});
289+
290+
test('It returns false if the environment variable POWERTOOLS_DEV is "0"', () => {
291+
292+
// Prepare
293+
process.env.POWERTOOLS_DEV = '0';
294+
const service = new EnvironmentVariablesService();
295+
296+
// Act
297+
const value = service.getDevMode();
298+
299+
// Assess
300+
expect(value).toEqual(false);
301+
});
302+
303+
test('It returns false if the environment variable POWERTOOLS_DEV is NOT set', () => {
304+
305+
// Prepare
306+
process.env.POWERTOOLS_DEV = 'somethingsilly';
307+
const service = new EnvironmentVariablesService();
308+
309+
// Act
310+
const value = service.getDevMode();
311+
312+
// Assess
313+
expect(value).toEqual(false);
314+
});
315+
316+
test('It returns false if the environment variable POWERTOOLS_DEV is "somethingsilly"', () => {
317+
318+
// Prepare
319+
process.env.POWERTOOLS_DEV = 'somethingsilly';
320+
const service = new EnvironmentVariablesService();
321+
322+
// Act
323+
const value = service.getDevMode();
324+
325+
// Assess
326+
expect(value).toEqual(false);
327+
});
328+
329+
});
330+
223331
});

0 commit comments

Comments
 (0)