chore(test): improve test coverage for fileSyncAppender

appenders/fileSync.js - Line 58 - throw new Error(`maxLogSize (${maxLogSize}) should be > 0`);
This commit is contained in:
Lam Wei Li 2022-03-08 17:22:12 +08:00
parent 6e6dbad5d3
commit 8ad41831d1
No known key found for this signature in database
GPG Key ID: 90F6ABECF080D7BF
2 changed files with 26 additions and 8 deletions

View File

@ -51,19 +51,15 @@ function touchFile(file, options) {
}
class RollingFileSync {
constructor(filename, size, backups, options) {
constructor(filename, maxLogSize, backups, options) {
debug('In RollingFileStream');
function throwErrorIfArgumentsAreNotValid() {
if (!filename || !size || size <= 0) {
throw new Error('You must specify a filename and file size');
}
if (maxLogSize < 0) {
throw new Error(`maxLogSize (${maxLogSize}) should be > 0`);
}
throwErrorIfArgumentsAreNotValid();
this.filename = filename;
this.size = size;
this.size = maxLogSize;
this.backups = backups;
this.options = options;
this.currentSize = 0;

View File

@ -61,6 +61,28 @@ test("log4js fileSyncAppender", batch => {
t.end();
});
batch.test("should give error if invalid maxLogSize", async t => {
const maxLogSize = -1;
const expectedError = new Error(`maxLogSize (${maxLogSize}) should be > 0`);
t.throws(
() =>
log4js.configure({
appenders: {
file: {
type: "fileSync",
filename: path.join(__dirname, "fa-invalidMaxFileSize-sync-test.log"),
maxLogSize: -1
}
},
categories: {
default: { appenders: ["file"], level: "debug" }
}
}),
expectedError
);
t.end();
});
batch.test("with a max file size and no backups", t => {
const testFile = path.join(__dirname, "/fa-maxFileSize-sync-test.log");
const logger = log4js.getLogger("max-file-size");