E
E
Egor Chebotarev2017-11-30 10:34:54
JavaScript
Egor Chebotarev, 2017-11-30 10:34:54

How do mock functions work in JEST?

Testing the getlang function . Inside it, get_language is used , so I write a mock function for it.
At the output I get *ReferenceError: get_language is not defined*

const getlang = require('./../j/lang');
const get_language = jest
    .fn()
    .mockReturnValue('ru');

console.log(get_language());

test('getlang return ERROR', () => {
    expect(getlang()).toBe('lang error');
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Egor Chebotarev, 2017-11-30
@Egche

The problem was solved by writing the mock function to the global scope of the node

beforeAll(() => {
  global.get_language =jest
    .fn()
    .mockReturnValue('ru');

});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question