Search code examples
javascriptunit-testingjasminearrow-functions

Does Jasmine support javascript with => (arrow)


Jasmine doesn't seem to support => (if my own findings are correct).

This is a snippet from my logic.js file

var myObj = new function(){
    this.getSomething = function(input){
        _myLocalArray.forEach(item =>{
            //left blank
        });
    };
};

My test is

/// <reference path="../../Ui/Content/Javascript/logic.js"/>
/// <reference path="jasmine.js" />

describe("My Tests", function () {

it("Description etc", function () {
    var result = myObj.getSomething("some input"); // FAILS HERE
    expect(result.length).toBe(0);
});
...

It fails with

JavaScript critical error at line 61, column 37 in file:///d:/github/Ui/Content/Javascript/logic.js\n\nSCRIPT1002: Syntax error

Line 61 is shown in th3e code snippet above with //FAILS HERE

and then an exception in Jasmine is thrown which is

myObj is undefined

If I comment out the code in logic (where I use =>) then the code executes without these errors

What am I doing wrong? Is => not supported? I find nothing in the docs to confirm this.

EDIT

I am not use nodeJs. I'm using vanilla javascript, visual studio 2017, Jasmine and Chutzpah.


Solution

  • This is quite important:

    I'm using vanilla javascript, visual studio 2017, Jasmine and Chutzpah.

    first, there is no vanilla javascript! Or at least, node does not mean you dont use vanilla javascript. The only meaning of vanilla javascript is that you don't use any framework/library. However you always need something to execute your javascript. Either a browser or something else like node. There are other options however, for example Nashorn, GraalVM or Duktape.

    Jasmine is a library written is Javascript. So Jasmine has nothing to do with the supported Javascript features. However Chutzpah has. So how does Chutzpah execute your javascript? Well, the readme answers this question:

    Chutzpah supports the QUnit, Jasmine and Mocha testing frameworks. Chutzpah uses the PhantomJS headless browser to run your tests.

    and here is your answer: Phantomjs does not support arrow functions!

    Actually phantomjs is dead and should be replaced by things like Chrome Headless. Also the activity on Chutzpah looks not very promising. So I can just recommend you to step away from Chutzpah for now, and use somthing that uses Chrome Headless under the hood.