I am working on a react webapp and have run into a really strange issue when trying to get my unit tests to pass.
Essentially I have a javascript class called Time
that's essentially just the time component of a DateTime (its based on a Luxon Duration). I have added isBefore(Time)
and isAfter(Time)
methods to this class to allow two times to be compared.
These methods are used in a helper function (checkTimeRange
) in another file to implement a three-way comparison (i.e. it can tell if one Time
is before, during, or after a range denoted by two other Time
s).
Here's the state of my unit tests:
Time
tests: passing, including forisAfter
.- helper tests: passing, including for
checkTimeRange
School
,ClassPeriod
, andBellSchedule
(all classes that contain methods that indirectly depend oncheckTimeRange
) tests for those methods are failing because<variable storing a Time>.isAfter is not a function
In researching, I found this, which seemed to describe the issue perfectly (in the title at least), but the solution seemed to be a simple typo, which doesn't appear to be the case here.
Things I have tried:
- checking out the project in another folder
- deleting and re-installing the node_modules
- clearing the yarn cache
- updating dependencies
- changing the functions from
public function name () {}
syntax to arrow function (public const name = () => {}
) syntax - checking for typos and variable shadowing
- running tests both via
yarn test
and using the VSCode integration thats showing up in my editor (idk whats causing this to show up though) - placing a unit test for
Time.isAfter
right alongside the case that is failing becauseisAfter is not a function
to show that its not a difference in how the different unit test files are set up - updating yarn from 1.17.3 to 1.22.19
I still have basically no idea as to what may be causing this. I have a feeling that maybe something deep inside Jest is not working properly, but I don't really know whats causing this contradicting behavior.
Here is the branch of the repo where im seeing this behavior if anyone needs more info on whats happening
The test that i'm looking at is the last one in the file of unit tests for the School
class. However many of the other classes, like ClassPeriod
also have their last test failing for similar reasons.
CodePudding user response:
tl;dr: <variable storing a Time>
is not storing a Time.
Here is the test error.
● School › can check if school is in session
TypeError: startTime.isAfter is not a function
112 |
113 | // swap the values if startTime is after end time
> 114 | if (startTime.isAfter(endTime)) {
| ^
115 | let t = startTime
116 | startTime = endTime
117 | endTime = t
at checkTimeRange (src/utils/helpers.tsx:114:19)
at School.isInSession (src/@types/school.ts:146:13)
at Object.<anonymous> (src/@types/school.test.ts:97:23)
at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)
at runJest (node_modules/@jest/core/build/runJest.js:404:19)
I added console.log(startTime);console.log(endTime);
to checkTimeRange
.
startTime
and endTime
are both Luxon DateTimes which do not have an isAfter
function. startTime.isAfter(endTime)
won't work.