vendredi 21 décembre 2018

Jest unit test 'Function' returns SyntaxError: Unexpected identifier at Function (

We have a stencil app that calls a wraps leaflet, the style function needs to take a Function as a method parameter, stencil can't do this, so we pass an object and then parse it to a function, anyway this works but trying to test this with Jest is giving us problems.

code

public onObjChange(newObj) {
    this.parsedFunction = Function('return ('+newObj+')')(); // ¯\_(ツ)_/¯ better than eval :)
}

test

it('Should accept a function parameter', () => {
    cut.onObjChange({test:'object'});
    expect(cut.geoObj).toEqual({test:'object'});
})

returns

SyntaxError: Unexpected identifier
    at Function (<anonymous>)

How can I resolve this?

Aucun commentaire:

Enregistrer un commentaire