mc-publish/tests/unit/utils/errors.ts/file-not-found-error.spec.ts

43 lines
1.5 KiB
TypeScript
Raw Normal View History

2023-01-08 04:42:07 -05:00
import { FileNotFoundError } from "@/utils/errors/file-not-found-error";
import mockFs from "mock-fs";
describe("FileNotFoundError", () => {
describe("constructor", () => {
test("initializes with default message if none provided", () => {
const error = new FileNotFoundError("test.txt");
expect(error).toBeInstanceOf(FileNotFoundError);
expect(error.name).toBe("FileNotFoundError");
expect(error.message).toBe("Could not find file 'test.txt'.");
expect(error.fileName).toBe("test.txt");
});
test("initializes with provided message", () => {
const error = new FileNotFoundError("test.txt", "Custom error message");
expect(error).toBeInstanceOf(FileNotFoundError);
expect(error.name).toBe("FileNotFoundError");
expect(error.message).toBe("Custom error message");
expect(error.fileName).toBe("test.txt");
});
});
describe("throwIfNotFound", () => {
beforeEach(() => {
mockFs({ "test": "test" });
});
afterEach(() => {
mockFs.restore();
});
test("throws error if file does not exist", () => {
expect(() => FileNotFoundError.throwIfNotFound("test.txt")).toThrow(FileNotFoundError);
});
test("does not throw error if file exists", () => {
expect(() => FileNotFoundError.throwIfNotFound("test")).not.toThrow();
});
});
});