Skip to content

Commit d8105db

Browse files
committed
fix: add timeout tests
1 parent a040dfa commit d8105db

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

packages/device-id/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@
2424
"license": "Apache-2.0",
2525
"main": "dist/index.js",
2626
"exports": {
27-
"require": "./dist/index.js",
28-
"import": "./dist/.esm-wrapper.mjs"
27+
".": {
28+
"require": "./dist/index.js",
29+
"import": "./dist/.esm-wrapper.mjs",
30+
"types": "./dist/index.d.ts"
31+
}
2932
},
3033
"types": "./dist/index.d.ts",
3134
"scripts": {

packages/device-id/src/get-device-id.spec.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,72 @@ describe('getDeviceId', function () {
8282

8383
expect(resultA).to.equal(resultB);
8484
});
85+
86+
it('handles timeout when getting machine id', async () => {
87+
let timeoutId: NodeJS.Timeout;
88+
const getMachineId = () =>
89+
new Promise<string>((resolve) => {
90+
timeoutId = setTimeout(() => resolve('delayed-id'), 10_000);
91+
});
92+
93+
let errorCalled = false;
94+
const result = await getDeviceId({
95+
getMachineId,
96+
isNodeMachineId: false,
97+
onError: () => {
98+
errorCalled = true;
99+
},
100+
timeout: 1,
101+
}).value;
102+
103+
clearTimeout(timeoutId!);
104+
expect(result).to.equal('unknown');
105+
});
106+
107+
it('handles external promise resolution', async () => {
108+
let timeoutId: NodeJS.Timeout;
109+
const getMachineId = () =>
110+
new Promise<string>((resolve) => {
111+
timeoutId = setTimeout(() => resolve('delayed-id'), 10_000);
112+
});
113+
114+
const { resolve, value } = getDeviceId({
115+
getMachineId,
116+
isNodeMachineId: false,
117+
});
118+
119+
resolve('external-id');
120+
121+
const result = await value;
122+
123+
clearTimeout(timeoutId!);
124+
expect(result).to.be.a('string');
125+
expect(result).to.equal('external-id');
126+
expect(result).to.not.equal('unknown');
127+
});
128+
129+
it('handles external promise rejection', async () => {
130+
let timeoutId: NodeJS.Timeout;
131+
const getMachineId = () =>
132+
new Promise<string>((resolve) => {
133+
timeoutId = setTimeout(() => resolve('delayed-id'), 10_000);
134+
});
135+
136+
const error = new Error('External rejection');
137+
138+
const { reject, value } = getDeviceId({
139+
getMachineId,
140+
isNodeMachineId: false,
141+
});
142+
143+
reject(error);
144+
145+
clearTimeout(timeoutId!);
146+
try {
147+
await value;
148+
expect.fail('Expected promise to be rejected');
149+
} catch (e) {
150+
expect(e).to.equal(error);
151+
}
152+
});
85153
});

0 commit comments

Comments
 (0)