chore(test): improve test coverage for logger

lib/logger.js - Line 25 - catch (err) {
lib/logger.js - Line 26 -   // will never get error unless nodejs has breaking changes to Error
lib/logger.js - Line 27 -   console.error('log4js.logger - defaultParseCallStack error', err); // eslint-disable-line no-console
lib/logger.js - Line 28 - }
lib/logger.js - Line 29 - return null;
This commit is contained in:
Lam Wei Li 2022-03-12 00:32:36 +08:00
parent 1647ba5bdf
commit 442bab55b8
No known key found for this signature in database
GPG Key ID: 90F6ABECF080D7BF
2 changed files with 43 additions and 10 deletions

View File

@ -9,16 +9,23 @@ const configuration = require("./configuration");
const stackReg = /at (?:(.+)\s+\()?(?:(.+?):(\d+)(?::(\d+))?|([^)]+))\)?/;
function defaultParseCallStack(data, skipIdx = 4) {
const stacklines = data.stack.split("\n").slice(skipIdx);
const lineMatch = stackReg.exec(stacklines[0]);
if (lineMatch && lineMatch.length === 6) {
return {
functionName: lineMatch[1],
fileName: lineMatch[2],
lineNumber: parseInt(lineMatch[3], 10),
columnNumber: parseInt(lineMatch[4], 10),
callStack: stacklines.join("\n")
};
try {
const stacklines = data.stack.split("\n").slice(skipIdx);
const lineMatch = stackReg.exec(stacklines[0]);
/* istanbul ignore else */
if (lineMatch && lineMatch.length === 6) {
return {
functionName: lineMatch[1],
fileName: lineMatch[2],
lineNumber: parseInt(lineMatch[3], 10),
columnNumber: parseInt(lineMatch[4], 10),
callStack: stacklines.join("\n")
};
}
}
catch (err) {
// will never get error unless nodejs has breaking changes to Error
console.error('log4js.logger - defaultParseCallStack error', err); // eslint-disable-line no-console
}
return null;
}

View File

@ -5,6 +5,7 @@ const callsites = require("callsites");
const levels = require("../../lib/levels");
const events = [];
const messages = [];
const Logger = sandbox.require("../../lib/logger", {
requires: {
"./levels": levels,
@ -16,6 +17,14 @@ const Logger = sandbox.require("../../lib/logger", {
events.push(evt);
}
}
},
globals: {
console: {
...console,
error(msg) {
messages.push(msg);
}
}
}
});
@ -219,6 +228,23 @@ test("../../lib/logger", batch => {
}
);
batch.test("parseCallStack function coverage", t => {
const logger = new Logger("stack");
logger.useCallStack = true;
let results;
results = logger.parseCallStack(new Error());
t.ok(results);
t.equal(messages.length, 0, "should not have error");
results = logger.parseCallStack("");
t.notOk(results);
t.equal(messages.length, 1, "should have error");
t.end();
});
batch.test("should correctly change the parseCallStack function", t => {
const logger = new Logger("stack");
const parseFunction = function() {