Skip to content

Commit

Permalink
fix file logger test
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed Nov 26, 2023
1 parent f6d97ff commit 372bef5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/loggers/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
const FormattedLogger = require("./formatted");
const _ = require("lodash");

const fs = require("fs").promises;
const fs = require("fs/promises");
const path = require("path");
const os = require("os");
const { makeDirs } = require("../utils");
Expand Down
13 changes: 6 additions & 7 deletions test/unit/loggers/file.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ const utils = require("../../../src/utils");
utils.makeDirs = jest.fn();

jest.mock("path");
jest.mock("fs");
jest.mock("fs/promises");

const path = require("path");
const fs = require("fs");
const fs = require("fs/promises");

path.join = jest.fn((...args) => args.join("/"));
path.resolve = jest.fn((...args) => args.join("/"));
Expand Down Expand Up @@ -278,7 +278,7 @@ describe("Test File logger class", () => {
expect(fs.appendFile).toHaveBeenCalledTimes(0);
});

it("should render rows and call appendFile", () => {
it("should render rows and call appendFile", async () => {
logger = new FileLogger({ level: "trace", eol: "\n" });
logger.init(loggerFactory);

Expand All @@ -287,15 +287,14 @@ describe("Test File logger class", () => {
logHandler("error", ["message", { a: 5 }]);

logger.formatter = jest.fn(() => "rendered");
fs.appendFile.mockClear();
fs.appendFile = jest.fn(() => Promise.resolve());

logger.flush();
await logger.flush();

expect(fs.appendFile).toHaveBeenCalledTimes(1);
expect(fs.appendFile).toHaveBeenCalledWith(
"./logs/moleculer-1970-01-01.log",
"[1970-01-01T00:00:00.000Z] FATAL node-1/MY-SERVICE: message { a: 5 }\n[1970-01-01T00:00:00.000Z] ERROR node-1/MY-SERVICE: message { a: 5 }\n",
expect.any(Function)
"[1970-01-01T00:00:00.000Z] FATAL node-1/MY-SERVICE: message { a: 5 }\n[1970-01-01T00:00:00.000Z] ERROR node-1/MY-SERVICE: message { a: 5 }\n"
);
});

Expand Down

0 comments on commit 372bef5

Please sign in to comment.