test: add error handling test for SumEngine evaluate block

Co-authored-by: Horizontal42 <75383519+Horizontal42@users.noreply.github.com>
This commit is contained in:
google-labs-jules[bot] 2026-10-07 03:55:52 +00:00
parent bede889ced
commit fbbe46e854

View file

@ -1,8 +1,9 @@
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 { evaluate } from "./evaluator"; import { evaluate } from "./evaluator";
import * as evaluator from "./evaluator";
import { formatValue } from "./formatter"; import { formatValue } from "./formatter";
let eng: SumEngine; let eng: SumEngine;
@ -451,4 +452,17 @@ 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("catches and handles arbitrary errors in evaluateDocument", () => {
// Spy on evaluator.evaluate and force an arbitrary error
const spy = vi.spyOn(evaluator, "evaluate").mockImplementation(() => {
throw new Error("Forced execution anomaly");
});
const results = eng.evaluateDocument("1 + 1");
expect(results).toHaveLength(1);
expect(results[0].error).toBe("Forced execution anomaly");
spy.mockRestore();
});
}); });