title: How To Reset Manual Mocks In Jest
body: <p>I have a manual mock of <code>crypto</code> that looks like this:</p>
  
  <pre><code>// __mocks__/crypto.js
  
  const crypto = jest.genMockFromModule('crypto')
  const toString: Function = jest.fn(() =&gt; {
    return {}.toString()
  })
  const mockStringable = {toString}
  const update: Function = jest.fn(() =&gt; mockStringable)
  const deciper = {update}
  crypto.createDecipheriv = jest.fn(() =&gt; deciper)
  
  export default crypto
  </code></pre>
  
  <p>Which is basically tested like this:</p>
  
  <pre><code>const crypto = require('crypto')
  jest.mock('crypto')
  
  describe('cookie-parser', () =&gt; {
    afterEach(() =&gt; {
      jest.resetAllMocks()
    })
    describe('decryptCookieValue', () =&gt; {
      it('should call the crypto library correctly', () =&gt; {
        const result = decryptCookieValue('test-encryption-key', 'test-encrypted-value')
        expect(crypto.pbkdf2Sync).toHaveBeenCalledTimes(2)
        expect(crypto.createDecipheriv).toHaveBeenCalled()
        // more tests, etc, etc, etc
        expect(crypto.createDecipheriv('', '', '').update).toHaveBeenCalled()
        expect(result).toEqual({}.toString())
      })
    })
  
    ...
  </code></pre>
  
  <p>This works however if in that same test file, I test another method that invokes <code>decryptCookieValue</code> from within <code>crypto.createDecipheriv</code> no longer returns my mock decipher. Instead it returns undefined. For instance:</p>
  
  <pre><code>describe('cookie-parser', () =&gt; {
    afterEach(() =&gt; {
      jest.resetAllMocks()
    })
    describe('decryptCookieValue', () =&gt; {
      it('should call the crypto library correctly', () =&gt; {
        const result = decryptCookieValue('test-encryption-key', 'test-encrypted-value')
        expect(crypto.pbkdf2Sync).toHaveBeenCalledTimes(2)
        expect(crypto.createDecipheriv).toHaveBeenCalled()
        expect(crypto.createDecipheriv('', '', '').update).toHaveBeenCalled()
        expect(result).toEqual({}.toString())
      })
    })
    ...
    ...
    describe('parseAuthenticationCookie', () =&gt; {
      it('should create the correct object', () =&gt; {
  
        // parseAuthenticationCookie calls decryptCookieValue internally
  
        const result = parseAuthenticationCookie('', '') // Fails because internal call to crypto.createDecipheriv stops returning mock decipher.
        expect(result).toEqual({accessToken: null})
      })
    })
  })
  </code></pre>
  
  <p>I think this is an issue with resetting the manual mock because if I take that later test and move it into a file all by itself with the same surrounding test harness it works just fine.</p>
  
  <pre><code>// new test file
  import crypto from 'crypto'
  import { parseAuthenticationCookie } from './index'
  
  jest.mock('crypto')
  
  describe('cookie-parser', () =&gt; {
    afterEach(() =&gt; {
      jest.resetAllMocks()
    })
    describe('parseAuthenticationCookie', () =&gt; {
      it('should create the correct object', () =&gt; {
  
        // Works just fine now
  
        const result = parseAuthenticationCookie('', '')
        expect(result).toEqual({accessToken: null})
      })
    })
  })
  </code></pre>
  
  <p>Is my assessment here correct and, if so, how do I reset the state of the manual mock after each test?</p>