Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Jasmine, array.includes does not work (must be replaced by other functions). Why?

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  ...
like image 522
fresko Avatar asked Oct 26 '16 12:10

fresko


People also ask

How do I check if an array is Jasmine?

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().

How do I check if a variable is defined in Jasmine?

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.

Which of the following matches function is used to test greater than condition?

The toBeGreaterThan and toBeLessThan matchers check if something is greater than or less than something else.


1 Answers

I would guess this is most likely due to one of two things:

  1. Your node version could be < 6.0.0, since Array.prototype.includes is not supported natively until [email protected], or

  2. The browser in your karma config file is set to "IE", since Internet Explorer does not support Array.prototype.includes.

like image 197
wesleysmyth Avatar answered Oct 20 '22 18:10

wesleysmyth