forked from microsoft/vscode-python-environments
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasyncUtils.unit.test.ts
More file actions
69 lines (57 loc) · 2.5 KB
/
asyncUtils.unit.test.ts
File metadata and controls
69 lines (57 loc) · 2.5 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
import assert from 'assert';
import * as sinon from 'sinon';
import * as logging from '../../common/logging';
import { safeRegister } from '../../common/utils/asyncUtils';
suite('safeRegister', () => {
let traceErrorStub: sinon.SinonStub;
setup(() => {
traceErrorStub = sinon.stub(logging, 'traceError');
});
teardown(() => {
sinon.restore();
});
test('resolves when the task succeeds', async () => {
await safeRegister('test-manager', Promise.resolve());
assert.ok(traceErrorStub.notCalled, 'traceError should not be called on success');
});
test('resolves (not rejects) when the task fails', async () => {
const failing = Promise.reject(new Error('boom'));
// safeRegister must not propagate the rejection
await safeRegister('failing-manager', failing);
// If we got here without throwing, the test passes
});
test('logs the manager name and error when the task fails', async () => {
const error = new Error('registration exploded');
await safeRegister('conda', Promise.reject(error));
assert.ok(traceErrorStub.calledOnce, 'traceError should be called once');
const [message, loggedError] = traceErrorStub.firstCall.args;
assert.ok(message.includes('conda'), 'log message should contain the manager name');
assert.strictEqual(loggedError, error, 'original error should be passed through');
});
test('independent tasks continue when one fails', async () => {
const results: string[] = [];
await Promise.all([
safeRegister('will-fail', Promise.reject(new Error('fail'))),
safeRegister(
'will-succeed-1',
Promise.resolve().then(() => {
results.push('a');
}),
),
safeRegister(
'will-succeed-2',
Promise.resolve().then(() => {
results.push('b');
}),
),
]);
assert.deepStrictEqual(results.sort(), ['a', 'b'], 'both successful tasks should complete');
assert.ok(traceErrorStub.calledOnce, 'only the failing task should log an error');
});
test('handles non-Error rejections', async () => {
await safeRegister('string-reject', Promise.reject('just a string'));
assert.ok(traceErrorStub.calledOnce);
const [, loggedError] = traceErrorStub.firstCall.args;
assert.strictEqual(loggedError, 'just a string');
});
});