deno.land / x / eta@v3.4.0 / test / err.spec.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* global it, expect, describe */
import path from "path";import { Eta, EtaError, EtaParseError, EtaRuntimeError, EtaFileResolutionError, EtaNameResolutionError,} from "../src/index";
describe("ParseErr", () => { const eta = new Eta();
it("error while parsing - renderString", () => { try { eta.renderString("template <%", {}); } catch (ex) { expect(ex).toBeInstanceOf(EtaError); expect(ex).toBeInstanceOf(EtaParseError); expect((ex as EtaParseError).name).toBe("EtaParser Error"); expect((ex as EtaParseError).message).toBe(`unclosed tag at line 1 col 10:
template <% ^`); expect(ex instanceof Error).toBe(true); } });
it("error while parsing - compile", () => { try { eta.compile("template <%"); } catch (ex) { expect(ex).toBeInstanceOf(EtaError); expect(ex).toBeInstanceOf(EtaParseError); expect((ex as EtaParseError).name).toBe("EtaParser Error"); expect((ex as EtaParseError).message).toBe(`unclosed tag at line 1 col 10:
template <% ^`); expect(ex instanceof Error).toBe(true); } });});
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 { eta.render("./runtime-error", {}); } catch (ex) { expect(ex).toBeInstanceOf(EtaError); expect(ex).toBeInstanceOf(EtaRuntimeError); expect((ex as EtaRuntimeError).name).toBe("ReferenceError"); expect((ex as EtaRuntimeError).message).toBe(`${errorFilepath}:2 1| >> 2| <%= undefinedVariable %> 3| Lorem Ipsum
undefinedVariable is not defined`); } });});
describe("EtaFileResolutionError", () => { it("error throws correctly when template does not exist", () => { const eta = new Eta({ debug: true, views: path.join(__dirname, "templates") }); const errorFilepath = path.join(__dirname, "templates/not-existing-template.eta");
try { eta.render("./not-existing-template", {}); } catch (ex) { expect(ex).toBeInstanceOf(EtaError); expect(ex).toBeInstanceOf(EtaFileResolutionError); expect((ex as EtaFileResolutionError).name).toBe("EtaFileResolution Error"); expect((ex as EtaFileResolutionError).message).toBe( `Could not find template: ${errorFilepath}` ); } });
it("error throws correctly when views options is missing", async () => { const eta = new Eta({ debug: true }); try { eta.render("Hi", {}); } catch (ex) { expect(ex).toBeInstanceOf(EtaFileResolutionError); expect((ex as EtaFileResolutionError).name).toBe("EtaFileResolution Error"); expect((ex as EtaFileResolutionError).message).toBe("Views directory is not defined"); }
try { await eta.renderAsync("Hi", {}); } catch (ex) { expect(ex).toBeInstanceOf(EtaFileResolutionError); expect((ex as EtaFileResolutionError).name).toBe("EtaFileResolution Error"); expect((ex as EtaFileResolutionError).message).toBe("Views directory is not defined"); } });
it("error throws correctly when template in not in th view directory", () => { const eta = new Eta({ debug: true, views: path.join(__dirname, "templates") });
const filePath = "../../../simple.eta"; try { eta.render(filePath, {}); } catch (ex) { expect(ex).toBeInstanceOf(EtaFileResolutionError); expect((ex as EtaFileResolutionError).name).toBe("EtaFileResolution Error"); expect((ex as EtaFileResolutionError).message).toBe( `Template '${filePath}' is not in the views directory` ); } });});
describe("EtaNameResolutionError", () => { const eta = new Eta({ debug: true, views: path.join(__dirname, "templates") });
it("error throws correctly", () => { const template = "@not-existing-tp";
try { eta.render(template, {}); } catch (ex) { expect(ex).toBeInstanceOf(EtaNameResolutionError); expect((ex as EtaNameResolutionError).name).toBe("EtaNameResolution Error"); expect((ex as EtaNameResolutionError).message).toBe(`Failed to get template '${template}'`); } });});
eta

Version Info

Tagged at
2 months ago