Skip to content

Commit

Permalink
Added automated test to assert appenders and categories are reverted …
Browse files Browse the repository at this point in the history
…back to initial state on log4js.shutdown()
  • Loading branch information
weili committed Jan 17, 2022
1 parent 44bf7ce commit f6d6acc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/categories.js
Expand Up @@ -203,11 +203,12 @@ const setEnableCallStackForCategory = (category, useCallStack) => {
configForCategory(category).enableCallStack = useCallStack;
};

module.exports = {
module.exports = categories;
module.exports = Object.assign(module.exports, {
appendersForCategory,
getLevelForCategory,
setLevelForCategory,
getEnableCallStackForCategory,
setEnableCallStackForCategory,
init,
};
});
30 changes: 30 additions & 0 deletions test/tap/logging-test.js
Expand Up @@ -4,6 +4,36 @@ const util = require("util");
const recording = require("../../lib/appenders/recording");

test("log4js", batch => {
batch.test("shutdown should return appenders and categories back to initial state", t => {
const stringifyMap = (map) => JSON.stringify(Array.from(map));
const deepCopyMap = (map) => new Map(JSON.parse(stringifyMap(map)));

const log4js = require("../../lib/log4js");

const appenders = require("../../lib/appenders");
const categories = require("../../lib/categories");
const initialAppenders = deepCopyMap(appenders);
const initialCategories = deepCopyMap(categories);

log4js.configure({
appenders: { recorder: { type: "recording" } },
categories: { default: { appenders: ["recorder"], level: "DEBUG" } }
});

const configuredAppenders = deepCopyMap(appenders);
const configuredCategories = deepCopyMap(categories);
t.notEqual(stringifyMap(configuredAppenders), stringifyMap(initialAppenders), "appenders should be different from initial state");
t.notEqual(stringifyMap(configuredCategories), stringifyMap(initialCategories), "categories should be different from initial state");

log4js.shutdown(() => {
const finalAppenders = deepCopyMap(appenders);
const finalCategories = deepCopyMap(categories);
t.equal(stringifyMap(finalAppenders), stringifyMap(initialAppenders), "appenders should revert back to initial state");
t.equal(stringifyMap(finalCategories), stringifyMap(initialCategories), "categories should revert back to initial state");
t.end();
});
});

batch.test("getLogger", t => {
const log4js = require("../../lib/log4js");
log4js.configure({
Expand Down

0 comments on commit f6d6acc

Please sign in to comment.