forked from zloirock/core-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathes.error.cause.js
58 lines (45 loc) · 2.63 KB
/
es.error.cause.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
/* eslint-disable sonarjs/inconsistent-function-call -- required for testing */
import { GLOBAL, PROTO } from '../helpers/constants.js';
const { create } = Object;
function runErrorTestCase($Error, ERROR_NAME) {
QUnit.test(`${ ERROR_NAME } constructor with 'cause' param`, assert => {
assert.isFunction($Error);
assert.arity($Error, 1);
assert.name($Error, ERROR_NAME);
assert.looksNative($Error);
if (PROTO && $Error !== Error) {
// eslint-disable-next-line no-prototype-builtins -- safe
assert.true(Error.isPrototypeOf($Error), 'constructor has `Error` in the prototype chain');
}
assert.same($Error.prototype.constructor, $Error, 'prototype constructor');
// eslint-disable-next-line no-prototype-builtins -- safe
assert.false($Error.prototype.hasOwnProperty('cause'), 'prototype has not cause');
assert.true($Error(1) instanceof $Error, 'no cause, without new');
assert.true(new $Error(1) instanceof $Error, 'no cause, with new');
assert.true($Error(1, {}) instanceof $Error, 'with options, without new');
assert.true(new $Error(1, {}) instanceof $Error, 'with options, with new');
assert.true($Error(1, 'foo') instanceof $Error, 'non-object options, without new');
assert.true(new $Error(1, 'foo') instanceof $Error, 'non-object options, with new');
assert.same($Error(1, { cause: 7 }).cause, 7, 'cause, without new');
assert.same(new $Error(1, { cause: 7 }).cause, 7, 'cause, with new');
assert.same($Error(1, create({ cause: 7 })).cause, 7, 'prototype cause, without new');
assert.same(new $Error(1, create({ cause: 7 })).cause, 7, 'prototype cause, with new');
let error = $Error(1, { cause: 7 });
assert.same(error.name, ERROR_NAME, 'instance name');
assert.same(error.message, '1', 'instance message');
assert.same(error.cause, 7, 'instance cause');
// eslint-disable-next-line no-prototype-builtins -- safe
assert.true(error.hasOwnProperty('cause'), 'cause is own');
error = $Error();
assert.same(error.message, '', 'default instance message');
assert.same(error.cause, undefined, 'default instance cause undefined');
// eslint-disable-next-line no-prototype-builtins -- safe
assert.false(error.hasOwnProperty('cause'), 'default instance cause missed');
});
}
for (const ERROR_NAME of ['Error', 'EvalError', 'RangeError', 'ReferenceError', 'SyntaxError', 'TypeError', 'URIError']) {
runErrorTestCase(GLOBAL[ERROR_NAME], ERROR_NAME);
}
if (GLOBAL.WebAssembly) for (const ERROR_NAME of ['CompileError', 'LinkError', 'RuntimeError']) {
if (GLOBAL.WebAssembly[ERROR_NAME]) runErrorTestCase(GLOBAL.WebAssembly[ERROR_NAME], ERROR_NAME);
}