【问题标题】:Using Jest to mock named imports使用 Jest 模拟命名导入
【发布时间】:2018-03-27 10:20:54
【问题描述】:

我有一个“notifications.js”模块,看起来有点像这样:

import { Notifications, Permissions } from 'expo'

export function setLocalNotification(storage = AsyncStorage) {
  return storage
    .getItem(NOTIFICATION_KEY)
    .then(JSON.parse)
    .then(data => {
      if (data === null) {
        return Permissions.askAsync(
          Permissions.NOTIFICATIONS
        ).then(({ status }) => {
          if (status === 'granted') {
            Notifications.cancelAllScheduledNotificationsAsync()
            ...etc.

在我的测试中,我想模拟 Permissions 和 Notifications 以便我可以在 notification.spec.js 中做这样的事情:

import { setLocalNotification } from './notifications'
import mockAsyncStorage from '../mock/AsyncStorage'

it('correctly cancels pending notifications', done => {
  setLocalNotification(mockAsyncStorage).then(done())
  expect(Permissions.askAsync).toBeCalled()
  expect(Notifications.cancelAllScheduledNotificationsAsync)
    .toBeCalled()
})

我使用jest.mockjest.setMock 尝试了各种方法,但我似乎无法正常工作。如何以所需的方式模拟这些命名导入?例如,我试过这个:

jest.setMock('Permissions', () => ({
  askAsync: jest
    .fn()
    .mockImplementationOnce(() => ({ status: 'granted' }))
}))

但这不起作用。它抛出

'module Permissions cannot be found from notifications.spec.js'

如果我尝试模拟整个 expo 模块,模拟函数 expect().toBeCalled() 返回 false。

【问题讨论】:

    标签: react-native jestjs expo es6-modules


    【解决方案1】:

    你必须模拟模块'expo'

    jest.mock('expo', ()=>({
      Permissions: {
         askAsync: jest.fn()
      }
    }))
    

    【讨论】:

    • 我最初尝试过这个,但我认为它不起作用,因为我的测试失败了。但事实证明他们失败了,因为我的实现有一个错误。所以这确实是正确的做法。
    • 这是对我有用的答案,对于我过去多次搜索但没有成功的问题
    猜你喜欢
    • 1970-01-01
    • 2018-12-14
    • 2022-07-09
    • 2017-02-06
    • 2019-12-17
    • 2021-01-21
    • 2018-06-30
    相关资源
    最近更新 更多