heyujie
2021-06-07 8f8155aa4c83f7c2577ac123add550766b6a7ce3
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
"use strict";
 
var assert          = require("chai").assert
  , handleException = require("../../lib/resolve-exception");
 
describe("lib/handle-exception", function () {
    it("Should throw TypeError", function () {
        try {
            handleException(12, "Invalid value");
            throw new Error("Unexpected");
        } catch (error) {
            assert.equal(error.name, "TypeError");
            assert.equal(error.message, "Invalid value");
        }
    });
    it("Should resolve value in default message", function () {
        try {
            handleException(12, "%v is invalid", {});
            throw new Error("Unexpected");
        } catch (error) {
            assert.equal(error.message, "12 is invalid");
        }
    });
    it("Should support optional values via inputOptions.isOptional", function () {
        assert.equal(handleException(null, "%v is invalid", { isOptional: true }, null));
    });
    it("Should support optional values via inputOptions.default", function () {
        // prettier-ignore
        assert.equal(handleException(null, "%v is invalid", { "default": "bar" }), "bar");
    });
    it("Should support custome error message via inputOptions.errorMessage", function () {
        try {
            handleException(null, "%v is invalid", { errorMessage: "%v is not supported age" });
            throw new Error("Unexpected");
        } catch (error) {
            assert.equal(error.message, "null is not supported age");
        }
    });
});