mercredi 4 décembre 2019

Why does Jest error with Maximum Call Stack Size Exceeded When I Import KnockoutJS Into A ViewModel I'm Testing?

I'm trying to write some tests using Jest for a KnockoutJs Project.

Apologies for any terminology I get wrong below I'm coming back to JS after about 10 years of not using it and still getting my head round things like ES6 modules.

The tests work fine until I need to test a ViewModel that uses knockout observable objects, I've added an import to my viewmodel to bring in KnockoutJs using ES6 module syntax and have babel setup to compile this so it should work in node.

My viewmodel looks like this...

export { myVm }
import * as ko from 'knockout'

function myVm() {
    var self = this;

    self.helloWorld = function () { return "Hello World" }

    return self;
}

Then my test file looks like...

import * as vm from '../src/viewModels/myVm'

test('Returns Hello World', () => {
    expect(vm.myVm().helloWorld()).toBe('Hello World');
});

When I execute Jest I get a Maximum call stack size exceeded error

Error Screenshot

If I remove the import * as ko line from my ViewModel it works fine but then I can't reference any of the object types in the knockout library.

Not sure if it's relevant but my .babelrc looks like this...

{
    "presets": [
        "env"
    ]
}

Any idea what I'm doing wrong when I'm importing Knockout into the ViewModel?

Aucun commentaire:

Enregistrer un commentaire