I am learning to test with the create-react-app built-in testing tools. I have successfully tested some dumb components and fetch calls but when I added a simple function test something goes wrong. I have searched for answers and I cannot tell if it is a bug or if I am doing it wrong. This is the function I am testing.
export default async function (trips, users) {
let arry = []
trips.forEach(trip => {
if (trip.get('distance') !== '0.0' || trip.get('distance') !== 0.0) {
users.forEach(user => {
let hours = 0
if (trip.get('userId') === user.get('id')) {
if (trip.get('duration')) {
hours += toSeconds(trip.get('duration'))
hours = toHHMMSS(hours)
hours = roundTime(hours)
const obj = {
hours: parseInt(hours), // eslint-disable-line
fullName: user.get('fullName'),
id: user.get('id')
}
arry.push(obj)
}
}
})
}
})
return arry
}
Here is the test
test('user hours for summary', () => {
let tripsData = testData.TRIPS_DATA
let usersData = testData.USERS_DATA
tripsData = Immutable.fromJS(tripsData)
usersData = Immutable.fromJS(usersData)
return userHours(tripsData, usersData).then(res => {
expect(res).toEqual(testData.USER_HOURS_RESULT)
})
})
I am also using
"custom-react-scripts": "^0.2.1"
instead of the default react-scripts. I thought that could be the problem but when I switched back it was throwing the same error.
"test": "react-scripts test --coverage --env=jsdom"
Based on this and this, it seems that this could be caused by a bug in create-react-app. Try running the test without coverage mode to see if it works.
If it still doesn't work then the presence of a bug is much less likely. Maybe append a .catch()
to the promise that you return to see if there's an error in your function that's been getting by undetected one way or another.