tests: make requests deterministic 💚

This commit is contained in:
Jannis R 2020-05-21 22:21:07 +02:00 committed by Jannis Redmann
parent 0c3acaba46
commit fea27d3a4a
8 changed files with 18 additions and 6 deletions

View file

@ -159,7 +159,8 @@ test('earlier/later journeys', async (t) => {
fetchJourneys: client.journeys, fetchJourneys: client.journeys,
validate, validate,
fromId: luxembourg, fromId: luxembourg,
toId: ettelbruck toId: ettelbruck,
when,
}) })
t.end() t.end()

View file

@ -49,7 +49,7 @@ test('departures at Hagen Bauhaus', async (t) => {
test('trip details', async (t) => { test('trip details', async (t) => {
const deps = await client.departures(hagenBauhaus, { const deps = await client.departures(hagenBauhaus, {
results: 1, duration: 120, departure: when results: 1, duration: 120, when
}) })
const p = deps[0] || {} const p = deps[0] || {}

View file

@ -407,6 +407,7 @@ test('stop', async (t) => {
test('line with additionalName', async (t) => { test('line with additionalName', async (t) => {
const departures = await client.departures(potsdamHbf, { const departures = await client.departures(potsdamHbf, {
when,
duration: 12 * 60, // 12 minutes duration: 12 * 60, // 12 minutes
products: {bus: false, suburban: false, tram: false} products: {bus: false, suburban: false, tram: false}
}) })

View file

@ -136,7 +136,8 @@ test('earlier/later journeys', async (t) => {
fetchJourneys: client.journeys, fetchJourneys: client.journeys,
validate, validate,
fromId: ingolstadtHbf, fromId: ingolstadtHbf,
toId: telemannstr toId: telemannstr,
when,
}) })
t.end() t.end()

View file

@ -3,15 +3,17 @@
const isRoughlyEqual = require('is-roughly-equal') const isRoughlyEqual = require('is-roughly-equal')
const testJourneysWalkingSpeed = async (cfg) => { const testJourneysWalkingSpeed = async (cfg) => {
const {test: t, journeys, validate, from, to, products, minTimeDifference} = cfg const {test: t, journeys, validate, from, to, when, products, minTimeDifference} = cfg
const {journeys: [journeyWithFastWalking]} = await journeys(from, to, { const {journeys: [journeyWithFastWalking]} = await journeys(from, to, {
departure: when,
results: 1, products, walkingSpeed: 'fast' results: 1, products, walkingSpeed: 'fast'
}) })
const legWithFastWalking = journeyWithFastWalking.legs.find(l => l.walking) const legWithFastWalking = journeyWithFastWalking.legs.find(l => l.walking)
t.ok(legWithFastWalking, 'no walking leg in journey with fast walking') t.ok(legWithFastWalking, 'no walking leg in journey with fast walking')
const {journeys: [journeyWithSlowWalking]} = await journeys(from, to, { const {journeys: [journeyWithSlowWalking]} = await journeys(from, to, {
departure: when,
results: 1, products, walkingSpeed: 'slow' results: 1, products, walkingSpeed: 'slow'
}) })
const legWithSlowWalking = journeyWithSlowWalking.legs.find(l => l.walking) const legWithSlowWalking = journeyWithSlowWalking.legs.find(l => l.walking)

View file

@ -10,9 +10,14 @@ const hour = 60 * 60 * 1000
const day = 24 * hour const day = 24 * hour
const week = 7 * day const week = 7 * day
const T_MOCK = 1592225430 * 1000
// next Monday 10 am // next Monday 10 am
const createWhen = (timezone, locale) => { const createWhen = (timezone, locale) => {
return DateTime.fromMillis(Date.now(), { const t = process.env.VCR_MODE && !process.env.VCR_OFF
? T_MOCK
: Date.now()
return DateTime.fromMillis(t, {
zone: timezone, zone: timezone,
locale, locale,
}).startOf('week').plus({weeks: 1, hours: 10}).toJSDate() }).startOf('week').plus({weeks: 1, hours: 10}).toJSDate()

View file

@ -133,6 +133,7 @@ test('journeys: walkingSpeed', async (t) => {
validate, validate,
from: havelchaussee, from: havelchaussee,
to: wannsee, to: wannsee,
when,
products: {bus: false}, products: {bus: false},
minTimeDifference: 5 * 60 * 1000 minTimeDifference: 5 * 60 * 1000
}) })

View file

@ -78,7 +78,8 @@ test('earlier/later journeys', async (t) => {
fetchJourneys: client.journeys, fetchJourneys: client.journeys,
validate, validate,
fromId: ewaldstrasse, fromId: ewaldstrasse,
toId: kornmarkt toId: kornmarkt,
when
}) })
t.end() t.end()