Jest having trouble mocking specific function

Alex Penningss

I've started using jest but I've been having some trouble mocking specific JS functions, I've already tried various would be solutions to this but i just couldn't get it to work.

Below is my JavaScript function code

functions.js :

function test1(){

return "hello1"
}

function test2(){

return "hello2"
}
module.exports = test1;
module.exports = test2;

and this is my jest code:

function.test.js

const testfunction = require('/function');

test('tooltip Correct DOM element test', () => {
    expect(testfunction.test1).toBe("hello1");
    });

Ankita Kuchhadiya

You are exporting the function incorrectly. You need to do:

function test1() {
  return "hello1"
}

function test2() {
  return "hello2"
}
module.exports = {
  test1: test1,
  test2: test2
};

Exporting the function in this way will enable to get testfunction.test1 inside the test file. And you need to call the function inside the test script like:

expect(testfunction.test1()).toBe("hello1");

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related