Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to mock variables inside a function in jest

I want to test the 'canViewPage method in jest. How do mock the const userPages which is the values from the func getUserPage

   canViewPage(page){
     const userPages = getUsersPages();
    if(userPages.includes(page)){
      return true;
    }
    return false;
   }


  getUsersPages(){
    // here i hardcode a list of pages, for simplicity purposes
    const pages = ['home','about','contact'];
    return pages

  }



here is what i tried

test('test canViewPage', () => {
    const spy = jest.spyOn(canViewPage, 'userPages');
    spy.mockReturnValue(['home','about','contact']);

    expect(canViewPage('premiumPage')).toBe(false); 

    spy.mockRestore();
  });

I also tried this

test('test canViewPage', () => {
    const spy = jest.spyOn(canViewPage, 'getUsersPage');
    spy.mockReturnValue(['home','about','contact']);

    expect(canViewPage('premiumPage')).toBe(false); 

    spy.mockRestore();
  });
like image 746
deko Avatar asked Jun 21 '19 15:06

deko


People also ask

How do you mock a class variable in Jest?

You are right to mock getUsersPage() , but you setting up the mock/spy incorrectly. getUsersPages() is not defined/exported from within canViewPage() . Instead try targeting getUsersPage() directly to create a mock/spy. With the spy working, then you can assert canViewPage('premiumPage') accordingly.

How do you mock a value in Jest?

You can use jest. mock (line 4) to mock the lang dependency. In the example above, the mock module has a current field which is set to a mock function. You want to test both branches of hello, so you use mockReturnValueOnce to make the mock function return "GL" in the first invocation, and "EN" in the second one.

How do you mock a custom function in Jest?

The simplest and most common way of creating a mock is jest. fn() method. If no implementation is provided, it will return the undefined value. There is plenty of helpful methods on returned Jest mock to control its input, output and implementation.


1 Answers

To mock the value of userPages you have to mock getUserPage. To achieve this, it depends on where and how getUserPage and canViewPage are defined. I'm going to assuming these two functions are defined in class MyAwesomeClass

// MyAwesomeClass.js
export default class MyAwesomeClass {
  canViewPage(page) {
    const userPages = this.getUsersPages();
    if (userPages.includes(page)) {
      return true;
    }
    return false;
  }


  getUsersPages() {
    // here i hardcode a list of pages, for simplicity purposes
    const pages = ['home', 'about', 'contact'];
    return pages;
  }
}

// test.js
import MyAwesomeClass from '<path to MyAwesomeClass.js>'
test('test canViewPage', () => {
  const instance = new MyAwesomeClass()
  const spy = jest.spyOn(instance, 'getUsersPages');
  spy.mockReturnValue(['mockItem1','mockItem2','mockItem3']);

  expect(instance.canViewPage('premiumPage')).toBe(false);
  expect(instance.canViewPage('mockItem1')).toBe(true);

  spy.mockRestore();
});

if canViewPage and getUsersPages are static methods within the class, you mock differently

test('test canViewPage', () => {
  const spy = jest.spyOn(MyAwesomeClass, 'getUsersPages');
  spy.mockReturnValue(['mockItem1','mockItem2','mockItem3']);

  expect(MyAwesomeClass.canViewPage('premiumPage')).toBe(false);
  expect(MyAwesomeClass.canViewPage('mockItem3')).toBe(true);

  spy.mockRestore();
});
like image 113
macphilips Avatar answered Oct 23 '22 03:10

macphilips