-
-
Notifications
You must be signed in to change notification settings - Fork 481
/
EleventyErrorUtilTest.js
49 lines (41 loc) · 1.71 KB
/
EleventyErrorUtilTest.js
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
import test from "ava";
import EleventyErrorUtil from "../src/Errors/EleventyErrorUtil.js";
const SAMPLE_ERROR = new Error("Nothing to see here");
const { cleanMessage, hasEmbeddedError, convertErrorToString, deconvertErrorToObject } =
EleventyErrorUtil;
test("hasEmbeddedError()", (t) => {
t.false(hasEmbeddedError(""));
t.true(hasEmbeddedError(convertErrorToString(SAMPLE_ERROR)));
});
test("cleanMessage()", (t) => {
t.is(cleanMessage(null), "");
t.is(cleanMessage(undefined), "");
t.is(cleanMessage(false), "");
t.is(cleanMessage(""), "");
const text = "I am the very model of a sample text input";
t.is(cleanMessage(text), text);
t.is(cleanMessage(text + convertErrorToString(SAMPLE_ERROR)), text);
});
test("deconvertErrorToObject() should throw on invalid inputs", (t) => {
t.throws(() => deconvertErrorToObject(undefined), {
message: "Could not convert error object from: undefined",
});
t.throws(() => deconvertErrorToObject(""), {
message: "Could not convert error object from: ",
});
t.throws(() => deconvertErrorToObject("Not an error"), {
message: "Could not convert error object from: Not an error",
});
});
test("deconvertErrorToObject() should return its argument if it does not contain another error", (t) => {
t.is(deconvertErrorToObject(SAMPLE_ERROR), SAMPLE_ERROR);
});
test("deconvertErrorToObject() should get message and stack from convertErrorToString()", (t) => {
const nestingError = new Error(
"This error contains a sample error: " + convertErrorToString(SAMPLE_ERROR)
);
const result = deconvertErrorToObject(nestingError);
t.is(result.name, nestingError.name);
t.is(result.message, SAMPLE_ERROR.message);
t.is(result.stack, SAMPLE_ERROR.stack);
});