-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathdisposable.test.ts
More file actions
280 lines (248 loc) · 8.88 KB
/
Copy pathdisposable.test.ts
File metadata and controls
280 lines (248 loc) · 8.88 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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import { afterEach, expect, vi } from 'vitest';
import {
AsyncDisposableStack,
DisposableStack,
asyncDisposeSymbol,
disposeSymbol,
unwrapSuppressedErrors,
} from './index';
describe('disposable', () => {
describe('disposeSymbol / asyncDisposeSymbol', () => {
it('should export disposeSymbol as a symbol', () => {
expect(typeof disposeSymbol).to.equal('symbol');
});
it('should export asyncDisposeSymbol as a symbol', () => {
expect(typeof asyncDisposeSymbol).to.equal('symbol');
});
it('should let `[disposeSymbol]() {}` class syntax key methods correctly', () => {
class Disposable {
public ran = false;
[disposeSymbol]() {
this.ran = true;
}
}
const instance = new Disposable();
instance[disposeSymbol]();
expect(instance.ran).to.equal(true);
});
it('should let DisposableStack#use find a method keyed on disposeSymbol', () => {
let ran = false;
const stack = new DisposableStack();
stack.use({
[disposeSymbol]() {
ran = true;
},
});
stack.dispose();
expect(ran).to.equal(true);
});
});
describe('exports', () => {
it('should export a constructable DisposableStack', () => {
expect(typeof DisposableStack).to.equal('function');
expect(new DisposableStack().disposed).to.equal(false);
});
it('should export a constructable AsyncDisposableStack', () => {
expect(typeof AsyncDisposableStack).to.equal('function');
expect(new AsyncDisposableStack().disposed).to.equal(false);
});
});
describe('unwrapSuppressedErrors', () => {
it('should return a single-element array for a non-SuppressedError', () => {
const error = new Error('plain');
expect(unwrapSuppressedErrors(error)).to.deep.equal([error]);
});
it('should flatten a SuppressedError chain outermost-first', () => {
const innermost = new Error('inner');
const middle = { error: new Error('middle'), suppressed: innermost };
const outer = { error: new Error('outer'), suppressed: middle };
const result = unwrapSuppressedErrors(outer);
expect(result).to.have.lengthOf(3);
expect((result[0] as Error).message).to.equal('outer');
expect((result[1] as Error).message).to.equal('middle');
expect((result[2] as Error).message).to.equal('inner');
});
it('should unwrap a real SuppressedError thrown by DisposableStack.dispose', () => {
const stack = new DisposableStack();
stack.defer(() => {
throw new Error('first');
});
stack.defer(() => {
throw new Error('second');
});
let caught: unknown;
try {
stack.dispose();
} catch (error) {
caught = error;
}
const result = unwrapSuppressedErrors(caught);
const messages = result.map((entry) => (entry as Error).message);
expect(messages).to.include('first');
expect(messages).to.include('second');
});
});
});
// Exercises the built-in fallback classes (not the native `globalThis.DisposableStack`)
// by stubbing the global away and re-importing the module, so the `?? fallback`
// branch is the one under test.
describe('disposable — built-in fallback', () => {
afterEach(() => {
vi.unstubAllGlobals();
vi.resetModules();
});
async function loadFallback({ withoutSuppressedError = false } = {}) {
vi.stubGlobal('DisposableStack', undefined);
vi.stubGlobal('AsyncDisposableStack', undefined);
if (withoutSuppressedError) {
vi.stubGlobal('SuppressedError', undefined);
}
vi.resetModules();
return import('./index');
}
describe('DisposableStack', () => {
it('should be constructable and start undisposed', async () => {
const mod = await loadFallback();
const stack = new mod.DisposableStack();
expect(typeof mod.DisposableStack).to.equal('function');
expect(stack.disposed).to.equal(false);
stack.dispose();
expect(stack.disposed).to.equal(true);
});
it('should run `use` disposers (keyed on disposeSymbol) in LIFO order', async () => {
const mod = await loadFallback();
const order: string[] = [];
const stack = new mod.DisposableStack();
stack.use({ [mod.disposeSymbol]: () => order.push('a') });
stack.use({ [mod.disposeSymbol]: () => order.push('b') });
stack.dispose();
expect(order).to.deep.equal(['b', 'a']);
});
it('should return the value from `use` and treat null/undefined as a no-op', async () => {
const mod = await loadFallback();
const stack = new mod.DisposableStack();
const resource = { [mod.disposeSymbol]: () => {} };
expect(stack.use(resource)).to.equal(resource);
expect(stack.use(null)).to.equal(null);
expect(stack.use(undefined)).to.equal(undefined);
expect(() => stack.dispose()).not.to.throw();
});
it('should throw a TypeError when `use` receives a non-disposable', async () => {
const mod = await loadFallback();
const stack = new mod.DisposableStack();
expect(() => stack.use({} as any)).to.throw(TypeError);
});
it('should pass the value to the `adopt` callback on dispose', async () => {
const mod = await loadFallback();
let received: unknown;
const stack = new mod.DisposableStack();
expect(
stack.adopt(42, (value) => {
received = value;
}),
).to.equal(42);
stack.dispose();
expect(received).to.equal(42);
});
it('should dispose only once (idempotent)', async () => {
const mod = await loadFallback();
let count = 0;
const stack = new mod.DisposableStack();
stack.defer(() => {
count += 1;
});
stack.dispose();
stack.dispose();
expect(count).to.equal(1);
});
it('should transfer ownership with `move` and dispose the original', async () => {
const mod = await loadFallback();
const order: string[] = [];
const stack = new mod.DisposableStack();
stack.defer(() => order.push('x'));
const moved = stack.move();
expect(stack.disposed).to.equal(true);
expect(moved.disposed).to.equal(false);
stack.dispose();
expect(order).to.deep.equal([]);
moved.dispose();
expect(order).to.deep.equal(['x']);
});
it('should throw when registering on an already-disposed stack', async () => {
const mod = await loadFallback();
const stack = new mod.DisposableStack();
stack.dispose();
expect(() => stack.use({ [mod.disposeSymbol]: () => {} })).to.throw();
expect(() => stack.defer(() => {})).to.throw();
expect(() => stack.adopt(1, () => {})).to.throw();
expect(() => stack.move()).to.throw();
});
it('should aggregate disposer failures into a SuppressedError chain', async () => {
const mod = await loadFallback();
const stack = new mod.DisposableStack();
stack.defer(() => {
throw new Error('first');
});
stack.defer(() => {
throw new Error('second');
});
let caught: unknown;
try {
stack.dispose();
} catch (error) {
caught = error;
}
const messages = mod.unwrapSuppressedErrors(caught).map((entry) => (entry as Error).message);
expect(messages).to.include('first');
expect(messages).to.include('second');
});
it('should aggregate failures even when `SuppressedError` is unavailable', async () => {
const mod = await loadFallback({ withoutSuppressedError: true });
const stack = new mod.DisposableStack();
stack.defer(() => {
throw new Error('first');
});
stack.defer(() => {
throw new Error('second');
});
let caught: unknown;
try {
stack.dispose();
} catch (error) {
caught = error;
}
const messages = mod.unwrapSuppressedErrors(caught).map((entry) => (entry as Error).message);
expect(messages).to.include('first');
expect(messages).to.include('second');
});
});
describe('AsyncDisposableStack', () => {
it('should dispose async resources in LIFO order', async () => {
const mod = await loadFallback();
const order: string[] = [];
const stack = new mod.AsyncDisposableStack();
stack.defer(async () => {
order.push('a');
});
stack.defer(async () => {
order.push('b');
});
expect(stack.disposed).to.equal(false);
await stack.disposeAsync();
expect(stack.disposed).to.equal(true);
expect(order).to.deep.equal(['b', 'a']);
});
it('should let `use` fall back to the sync dispose symbol', async () => {
const mod = await loadFallback();
let ran = false;
const stack = new mod.AsyncDisposableStack();
stack.use({
[mod.disposeSymbol]: () => {
ran = true;
},
});
await stack.disposeAsync();
expect(ran).to.equal(true);
});
});
});