How to spy on a default exported function with Jest?

后端 未结 3 431
粉色の甜心
粉色の甜心 2020-12-29 03:36

Suppose I have a simple file exporting a default function:

// UniqueIdGenerator.js
const uniqueIdGenerator = () => Math.random().toString(36).substring(2,         


        
3条回答
  •  南方客
    南方客 (楼主)
    2020-12-29 04:27

    In some cases you have to mock the import to be able to spy the default export:

    import * as fetch from 'node-fetch'
    
    jest.mock('node-fetch', () => ({
      default: jest.fn(),
    }))
    
    jest.spyOn(fetch, 'default')
    

提交回复
热议问题