In Jasmine, array.includes does not work (must be replaced by other functions). Why?
arrays, javascript, karma-jasmine, unit-testing
Solution
I would guess this is most likely due to one of two things:
Your node version could be < 6.0.0, since Array.prototype.includes is not supported natively until node@6.0.0, or
The browser in your karma config file is set to "IE", since Internet Explorer does not support Array.prototype.includes.
Problem
My problem seems very strange. I have a constructor with a new, very simple function, that should check if a variable is contained in an array. It works perfectly (i use this function in a form). But... i cannot write any Unit Test on this function, since Karma/Jasmine cannot see the function "includes" of the array. Could somebody suggest me what to do? Here the situation, a little bit simplified: //theConstructor to be tested ``` vm.isNameAlreadyUsed = function () { //debut logging: console.log ("vm.allNames ",vm.allNames); // output: vm.allNames ['A', 'B', 'C'] console.log ("and vm.nameToBeChecked is ",vm.nameToBeChecked); //output: and vm.nameToBeChecked is 'A' return vm.allNames.includes(vm.nameToBeChecked); // The previous works as expected at runtime, but it causes the following exception in karma/jasmine: // TypeError: undefined is not a constructor (evaluating 'vm.allNames.includes(vm.nameToBeChecked) }; ``` //Test (karma/jasmine) ``` theConstructor.allNames = ["A", "B", "C"]; theConstructor.nameToBeChecked = "A"; var result theConstructor.isNameAlreadyUsed(); //error! expect(result).toBeTruthy(); ``` Is it possible that jasmine cannot see "includes"? the array is filled, the variable also... and why should be any constructor there? ``` TypeError: undefined is not a constructor (evaluating 'vm.allNames.includes(vm.nameToBeChecked) ``` Thanks UPDATE I noticed that in jasmine, any call to "includes" causes an error. It doesn't depends where. Example it's enough to write the following code in a jasmine file to get an error mentioning a... constructor (?!?): ``` [1, 2, 3].includes(2); // TypeError: undefined is not a constructor (evaluating '[1, 2, 3].includes(2)') in ... ```