Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,14 @@
"types": "./index.d.ts",
"scripts": {
"lint": "eslint lib/*.js lib/winston/*.js lib/winston/**/*.js --resolve-plugins-relative-to ./node_modules/@dabh/eslint-config-populist",
"test": "rimraf test/fixtures/logs/* && mocha",
"lint:all": "eslint lib/**/*.js test/**/*.js --resolve-plugins-relative-to ./node_modules/@dabh/eslint-config-populist",
"pretest": "rimraf test/fixtures/logs/*",
"test": "mocha",
"test:coverage": "nyc npm run test:unit",
"test:unit": "mocha test/unit",
"test:integration": "mocha test/integration",
"build": "rimraf dist && babel lib -d dist",
"prebuild": "rimraf dist",
"build": "babel lib -d dist",
"prepublishOnly": "npm run build"
},
"engines": {
Expand Down
26 changes: 22 additions & 4 deletions test/helpers/mocks/mock-transport.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const stream = require('stream')
const stream = require('stream');
const winston = require('../../../lib/winston');
const { Writable } = require('stream');

/**
* Returns a new Winston transport instance which will invoke
* the `write` method on each call to `.log`
* the `write` method on each call to `.log`
*
* @param {function} write Write function for the specified stream
* @returns {StreamTransportInstance} A transport instance
Expand All @@ -14,9 +15,26 @@ function createMockTransport(write) {
write: write
});

return new winston.transports.Stream({ stream: writeable })
return new winston.transports.Stream({ stream: writeable });
}

/**
* Returns a valid Winston transport that writes to the passed array object
* @param array Array to be used to store the "written" chunks
* @returns {winston.transports.Stream}
*/
function inMemory(array, options = {}) {
const memoryStream = new Writable({
objectMode: true,
write: (chunk, encoding, next) => {
array.push(chunk);
next();
}
});
return new winston.transports.Stream({ stream: memoryStream, ...options });
}

module.exports = {
createMockTransport
createMockTransport,
inMemory
};
57 changes: 57 additions & 0 deletions test/unit/regressions/3-7-x.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import assume from 'assume';
import { createLogger } from '../../../lib/winston.js';
import { inMemory } from '../../helpers/mocks/mock-transport.js';


class SuperError extends Error {
constructor() {
super();
Object.defineProperty(this, 'canBeAnything', { enumerable: true, value: '' });
}
}

class ThisError extends SuperError {
message;

constructor() {
super();
this.message = 'This must not be empty';
}
}

describe.skip('[TODO] Regressions reported in v3.7.x', () => {
let logger;
let actualLogOutput;

beforeEach(() => {
actualLogOutput = [];
logger = createLogger({
defaultMeta: { loggerName: 'parent' },
transports: [inMemory(actualLogOutput)]
});
});

describe('Logging of Errors', () => {
it('should not throw an error when logging an instance of a class that extends Error', () => {
const error = new ThisError();
const expectedOutput = [
{ level: 'error', message: 'This must not be empty', stack: error.stack, loggerName: 'parent' }
];

logger.info(new ThisError());

assume(expectedOutput).eqls(actualLogOutput);
});

it('should handle plain Error instances correctly', () => {
const error = new Error('dummy error');
const expectedOutput = [
{ level: 'error', message: 'dummy error', stack: error.stack, service: 'user-service' }
];

logger.error(error);

assume(expectedOutput).eqls(actualLogOutput);
});
});
});
28 changes: 28 additions & 0 deletions test/unit/winston/child-loggers.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

import assume from 'assume';
import { createLogger } from '../../../lib/winston.js';
import { inMemory } from '../../helpers/mocks/mock-transport.js';

describe('Child Loggers', () => {
let actualLogOutput;
let logger;
let childLogger;

beforeEach(() => {
actualLogOutput = [];
logger = createLogger({ transports: [inMemory(actualLogOutput)] });
childLogger = logger.child({ service: 'user-service' });
});

it('handles error stack traces in child loggers correctly', () => {
const error = new Error('dummy error');
const expectedOutput = [
{ level: 'error', message: 'dummy error', stack: error.stack, service: 'user-service' }
];

childLogger.error(error);

assume(expectedOutput).eqls(actualLogOutput);
});
});
Loading
Loading