取消模拟模块时如何在 Jest 中模拟导入的命名函数 [英] How to mock imported named function in Jest when module is unmocked

查看:22
本文介绍了取消模拟模块时如何在 Jest 中模拟导入的命名函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Jest 中测试以下模块:

//myModule.js导出函数 otherFn() {console.log('做某事');}导出函数 testFn() {其他Fn();//做其他事情}

如上所示,它导出一些命名函数,重要的是testFn使用otherFn.

在 Jest 中,当我为 testFn 编写单元测试时,我想模拟 otherFn 函数,因为我不希望 otherFn 影响我对 testFn 的单元测试.我的问题是我不确定这样做的最佳方法:

//myModule.test.jsjest.unmock('myModule');从myModule"导入 { testFn, otherFn };描述('测试类别',()=> {it('测试一些关于 testFn', () => {//我想在这里模拟otherFn"但不能重新分配//又名不能做 otherFn = jest.fn()});});

感谢任何帮助/见解.

解决方案

jest.mock()

中使用 jest.requireActual()<块引用>

jest.requireActual(moduleName)

返回实际模块而不是模拟,绕过对模块是否应接收模拟实现的所有检查.

示例

我更喜欢这种简洁的用法,您需要并在返回的对象中传播:

//myModule.test.js从 './myModule.js' 导入 { otherFn }jest.mock('./myModule.js', () => ({...(jest.requireActual('./myModule.js')),otherFn: jest.fn()}))描述('测试类别',()=> {it('测试一些关于 otherFn', () => {otherFn.mockReturnValue('foo')期望(otherFn()).toBe('foo')})})

Jest 的 Manual Mocks 文档中也引用了此方法(在 末尾)示例):

<块引用>

为了确保手动模拟和它的实际实现保持同步,在手动模拟中使用 jest.requireActual(moduleName) 并用模拟函数修改它可能很有用在导出之前.

I have the following module I'm trying to test in Jest:

// myModule.js

export function otherFn() {
  console.log('do something');
}

export function testFn() {
  otherFn();

  // do other things
}

As shown above, it exports some named functions and importantly testFn uses otherFn.

In Jest when I'm writing my unit test for testFn, I want to mock the otherFn function because I don't want errors in otherFn to affect my unit test for testFn. My issue is that I'm not sure the best way to do that:

// myModule.test.js
jest.unmock('myModule');

import { testFn, otherFn } from 'myModule';

describe('test category', () => {
  it('tests something about testFn', () => {
    // I want to mock "otherFn" here but can't reassign
    // a.k.a. can't do otherFn = jest.fn()
  });
});

Any help/insight is appreciated.

解决方案

Use jest.requireActual() inside jest.mock()

jest.requireActual(moduleName)

Returns the actual module instead of a mock, bypassing all checks on whether the module should receive a mock implementation or not.

Example

I prefer this concise usage where you require and spread within the returned object:

// myModule.test.js

import { otherFn } from './myModule.js'

jest.mock('./myModule.js', () => ({
  ...(jest.requireActual('./myModule.js')),
  otherFn: jest.fn()
}))

describe('test category', () => {
  it('tests something about otherFn', () => {
    otherFn.mockReturnValue('foo')
    expect(otherFn()).toBe('foo')
  })
})

This method is also referenced in Jest's Manual Mocks documentation (near the end of Examples):

To ensure that a manual mock and its real implementation stay in sync, it might be useful to require the real module using jest.requireActual(moduleName) in your manual mock and amending it with mock functions before exporting it.

这篇关于取消模拟模块时如何在 Jest 中模拟导入的命名函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆