Skip to content

Commit ebe95cb

Browse files
test: Fix a flaky test
In the modified test file, sleep() is backed by setTimeout(), and some Node runtimes might execute setTimeout callback before the specified delay, meaning that tmi.getValue() will occasionally return `9`. This produces a flaky test. An extra ms leeway in the assertion works round the issue. nodejs/node#10154 Test name updated to reflect that the timer's accuracy does not have single-digit precision
1 parent 01e4032 commit ebe95cb

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

test/metrics.spec.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,15 @@ describe('TimerMetricInstance', () => {
4949
expect(tmi.getValue()).toBeUndefined;
5050
});
5151

52-
it('can time things', async () => {
52+
it('can time things with sufficient accuracy', async () => {
5353
const tmi = new TimerMetricInstance('timer/network_time');
5454
tmi.start();
5555
await sleep(10);
5656
tmi.stop();
57-
expect(tmi.getValue()).toBeGreaterThan(9);
57+
// Sleep() is backed by setTimeout(), and some Node runtimes might execute setTimeout callback before the specified
58+
// delay, meaning that tmi.getValue() will occasionally return `9` at this point. An extra ms leeway in this
59+
// assertion works round the issue. https://github.com/nodejs/node/issues/10154
60+
expect(tmi.getValue()).toBeGreaterThanOrEqual(9);
5861
});
5962

6063
it('.start() / .stop() logs start/top and improper use warnings if you try to start/stop it again after stopping it', async () => {

0 commit comments

Comments
 (0)