dimanche 27 décembre 2020

Sinon - Mocking anonymous function that's called on import

Is there a way to stub a anonymous function that's called on import.

// a.js

function init() {
    // code here
}

init(); // this function is called

export function anything() {}

and another file that imports this file

// b.js
import anything from "a.js";

The problem I'm having is that I need to stub the init function in a test file that tests b.js. I can't export it as well and then instantiate it in the b.js.

Is there a way to mock the init function? I've tried using some suites that would help with mocking but the problem is that it seems you have to import the file before stubbing the function.

proxyquire (using es6 imports) and rewiremock won't work.

Aucun commentaire:

Enregistrer un commentaire