11/* global it, expect, describe */
22
33import path from "path" ;
4- import { Eta } from "../src/index" ;
4+ import {
5+ Eta ,
6+ EtaError ,
7+ EtaParseError ,
8+ EtaRuntimeError ,
9+ EtaFileResolutionError ,
10+ EtaNameResolutionError ,
11+ } from "../src/index" ;
512
613describe ( "ParseErr" , ( ) => {
714 const eta = new Eta ( ) ;
815
9- it ( "error while parsing" , ( ) => {
16+ it ( "error while parsing - renderString " , ( ) => {
1017 try {
1118 eta . renderString ( "template <%" , { } ) ;
1219 } catch ( ex ) {
13- expect ( ( ex as Error ) . name ) . toBe ( "Eta Error" ) ;
14- expect ( ( ex as Error ) . message ) . toBe ( `unclosed tag at line 1 col 10:
20+ expect ( ex ) . toBeInstanceOf ( EtaError ) ;
21+ expect ( ex ) . toBeInstanceOf ( EtaParseError ) ;
22+ expect ( ( ex as EtaParseError ) . name ) . toBe ( "EtaParser Error" ) ;
23+ expect ( ( ex as EtaParseError ) . message ) . toBe ( `unclosed tag at line 1 col 10:
24+
25+ template <%
26+ ^` ) ;
27+ expect ( ex instanceof Error ) . toBe ( true ) ;
28+ }
29+ } ) ;
30+
31+ it ( "error while parsing - compile" , ( ) => {
32+ try {
33+ eta . compile ( "template <%" ) ;
34+ } catch ( ex ) {
35+ expect ( ex ) . toBeInstanceOf ( EtaError ) ;
36+ expect ( ex ) . toBeInstanceOf ( EtaParseError ) ;
37+ expect ( ( ex as EtaParseError ) . name ) . toBe ( "EtaParser Error" ) ;
38+ expect ( ( ex as EtaParseError ) . message ) . toBe ( `unclosed tag at line 1 col 10:
1539
1640 template <%
1741 ^` ) ;
@@ -29,8 +53,10 @@ describe("RuntimeErr", () => {
2953 try {
3054 eta . render ( "./runtime-error" , { } ) ;
3155 } catch ( ex ) {
32- expect ( ( ex as Error ) . name ) . toBe ( "ReferenceError" ) ;
33- expect ( ( ex as Error ) . message ) . toBe ( `${ errorFilepath } :2
56+ expect ( ex ) . toBeInstanceOf ( EtaError ) ;
57+ expect ( ex ) . toBeInstanceOf ( EtaRuntimeError ) ;
58+ expect ( ( ex as EtaRuntimeError ) . name ) . toBe ( "ReferenceError" ) ;
59+ expect ( ( ex as EtaRuntimeError ) . message ) . toBe ( `${ errorFilepath } :2
3460 1|
3561 >> 2| <%= undefinedVariable %>
3662 3| Lorem Ipsum
@@ -39,3 +65,71 @@ undefinedVariable is not defined`);
3965 }
4066 } ) ;
4167} ) ;
68+
69+ describe ( "EtaFileResolutionError" , ( ) => {
70+ it ( "error throws correctly when template does not exist" , ( ) => {
71+ const eta = new Eta ( { debug : true , views : path . join ( __dirname , "templates" ) } ) ;
72+ const errorFilepath = path . join ( __dirname , "templates/not-existing-template.eta" ) ;
73+
74+ try {
75+ eta . render ( "./not-existing-template" , { } ) ;
76+ } catch ( ex ) {
77+ expect ( ex ) . toBeInstanceOf ( EtaError ) ;
78+ expect ( ex ) . toBeInstanceOf ( EtaFileResolutionError ) ;
79+ expect ( ( ex as EtaFileResolutionError ) . name ) . toBe ( "EtaFileResolution Error" ) ;
80+ expect ( ( ex as EtaFileResolutionError ) . message ) . toBe (
81+ `Could not find template: ${ errorFilepath } `
82+ ) ;
83+ }
84+ } ) ;
85+
86+ it ( "error throws correctly when views options is missing" , async ( ) => {
87+ const eta = new Eta ( { debug : true } ) ;
88+ try {
89+ eta . render ( "Hi" , { } ) ;
90+ } catch ( ex ) {
91+ expect ( ex ) . toBeInstanceOf ( EtaFileResolutionError ) ;
92+ expect ( ( ex as EtaFileResolutionError ) . name ) . toBe ( "EtaFileResolution Error" ) ;
93+ expect ( ( ex as EtaFileResolutionError ) . message ) . toBe ( "Views directory is not defined" ) ;
94+ }
95+
96+ try {
97+ await eta . renderAsync ( "Hi" , { } ) ;
98+ } catch ( ex ) {
99+ expect ( ex ) . toBeInstanceOf ( EtaFileResolutionError ) ;
100+ expect ( ( ex as EtaFileResolutionError ) . name ) . toBe ( "EtaFileResolution Error" ) ;
101+ expect ( ( ex as EtaFileResolutionError ) . message ) . toBe ( "Views directory is not defined" ) ;
102+ }
103+ } ) ;
104+
105+ it ( "error throws correctly when template in not in th view directory" , ( ) => {
106+ const eta = new Eta ( { debug : true , views : path . join ( __dirname , "templates" ) } ) ;
107+
108+ const filePath = "../../../simple.eta" ;
109+ try {
110+ eta . render ( filePath , { } ) ;
111+ } catch ( ex ) {
112+ expect ( ex ) . toBeInstanceOf ( EtaFileResolutionError ) ;
113+ expect ( ( ex as EtaFileResolutionError ) . name ) . toBe ( "EtaFileResolution Error" ) ;
114+ expect ( ( ex as EtaFileResolutionError ) . message ) . toBe (
115+ `Template '${ filePath } ' is not in the views directory`
116+ ) ;
117+ }
118+ } ) ;
119+ } ) ;
120+
121+ describe ( "EtaNameResolutionError" , ( ) => {
122+ const eta = new Eta ( { debug : true , views : path . join ( __dirname , "templates" ) } ) ;
123+
124+ it ( "error throws correctly" , ( ) => {
125+ const template = "@not-existing-tp" ;
126+
127+ try {
128+ eta . render ( template , { } ) ;
129+ } catch ( ex ) {
130+ expect ( ex ) . toBeInstanceOf ( EtaNameResolutionError ) ;
131+ expect ( ( ex as EtaNameResolutionError ) . name ) . toBe ( "EtaNameResolution Error" ) ;
132+ expect ( ( ex as EtaNameResolutionError ) . message ) . toBe ( `Failed to get template '${ template } '` ) ;
133+ }
134+ } ) ;
135+ } ) ;
0 commit comments