How to test a function that works with time

23 · Christian · May 6, 2021, 10:22 a.m.
While building Minimal Analytics I had the need to test a function that was dependend on time. In my case, the function returned the milliseconds until midnight. Here is a simple way to test the function msUntilMidnight. The test case The simplest test I could come up with, looks as follows: const test = require('ava')const { msUntilMidnight } = require('./backup')const HOURS = 60 * 60 * 1000test('returns ms until midnight', t => { const relativeNow = +new Date(new Date().toISOString().substrin...