Search code examples
javascriptjasminechutzpah

Chutzpah or Jasmine does not let me run tests which use prototype


Chutzpah and Jasmine runs fine in my solution in Visual Studio, for my javascript tests.

I have an issue, and I think it's chutzpah but I'm not 100% certain (it could be Jasmine).

I now need to test my extension methods, which contains code similar to

if (!Array.prototype.where) {
    Array.prototype.where = function(callback) {
        const arr = [];
        this.forEach(function(item) {
            if (callback(item)) {
                arr.push(item);
            }
        });
        return arr;
    };
}

My Jasmine test is.

    //arrange
    const array = [];
    const fixedInput = "a";
    array.push(fixedInput);
    array.push("b");
    
    //act
    const result = array.where(a => a === fixedInput);
    //console.log(result);

    //assert
    const expected = [fixedInput];
    expect(result).toBe(expected);
    expect(result.length).toBe(1);        

I am unable to run this test, over command line or the Chutzpah plug in within Visual Studio (and it does not show as a viable test in VS)

If I comment out the const result = array.where(a => a === fixedInput); then the test is discovered.

If I execute the test, with that line commented out, the Jasmine web page of test results is shown - with it open, I can edit the test, uncomment the line and I get the result I would expect (but only for a few seconds until that result page turns into a "file not found").

The output window shows

Unknown error occurred when executing test file. Received exit code of 2

I don't know how to solve this. Any advice?


Solution

  • You are probably hitting an issue that Chutzpah by default uses an old JS engine. You should configure your chutzpah.json to use a newer one https://github.com/mmanela/chutzpah/wiki/Browser-Engines