I created this function below getIntervalsBetweenDates with the intention of using for testing certain timers and testing code.
export function getIntervalsBetweenDates (dates) {
dates.sort()
let intervals = []
dates.forEach((date, index) => {
let nextDate = dates[index + 1]
if (nextDate) intervals.push(Math.abs(date - nextDate))
})
return intervals
}
Itself has a test below. My intention here is to track the overall time it takes to run this inner code, and track the intervals between the code as well. Then diff the dates and get a rough estimate.
import Promise from 'bluebird'
import test from 'ava'
import {getIntervalsBetweenDates} from './index'
test('getIntervalsBetweenDates', async (t) => {
let total = []
let intervals = []
total.push(new Date())
intervals.push(new Date())
await Promise.delay(200)
intervals.push(new Date())
await Promise.delay(200)
intervals.push(new Date())
await Promise.delay(200)
total.push(new Date())
let intervalValues = getIntervalsBetweenDates(intervals)
let totalValue = getIntervalsBetweenDates(total)[0]
intervalValues.forEach(intervalValue => {
t.is(intervalValue >= 200, true)
t.is(intervalValue < 350, true)
})
t.is(totalValue > 600, true)
t.is(totalValue < 800, true)
})
The problem has been that when I keep testing this code above it seems that I have to keep tweaking this test, extending the time more and more. Is there a solid way to test this getIntervalsBetweenDates that is a more secure? Could ava be slowing down the more tests I add, unrelated to this code above?
Aucun commentaire:
Enregistrer un commentaire