mirror of
https://github.com/Horizontal42/summarum.git
synced 2026-11-10 20:21:58 +03:00
🧪 Add test for unexpected errors in evaluateDocument
Added a test in `src/engine/engine.test.ts` to mock evaluation failure and verify the engine safely catches it, nulls the value, populates the error string, and logs a warning. Co-authored-by: Horizontal42 <75383519+Horizontal42@users.noreply.github.com>
This commit is contained in:
parent
37cfd2144b
commit
a1acba5648
1 changed files with 20 additions and 1 deletions
|
|
@ -1,9 +1,11 @@
|
||||||
import { describe, it, expect, beforeAll } from "vitest";
|
import { describe, it, expect, beforeAll, vi } from "vitest";
|
||||||
import { SumEngine } from "./index";
|
import { SumEngine } from "./index";
|
||||||
import { qty, EvalError, XRefError } from "./types";
|
import { qty, EvalError, XRefError } from "./types";
|
||||||
import type { XRefResolution, EvalCtx } from "./evaluator";
|
import type { XRefResolution, EvalCtx } from "./evaluator";
|
||||||
|
import * as evaluator from "./evaluator";
|
||||||
import { evaluate } from "./evaluator";
|
import { evaluate } from "./evaluator";
|
||||||
import { formatValue } from "./formatter";
|
import { formatValue } from "./formatter";
|
||||||
|
import { logger } from "../logger";
|
||||||
|
|
||||||
let eng: SumEngine;
|
let eng: SumEngine;
|
||||||
|
|
||||||
|
|
@ -451,4 +453,21 @@ describe("evaluation errors", () => {
|
||||||
} as unknown as EvalCtx;
|
} as unknown as EvalCtx;
|
||||||
expect(() => evaluate({ k: "xref", sheet: "Sheet", key: "key" }, ctx)).toThrowError(XRefError);
|
expect(() => evaluate({ k: "xref", sheet: "Sheet", key: "key" }, ctx)).toThrowError(XRefError);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("evaluateDocument captures unexpected errors", () => {
|
||||||
|
const mockEval = vi.spyOn(evaluator, "evaluate").mockImplementation(() => {
|
||||||
|
throw new Error("Boom");
|
||||||
|
});
|
||||||
|
|
||||||
|
const warnSpy = vi.spyOn(logger, "warn").mockImplementation(() => {});
|
||||||
|
|
||||||
|
const result = eng.evaluateDocument("1 + 1");
|
||||||
|
|
||||||
|
expect(result[0].error).toBe("Boom");
|
||||||
|
expect(result[0].value).toBeNull();
|
||||||
|
expect(warnSpy).toHaveBeenCalledWith("evaluate failed:", expect.any(Error));
|
||||||
|
|
||||||
|
warnSpy.mockRestore();
|
||||||
|
mockEval.mockRestore();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue