how to unmock a single instance method with jest

20,121

Solution 1

mockFn.mockRestore() worked for me with [email protected]:

// Create a spy with a mock
const consoleInfoSpy = jest.spyOn(console, 'info').mockImplementation(() => {})

// Run test or whatever code which uses console.info
console.info('This bypasses the real console.info')

// Restore original console.info
consoleInfoSpy.mockRestore()

Solution 2

This does not strictly apply to the OP, but answer-seekers may end up here. You can mock a module except certain parts for all tests like so.

mocks/saladMaker.js

// Let Jest create the mock.
const saladMaker = jest.genMockFromModule('../saladMaker');

// Get the unmocked chop method.
const {chop} = require.requireActual('../saladMaker');

// Patch it in.
saladMaker.chop = chop;

module.exports = saladMaker;

The key part is to use requireActual to get at the unmocked module.

bypassing module mocks

Update

require.requireActual has been replaced with jest.requireActual.

Solution 3

It is not possible to get the original module after mocking it in Jest. What jest.mock does is to replace the module with your mock.

So even you write:

Foo = require('foo')
jest.mock('foo')

Jest will hoist the jest.mock('foo') call on top of the call stack, so it's the first thing that happens when the test starts. This will also affect all other modules you import and that import foo.js.

You could try to use spyOn to spy on functions of an object, should work with classes as well, but I'm not quite sure.

Share:
20,121

Related videos on Youtube

brewster
Author by

brewster

Updated on February 05, 2022

Comments

  • brewster
    brewster about 2 years

    coming from rspec, i am having trouble understanding mocking with jest. the approach i am trying for, is to automock a class's constructor and all of it's functions, and then unmock them one by one to test only that one function. the only documentation i can find on it, is with using 2 classes, mocking 1 class, and then testing that those functions are called from the other unmocked class.

    below is a basic, contrived idea of what i am trying to do. can someone direct me to the jest-way of doing this?

    foo.js

    class Foo
      constructor: ->
        this.bar()
        this.baz()
      bar: ->
        return 'bar'
      baz: ->
        return 'baz'
    

    foo_test.js

    // require the class
    Foo = require('foo')
    
    // mock entire Foo class methods
    jest.mock('foo')
    
    // unmock just the bar method
    jest.unmock(Foo::bar)
    
    // or by
    Foo::bar.mockRestore()
    
    // and should now be able to call
    foo = new Foo
    foo.bar() // 'bar'
    foo.baz() // undefined (still mocked)
    
    // i even tried unmocking the instance
    foo = new Foo
    jest.unmock(foo.bar)
    foo.bar.mockRestore()
    
  • brewster
    brewster over 5 years
    yea i have been playing with that too, but having the same issue. i was also looking at try to just mock the constructor, but can't seem to figure that out either. somehow jest is doing it, but looking through their source code may take longer than just figuring it out myself
  • sankalp
    sankalp almost 3 years
    jest.genMockFromModule() is deprecated so we can use jest.createMockFromModule()