-1

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?

MyDaftQuestions
  • 4,487
  • 17
  • 63
  • 120
  • Did you actually make an extension method that's basically `Array.filter` with a different name, or is that purely an example? – JLRishe Feb 21 '21 at 11:38
  • @JLRishe - it turns out... yes :) ... I did not know about `find`. However, I also have this for `HTMLCollection` and `NodeList` and it feels more familiar to `C#` ... Although I guess even if there is a better way (by using `find`), the issue described still exists – MyDaftQuestions Feb 21 '21 at 11:41
  • True, it is a valid question, but I would say it's very unwise to go around modifying prototypes to add stuff that you've invented. And I edited my comment about a minute after I made it, but what you have there is actually like Array.filter, which has been around since 2009! For things that aren't arrays, you can do `Array.from(nodeList).filter(someCondition)` – JLRishe Feb 21 '21 at 11:52

1 Answers1

1

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

Matthew Manela
  • 16,572
  • 3
  • 64
  • 66
  • Hi - I know who you are and follow your posts/blogs etc. Just a quick aside - thank you for everything you have done for the community. Too many compliments to give in a comment so I'll leave it with, thank you! – MyDaftQuestions Feb 22 '21 at 20:01
  • Can you confirm, that the JSON in the file you link to goes into the chutzpah json file at the root of the project? – MyDaftQuestions Feb 22 '21 at 20:02
  • Yes at top level in chutzpah.json add "Engine" and "EngineOptions" (optionally) – Matthew Manela Feb 22 '21 at 20:12