Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock specific function in object using Jest?

I'm testing a React/Reflux application using Jest. I have the following function in a store:

onLoad: function() {
  console.log("ORIGINAL LOAD");
  // http request here
}

I'm trying to mock it out so that it just does what it needs to do without doing the actual network stuff:

beforeEach(function() {

  // mock out onLoad so instead of making API call the store gets test data
  PostStore.onLoad = jest.genMockFunction().mockImplementation(function () {
    var p1 = new Post(
      "54da7df5119025513400000a",                    // id
      "Test Post",                                   // title
      "Kji6ftLjUqhElgnqOBqMUKxYONpU7nK/cu6jTA==\n",  // owner anonId
      "Test Course 1",                               // course name
      "This is a test!",                             // content
      6,                                             // upvotes
      2,                                             // downvotes
      ["Kji6ftLjUqhElgnqOBqMUKxYONpU7nK/cu6jTA==\n"] // voter anonIds
    );

    this.posts = [p1];
    console.log("mocked function");
  });

  // component initialized here
});

However, it seems like the mocked function never even gets created. When I run the tests, the console still logs ORIGINAL LOAD.

What's the correct way to override the object's method so that instead of setting the posts array in PostStore by doing an ajax call it just sets it with test data?

like image 690
Hugo Avatar asked Feb 18 '15 21:02

Hugo


People also ask

How do you mock a function in an object Jest?

When mocking global object methods in Jest, the optimal way to do so is using the jest. spyOn() method. It takes the object and name of the method you want to mock, and returns a mock function. The resulting mock function can then be chained to a mocked implementation or a mocked return value.

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.

Can you mock an object in Jest?

The jest object is automatically in scope within every test file. The methods in the jest object help create mocks and let you control Jest's overall behavior. It can also be imported explicitly by via import {jest} from '@jest/globals' .

How do you mock a function of a class in Jest?

In order to mock a constructor function, the module factory must return a constructor function. In other words, the module factory must be a function that returns a function - a higher-order function (HOF). Since calls to jest. mock() are hoisted to the top of the file, Jest prevents access to out-of-scope variables.


2 Answers

I have found jest mock instance function It's here

eg:

import expect from 'expect';

jest.mock('../libs/utils/Master');
import Master from '../libs/utils/Master';

Master.mockImplementation(() => {
  return {
    returnOne: jest.fn().mockReturnValueOnce(1)
  }
})
describe('test Master', function() {
  it('test search', function() {
    let master = new Master();
    expect(master.returnOne()).toEqual(1);
  });
});
like image 119
Yi zhang Avatar answered Sep 19 '22 23:09

Yi zhang


Almost there, all you need to do is

const onLoad = jest.fn().mockImplementation(function () {
    var p1 = new Post();//Add your stuff here
    this.posts = [p1];
    console.log("mocked function");
  });
PostStore.onLoad = onLoad.bind(PostStore); //PostStore is your object.
like image 31
mdsAyubi Avatar answered Sep 16 '22 23:09

mdsAyubi