Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/util/common/test/testUtils.spec.ts
13401 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { describe, expect, it, vi } from 'vitest';
7
import { DisposableStore } from '../../vs/base/common/lifecycle';
8
import { throwIfDisposablesAreLeaked, throwIfDisposablesAreLeakedAsync } from './testUtils';
9
10
describe('testUtils', () => {
11
describe('throwIfDisposablesAreLeaked', () => {
12
it('should not throw when no disposables are leaked', () => {
13
expect(() => {
14
throwIfDisposablesAreLeaked(() => {
15
// Create and properly dispose a disposable
16
const store = new DisposableStore();
17
store.add({ dispose: () => { } });
18
store.dispose();
19
});
20
}).not.toThrow();
21
});
22
23
it('should throw when disposables are leaked', () => {
24
// suppress the console.error when it expectedly fails
25
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => { });
26
try {
27
expect(() => {
28
throwIfDisposablesAreLeaked(() => {
29
// Create a disposable but don't dispose it
30
const store = new DisposableStore();
31
store.add({ dispose: () => { } });
32
// Don't call store.dispose()
33
});
34
}).toThrow(/There are \d+ undisposed disposables!/);
35
} finally {
36
consoleSpy.mockRestore();
37
}
38
});
39
});
40
41
describe('throwIfDisposablesAreLeakedAsync', () => {
42
it('should not throw when no disposables are leaked in async context', async () => {
43
await expect(
44
throwIfDisposablesAreLeakedAsync(async () => {
45
const store = new DisposableStore();
46
store.add({ dispose: () => { } });
47
store.dispose();
48
})
49
).resolves.not.toThrow();
50
});
51
52
it('should throw when disposables are leaked in async context', async () => {
53
// suppress the console.error when it expectedly fails
54
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => { });
55
try {
56
await expect(
57
throwIfDisposablesAreLeakedAsync(async () => {
58
const store = new DisposableStore();
59
store.add({ dispose: () => { } });
60
// Don't dispose
61
})
62
).rejects.toThrow(/There are \d+ undisposed disposables!/);
63
} finally {
64
consoleSpy.mockRestore();
65
}
66
});
67
68
it('should work with async operations', async () => {
69
await expect(
70
throwIfDisposablesAreLeakedAsync(async () => {
71
const store = new DisposableStore();
72
store.add({ dispose: () => { } });
73
74
// Simulate async work
75
await new Promise(resolve => setTimeout(resolve, 1));
76
77
store.dispose();
78
})
79
).resolves.not.toThrow();
80
});
81
});
82
});
83