Skip to content

Commit 111f05f

Browse files
committed
Fix flaky test with fake clock
1 parent 761046d commit 111f05f

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

test/validation.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import untypedTest, { TestFn } from 'ava'
2+
import { installTimers } from './helper'
23
import { Adapter, AdapterEndpoint, CustomInputValidator, EndpointGenerics } from '../src/adapter'
34
import { BaseAdapterSettings } from '../src/config'
45
import { AdapterRequest, AdapterResponse } from '../src/util'
@@ -681,6 +682,12 @@ test.serial('Test host validator', async (t) => {
681682
})
682683

683684
test.serial('Test response timestamp validator', async (t) => {
685+
// Use a fake clock to avoid a race condition between getting the error and
686+
// the clock progressing to the next millisecond.
687+
const now = Date.now()
688+
const clock = installTimers()
689+
clock.setSystemTime(now)
690+
684691
const timestampValidator = validator.responseTimestamp()
685692
let value = new Date().getTime()
686693
let error = timestampValidator.fn(value)
@@ -695,6 +702,8 @@ test.serial('Test response timestamp validator', async (t) => {
695702
value = 0
696703
error = timestampValidator.fn(value)
697704
t.is(error, 'Minimum allowed value is 1514764861000. Received 0')
705+
706+
clock.uninstall()
698707
})
699708

700709
test.serial('Test base64 validator', async (t) => {

0 commit comments

Comments
 (0)