-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.js
97 lines (77 loc) · 2.49 KB
/
test.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import {EventEmitter} from 'node:events'
import assert from 'node:assert/strict'
import test from 'node:test'
import {unherit} from './index.js'
test('unherit(Super)', () => {
let Emitter = unherit(EventEmitter)
// @ts-expect-error: TS is wrong, it does exist.
assert.equal(Emitter.prototype.defaultMaxListeners, undefined)
// @ts-expect-error: TS is wrong, it does exist.
Emitter.prototype.defaultMaxListeners = 0
// @ts-expect-error
assert.equal(new Emitter().defaultMaxListeners, 0, 'should work (1)')
assert.equal(
// @ts-expect-error
new EventEmitter().defaultMaxListeners,
undefined,
'should work (2)'
)
assert.equal(new Emitter().constructor, Emitter, 'should work (3)')
assert.equal(new EventEmitter().constructor, EventEmitter, 'should work (4)')
Emitter = unherit(EventEmitter)
assert.ok(
new Emitter() instanceof EventEmitter,
'should fool `instanceof` checks'
)
class A {
/**
* Constructor which internally uses an `instanceof` check.
*
* @constructor
* @param {string} one
* @param {string} two
* @param {string} three
*/
constructor(one, two, three) {
assert.equal(one, 'foo')
assert.equal(two, 'bar')
assert.equal(three, 'baz')
assert.ok(this instanceof A)
/** @type {unknown[]} */
this.values = [one, two, three]
}
}
const B = unherit(A)
const b = new B('foo', 'bar', 'baz')
assert.ok(b instanceof A, 'should support classes (1)')
assert.ok(b instanceof B, 'should support classes (2)')
assert.deepEqual(b.values, ['foo', 'bar', 'baz'], 'support classes (3)')
/** @type {(() => void) & {prototype: {values: unknown[]}}} */
function C() {}
// TS doesn’t like `prototype`s.
// type-coverage:ignore-next-line
C.prototype.values = [1, 2]
class Proto extends C {}
function D() {}
D.prototype = new Proto()
D.prototype.values = [1, 2, 3]
// @ts-expect-error: correct, it does not exist.
D.prototype.object = {a: true}
const E = unherit(D)
// This failed in 1.0.4
assert.deepEqual(
E.prototype.values,
[1, 2, 3],
'shouldn’t fail on inheritance (1)'
)
assert.deepEqual(
new E().values,
[1, 2, 3],
'shouldn’t fail on inheritance (2)'
)
E.prototype.values.push(4)
const F = unherit(D)
assert.deepEqual(F.prototype.values, [1, 2, 3], 'should clone values (1)')
assert.deepEqual(new F().values, [1, 2, 3], 'should clone values (2)')
assert.deepEqual(new F().object, {a: true}, 'should clone values (3)')
})