Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/inlineEdits/test/vscode-node/raceAndAll.spec.ts
13405 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 { expect, suite, test, vi } from 'vitest';
7
import { raceAndAll } from '../../vscode-node/raceAndAll';
8
9
const noopErrorHandler = () => { };
10
11
suite('raceAndAll', () => {
12
13
test('first resolves with the first settled promise', async () => {
14
const { first } = raceAndAll([
15
Promise.resolve('a'),
16
new Promise<string>(resolve => setTimeout(() => resolve('b'), 5)),
17
], noopErrorHandler);
18
const result = await first;
19
expect(result).toEqual(['a', undefined]);
20
});
21
22
test('all resolves with all values', async () => {
23
const { all } = raceAndAll([
24
Promise.resolve('a'),
25
Promise.resolve('b'),
26
], noopErrorHandler);
27
const result = await all;
28
expect(result).toEqual(['a', 'b']);
29
});
30
31
test('when first promise rejects, first still resolves with the second promise', async () => {
32
const { first, all } = raceAndAll([
33
Promise.reject(new Error('fail')),
34
Promise.resolve('b'),
35
], noopErrorHandler);
36
const allResult = expect(all).rejects.toThrow('fail');
37
const result = await first;
38
expect(result).toEqual([undefined, 'b']);
39
await allResult;
40
});
41
42
test('when second promise rejects, first resolves with the first promise', async () => {
43
const { first, all } = raceAndAll([
44
Promise.resolve('a'),
45
Promise.reject(new Error('fail')),
46
], noopErrorHandler);
47
const allResult = expect(all).rejects.toThrow('fail');
48
const result = await first;
49
expect(result).toEqual(['a', undefined]);
50
await allResult;
51
});
52
53
test('when all promises reject, first rejects', async () => {
54
const { first, all } = raceAndAll([
55
Promise.reject(new Error('fail1')),
56
Promise.reject(new Error('fail2')),
57
], noopErrorHandler);
58
await expect(first).rejects.toThrow('All promises passed to raceAndAll were rejected');
59
await expect(all).rejects.toThrow('fail1');
60
});
61
62
test('when first promise rejects and second resolves later, first waits for second', async () => {
63
const { first, all } = raceAndAll([
64
Promise.reject(new Error('fail')),
65
new Promise<string>(resolve => setTimeout(() => resolve('b'), 5)),
66
], noopErrorHandler);
67
const allResult = expect(all).rejects.toThrow('fail');
68
const result = await first;
69
expect(result).toEqual([undefined, 'b']);
70
await allResult;
71
});
72
73
test('errorHandler is called for each rejection', async () => {
74
const errorHandler = vi.fn();
75
const err1 = new Error('fail1');
76
const err2 = new Error('fail2');
77
const { first, all } = raceAndAll([
78
Promise.reject(err1),
79
Promise.reject(err2),
80
], errorHandler);
81
const allResult = expect(all).rejects.toThrow('fail1');
82
await expect(first).rejects.toThrow();
83
await allResult;
84
expect(errorHandler).toHaveBeenCalledTimes(2);
85
expect(errorHandler).toHaveBeenCalledWith(err1);
86
expect(errorHandler).toHaveBeenCalledWith(err2);
87
});
88
});
89
90