1

I'm currently trying to test some of my code using jest but for some reason if I send in 4 arguments in the function, it basically shifts the values forward two and the second two are undefined.

This was my test code

const userDistance = require("./avtest");

test("Checks userDistance calculation", () => {
   expect(userDistance(1, 1, 4, 5)).toBe(5);
});

Here is the function being tested

function userDistance(x, y, ux, uy) {
    let tx = (ux - x) * (ux - x);
    let ty = (uy - y) * (uy - y);
    return Math.sqrt(ty + tx);
}
module.exports = userDistance;

However, if I do a console.log() for each argument(eg x, y, ux, uy), x prints out as 4, y prints out as 5, and ux and uy are undefined. If I add two more arguments to the function then it works fine (the two added args are also undefined however). I am incredibly confused so any help would be appreciated

  • What does `expect(userDistance.length).toBe(4)` yield? – Sebastian Simon Apr 25 '21 at 18:36
  • userDistance.length is 6 and I have no idea why – cheese potations Apr 25 '21 at 18:40
  • I’ve never seen such behavior. Are you sure you’re importing the correct file, and you’ve pressed the Save button in the editor, etc.? – Sebastian Simon Apr 25 '21 at 18:48
  • It's importing the file avtest.js and I'm rerunning jest and jest --watch to make sure it tests the most recent files. I'm on intellij so there isn't really a manual save but this error still happens when it's running on the most recent version of the files. – cheese potations Apr 25 '21 at 18:52
  • 1
    Please, provide https://stackoverflow.com/help/mcve that can reproduce the problem. It's impossible for the code you posted to behave like that. `userDistance.length === 6` means that it's some other function, like different file version or wrong module. `console.log(userDistance.toString())` can give an idea what you're looking for. Try to run tests without IDE to reduce moving parts. Also check https://stackoverflow.com/questions/44866994/how-can-i-clear-the-jest-cache – Estus Flask Apr 25 '21 at 19:42

0 Answers0