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 ...
Array Equality should check for array equility Message: Expected [ 1, 2, 3 ] to be [ 1, 2, 3 ]. Tip: To check for deep equality, use . toEqual() instead of . toBe().
currentVal = 0; describe("Different Methods of Expect Block",function () { it("Example of toBeDefined", function () { expect(currentVal). toBeDefined(); }); }); In the above code, toBeDefined() will check whether the variable currentVal is defined in the system or not.
The toBeGreaterThan and toBeLessThan matchers check if something is greater than or less than something else.
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 [email protected], or
The browser in your karma config file is set to "IE", since Internet Explorer does not support Array.prototype.includes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With