added a helper to tests for one-liner error matching in unit tests. fixed bugs in tests after the tree fixture changed
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
import type {
|
||||
AlreadyExistsError,
|
||||
DirectoryLoopError,
|
||||
IllegalMutationError,
|
||||
NodeNotFoundError,
|
||||
NotADirectoryError,
|
||||
} from "@core/filesystem/errors";
|
||||
import type { TreeNodeType } from "@core/filesystem/treenode";
|
||||
import { expect } from "vitest";
|
||||
|
||||
export function expectThrows(fn: () => unknown, expectedShape: Record<string, unknown>) {
|
||||
let thrown: unknown;
|
||||
try {
|
||||
fn();
|
||||
} catch (e) {
|
||||
thrown = e;
|
||||
}
|
||||
|
||||
expect(thrown).toBeDefined();
|
||||
expect(thrown).toMatchObject(expectedShape);
|
||||
}
|
||||
|
||||
export function expectNotADirectoryError(fn: () => unknown, overrides?: Partial<NotADirectoryError>) {
|
||||
expectThrows(fn, {
|
||||
type: "NotADirectory",
|
||||
inode: expect.any(Number) as number,
|
||||
message: expect.any(String) as string,
|
||||
timestamp: expect.any(Number) as number,
|
||||
nodeType: expect.any(String) as TreeNodeType,
|
||||
...overrides,
|
||||
});
|
||||
}
|
||||
|
||||
export function expectAlreadyExistsError(fn: () => unknown, overrides?: Partial<AlreadyExistsError>) {
|
||||
expectThrows(fn, {
|
||||
type: "AlreadyExists",
|
||||
parentInode: expect.any(Number) as number,
|
||||
name: expect.any(String) as string,
|
||||
message: expect.any(String) as string,
|
||||
timestamp: expect.any(Number) as number,
|
||||
...overrides,
|
||||
});
|
||||
}
|
||||
|
||||
export function expectNodeNotFoundError(fn: () => unknown, overrides?: Partial<NodeNotFoundError>) {
|
||||
expectThrows(fn, {
|
||||
type: "NodeNotFound",
|
||||
inode: expect.any(Number) as number,
|
||||
message: expect.any(String) as string,
|
||||
timestamp: expect.any(Number) as number,
|
||||
...overrides,
|
||||
});
|
||||
}
|
||||
|
||||
export function expectIllegalMutationError(fn: () => unknown, overrides?: Partial<IllegalMutationError>) {
|
||||
expectThrows(fn, {
|
||||
type: "IllegalMutation",
|
||||
message: expect.any(String) as string,
|
||||
timestamp: expect.any(Number) as number,
|
||||
...overrides,
|
||||
});
|
||||
}
|
||||
|
||||
export function expectDirectoryLoopError(fn: () => void, overrides?: Partial<DirectoryLoopError>) {
|
||||
expectThrows(fn, {
|
||||
type: "DirectoryLoop",
|
||||
descendantInode: expect.any(Number) as number,
|
||||
ancestorInode: expect.any(Number) as number,
|
||||
message: expect.any(String) as string,
|
||||
timestamp: expect.any(Number) as number,
|
||||
...overrides,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user