jeudi 4 août 2016

Mock exported function in module

I have two modules which contain exported functions. "ModuleB" uses a function from "ModuleA". Now I want to test "ModuleB" and mock the used function from "ModuleA".

I use ES6 with babel. For testing I use karma and jasmine.

I tried using babel-rewire and inject-loader, but it just does not work. I'm kind of new to all this and I guess I'm just doing something wrong. So any help is appreciated!

moduleA.js

export function getResult() {
    return realResult;
}

moduleB.js

import * as ModuleA from './moduleA'

export function functionToTest() {
    let result = ModuleA.getResult();
    // do stuff with result which I want to test
}

my test

it("does the right thing", () => {
    // I tried using rewire, but this gives me an exception:
    ModuleB.__Rewire__('ModuleA', {

        getResult: () => {
            return fakeResult;
        }
    });

    let xyz = ModuleB.functionToTest(canvas, element);
});

Aucun commentaire:

Enregistrer un commentaire