forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-domexception-subclass.js
More file actions
56 lines (46 loc) · 1.6 KB
/
test-domexception-subclass.js
File metadata and controls
56 lines (46 loc) · 1.6 KB
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
'use strict';
require('../common');
const assert = require('assert');
const { isNativeError } = require('util/types');
class MyDOMException extends DOMException {
ownProp;
#reason;
constructor() {
super('my message', 'NotFoundError');
this.ownProp = 'bar';
this.#reason = 'hello';
}
get reason() {
return this.#reason;
}
}
const myException = new MyDOMException();
// Verifies the prototype chain
assert(myException instanceof MyDOMException);
assert(myException instanceof DOMException);
assert(myException instanceof Error);
// Verifies [[ErrorData]]
assert(isNativeError(myException));
// Verifies subclass properties
assert(Object.hasOwn(myException, 'ownProp'));
assert(!Object.hasOwn(myException, 'reason'));
assert.strictEqual(myException.reason, 'hello');
// Verifies error properties
assert.strictEqual(myException.name, 'NotFoundError');
assert.strictEqual(myException.code, 8);
assert.strictEqual(myException.message, 'my message');
assert.strictEqual(typeof myException.stack, 'string');
// Verify structuredClone only copies known error properties.
const cloned = structuredClone(myException);
assert(!(cloned instanceof MyDOMException));
assert(cloned instanceof DOMException);
assert(cloned instanceof Error);
assert(isNativeError(cloned));
// Verify custom properties
assert(!Object.hasOwn(cloned, 'ownProp'));
assert.strictEqual(cloned.reason, undefined);
// Verify cloned error properties
assert.strictEqual(cloned.name, 'NotFoundError');
assert.strictEqual(cloned.code, 8);
assert.strictEqual(cloned.message, 'my message');
assert.strictEqual(cloned.stack, myException.stack);