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

47 lines
1.7 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(() => {
2023-05-18 13:33:21 -04:00
mockFs({ test: "test" });
2023-01-08 04:42:07 -05:00
});
afterEach(() => {
mockFs.restore();
});
test("throws error if file does not exist", () => {
expect(() => FileNotFoundError.throwIfNotFound("test.txt")).toThrow(FileNotFoundError);
});
test("throws a FileNotFoundError with the provided error message", () => {
expect(() => FileNotFoundError.throwIfNotFound("test.txt", "I don't like file extensions.")).toThrowError(new FileNotFoundError("test.txt", "I don't like file extensions."));
});
2023-01-08 04:42:07 -05:00
test("does not throw error if file exists", () => {
expect(() => FileNotFoundError.throwIfNotFound("test")).not.toThrow();
});
});
});