@sunamo/sunodejs
Version:
Node.js utilities for file system operations, process management, and Electron apps. Includes TypeScript support with functions for file operations, directory management, and cross-platform compatibility.
56 lines (55 loc) • 2.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
jest.mock("fs/promises", () => ({
writeFile: jest.fn(),
mkdir: jest.fn(),
}));
jest.mock("path", () => ({
dirname: jest.fn(),
}));
const TF_1 = require("../TF");
const mockWriteFile = jest.mocked(require("fs/promises").writeFile);
const mockMkdir = jest.mocked(require("fs/promises").mkdir);
const mockDirname = jest.mocked(require("path").dirname);
describe("writeAllLines", () => {
beforeEach(() => {
jest.clearAllMocks();
});
it("writes all lines to a file", async () => {
console.log("writeAllLines test start");
const log = {
error: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
debug: jest.fn(),
};
const filePath = "/test/path/testfile.txt";
const lines = ["a", "b", "c"];
mockDirname.mockReturnValue("/test/path");
mockMkdir.mockResolvedValue(undefined);
mockWriteFile.mockResolvedValue(undefined);
console.log("writeAllLines test call");
await (0, TF_1.writeAllLines)(log, filePath, lines);
expect(mockDirname).toHaveBeenCalledWith(filePath);
expect(mockMkdir).toHaveBeenCalledWith("/test/path", { recursive: true });
expect(mockWriteFile).toHaveBeenCalledWith(filePath, "a\nb\nc", "utf8");
expect(log.error).not.toHaveBeenCalled();
console.log("writeAllLines test end");
});
it("handles write errors", async () => {
const log = {
error: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
debug: jest.fn(),
};
const filePath = "/test/path/testfile.txt";
const lines = ["a", "b", "c"];
const writeError = new Error("Write failed");
mockDirname.mockReturnValue("/test/path");
mockMkdir.mockResolvedValue(undefined);
mockWriteFile.mockRejectedValue(writeError);
await expect((0, TF_1.writeAllLines)(log, filePath, lines)).rejects.toThrow("Write failed");
expect(log.error).toHaveBeenCalledWith(`Error writing to file: ${filePath}`, writeError);
});
});