Mocking time in node and still using await/async



Sometimes you need to run an end-to-end test that depends on the native clock. For example a job service might dequeue a job to be processed every 5 seconds. We want to be able to test some behavior which depends on the job service but we don’t want to have to wait 5 seconds. In node, and other javascript environments, we can do this by mocking the internal clock using a library called lolex. Lolex supports automatically incrementing time which makes it easy to use the async/await functions.

// replaces the native timers with the fake one.
// the option `shouldAdvanceTime` allows us to use the await
const clock = lolex.install({ shouldAdvanceTime: true, advanceTimeDelta: 1 });

// start an export job
const res = await request(app.listen())
    .set("authorization", `Bearer ${token}`)

// move the clock forward by 6 seconds

// verify the job is complete.