forked from jsdom/jsdom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchai-helpers.js
60 lines (50 loc) · 1.7 KB
/
chai-helpers.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
50
51
52
53
54
55
56
57
58
59
60
"use strict";
const DOMException = require("domexception");
module.exports = (chai, util) => {
const { assert, Assertion } = chai;
const { flag } = util;
assert.throwsDomException = (fn, document, name, message) => {
const assertErr = new Assertion(fn, message).to.throwDomException(document, name);
return flag(assertErr, "object");
};
Assertion.addMethod("throwDomException", function (document, name, message) {
if (message) {
flag(this, "message", message);
}
const fn = flag(this, "object");
const expectedCode = new DOMException("", name).code;
new Assertion(fn, message).is.a("function");
let thrownError = null;
try {
fn();
} catch (error) {
thrownError = error;
}
this.assert(
thrownError &&
thrownError instanceof document.defaultView.DOMException &&
thrownError.name === name &&
thrownError.code === expectedCode,
`expected #{this} to throw a window.DOMException with name '${name}' and code '${expectedCode}' but ` +
(thrownError ?
`#{act} was thrown with name '${thrownError.name}' and code '${thrownError.code}'` :
`nothing was thrown`)
,
`expected #{this} to not throw a window.DOMException with name '${name}' and code '${expectedCode}'`,
"window.DOMException",
thrownError instanceof Error ? thrownError.toString() : thrownError
);
// for chaining
flag(this, "object", thrownError);
});
assert.isRejected = (promise, errorType) => {
return promise.then(
() => "expected promise to reject, not fulfill",
reason => {
if (errorType) {
assert.strictEqual(reason.name, errorType.name);
}
}
);
};
};