Skip to content

Commit

Permalink
test: add better error tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nebrelbug committed Jun 10, 2023
1 parent 2e51f43 commit 4332989
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
36 changes: 23 additions & 13 deletions test/err.spec.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,41 @@
/* global it, expect, describe */

import { ParseErr } from "../src/err";
import path from "path";
import { Eta } from "../src/index";

describe("ParseErr", () => {
it("error throws correctly", () => {
const eta = new Eta();

it("error while parsing", () => {
try {
ParseErr("Something Unexpected Happened!", "template {{", 9);
eta.renderString("template <%", {});
} catch (ex) {
expect((ex as Error).name).toBe("Eta Error");
expect((ex as Error).message).toBe(`Something Unexpected Happened! at line 1 col 10:
expect((ex as Error).message).toBe(`unclosed tag at line 1 col 10:
template {{
template <%
^`);
expect(ex instanceof Error).toBe(true);
}
});
it("error throws without Object.setPrototypeOf", () => {
Object.defineProperty(Object, "setPrototypeOf", { value: undefined });
});

describe("RuntimeErr", () => {
const eta = new Eta({ debug: true, views: path.join(__dirname, "templates") });

const errorFilepath = path.join(__dirname, "templates/runtime-error.eta");

it("error throws correctly", () => {
try {
ParseErr("Something Unexpected Happened!", "template {{", 9);
eta.render("./runtime-error", {});
} catch (ex) {
expect((ex as Error).name).toBe("Eta Error");
expect((ex as Error).message).toBe(`Something Unexpected Happened! at line 1 col 10:
expect((ex as Error).name).toBe("ReferenceError");
expect((ex as Error).message).toBe(`${errorFilepath}:2
1|
>> 2| <%= undefinedVariable %>
3| Lorem Ipsum
template {{
^`);
expect(ex instanceof Error).toBe(true);
undefinedVariable is not defined`);
}
});
});
16 changes: 16 additions & 0 deletions test/render.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@ describe("render caching", () => {

expect(eta.templatesSync.get("@template1")).toBeTruthy();
});

it("throws if template doesn't exist", () => {
expect(() => {
eta.render("@should-error", {});
}).toThrow(/Failed to get template/);
});
});

describe("render caching w/ files", () => {
const eta = new Eta({ cache: true, views: path.join(__dirname, "templates") });

eta.loadTemplate(path.join(__dirname, "templates/nonexistent.eta"), "Hi <%=it.name%>");

it("Template files cache", () => {
expect(eta.render("./nonexistent", { name: "Ada Lovelace" })).toEqual("Hi Ada Lovelace");
});
});

describe("useWith", () => {
Expand Down
3 changes: 3 additions & 0 deletions test/templates/runtime-error.eta
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

<%= undefinedVariable %>
Lorem Ipsum

0 comments on commit 4332989

Please sign in to comment.