I have been trying to mock a function which is imported in another file and used in a class. There are similar questions here and I went through a lot of them but still failed to get my tests working. Here is the structure of my code:
//util.js
export const stageHelper = (key) => {
return STAGE_NAMES[key];
};
//main.js
import {stageHelper} from './util'
class Main {
static config = {
value: stageHelper('abc'),
}
static getValue() {
return this.config.value;
}
}
//main.spec.js
import * as util from './util';
import {Configuration} from '../configuration/configuration';
jest.mock('./util');
describe('Main', () => {
const utilSpy = jest.spyOn(util, 'stageHelper').mockImplementation(() => 'testValue');
//util.stageHelper = jest.fn().mockImplementation(() => 'testValue'); // tried this too
//utilSpy.mockReturnValue('ja'); // tried this too
expect(Main.getValue()).toEqual('testValue'); // this test fails - the value is 'undefined'
});
I get undefined
when calling Main.getValue()
from my test. However, I expect this to return testValue
since this is what I have mocked the return value as.
Could someone please help me get this right? This would be much appreciated!
Aucun commentaire:
Enregistrer un commentaire