simple-leveled-log-methods
Version:
a simple and opinionated logging library. plays well with aws lambda + cloudwatch.
47 lines • 2.46 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const _1 = require(".");
const constants_1 = require("./constants");
const formatLogContentsForEnvironment_1 = require("./formatLogContentsForEnvironment");
const identifyEnvironment_1 = require("./identifyEnvironment");
jest.mock('./identifyEnvironment');
const identifyEnvironmentMock = identifyEnvironment_1.identifyEnvironment;
describe('formatMetadataForEnvironment', () => {
it('should not stringify the contents - but should stringify the metadata - if in the local environment', () => {
identifyEnvironmentMock.mockReturnValue(constants_1.SupportedEnvironment.LOCAL);
const metadata = { name: 'bob', likes: ['oranges', 'apples'] };
const logContents = {
level: _1.LogLevel.INFO,
timestamp: new Date().toISOString(),
message: 'hello world!',
metadata,
};
const formatted = (0, formatLogContentsForEnvironment_1.formatLogContentsForEnvironment)(logContents);
expect(formatted).toEqual(Object.assign(Object.assign({}, logContents), { metadata: JSON.stringify(metadata) }));
});
it('should stringify the contents in the aws lambda environment', () => {
identifyEnvironmentMock.mockReturnValue(constants_1.SupportedEnvironment.AWS_LAMBDA);
const metadata = { name: 'bob', likes: ['oranges', 'apples'] };
const logContents = {
level: _1.LogLevel.INFO,
timestamp: new Date().toISOString(),
message: 'hello world!',
metadata,
};
const formatted = (0, formatLogContentsForEnvironment_1.formatLogContentsForEnvironment)(logContents);
expect(formatted).toEqual(JSON.stringify(logContents));
});
it('should not stringify the contents nor the metadata if in web browser environment', () => {
identifyEnvironmentMock.mockReturnValue(constants_1.SupportedEnvironment.WEB_BROWSER);
const metadata = { name: 'bob', likes: ['oranges', 'apples'] };
const logContents = {
level: _1.LogLevel.INFO,
timestamp: new Date().toISOString(),
message: 'hello world!',
metadata,
};
const formatted = (0, formatLogContentsForEnvironment_1.formatLogContentsForEnvironment)(logContents);
expect(formatted).toEqual(logContents);
});
});
//# sourceMappingURL=formatLogContentsForEnvironment.test.js.map
;