来自Jest中的模块的模拟单功能并在其上断言

发布于 2025-02-08 18:33:58 字数 813 浏览 2 评论 0 原文

我试图用Jest的模块中的单个功能模拟一个单个功能,并断言我已经使用某些参数调用了它。我有一个大致看起来像这样的文件:

export const f = () => {
  ...
}
export const g = () => {
  ...
  f(...)
}
export const h = () => {
  ...
  g(...)
}

我正在尝试测试功能 g h ,而我要编写的断言是 f 在调用 g h 时,请使用某些参数调用。因此,在我的测试中,我想模拟 f ,并能够断言它的所谓。但是,当我在测试中进行类似的事情时:

import f, g, h from 'module'

jest.mock('module', () => {
  const original = jest.requireActual('module')
  
  return {
    ...original,
    f: jest.fn()
  }
})
test('g calls f correctly', () => {
  g()
  // I want to assert that f was called with some parameters
})

我没有提到 f ,并且似乎当 g 在测试中调用时,实际函数 f 被称为而不是模拟。我需要在这里进行什么才能实现此功能?

I'm trying to mock out a single function in a module with Jest and assert that I have called it with certain parameters. I have a file that roughly looks like so:

export const f = () => {
  ...
}
export const g = () => {
  ...
  f(...)
}
export const h = () => {
  ...
  g(...)
}

I'm trying to test out functions g and h, and one the assertions I'm trying to write is that f gets called with certain parameters when calling g and h. So in my tests, I want to mock out f and be able to assert what it was called with. However, when I do something like this in my tests:

import f, g, h from 'module'

jest.mock('module', () => {
  const original = jest.requireActual('module')
  
  return {
    ...original,
    f: jest.fn()
  }
})
test('g calls f correctly', () => {
  g()
  // I want to assert that f was called with some parameters
})

I have no reference to f, and it seems like when g gets called in the test, the actual function f is being called rather than a mock. What do I need to change here to get this working?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

强辩 2025-02-15 18:33:58

看起来您走在正确的道路上。我建议您尝试以下示例之一:

import { f, g } from './module'

jest.mock('./module', () => {
  const actual = jest.requireActual()

  return { ...actual, f: jest.fn() }
})

test('g calls f correctly', () => {
  g()
  expect(f).toHaveBeenCalled()
})

如果您遇到错误,说 f 应该是模拟功能,则可以尝试以下操作:

import { g } from './module'

// Create a reference of the mocked function to use later in the test.
// The name must start with "mock". It's a Jest rule.
const mockedF = jest.fn()

jest.mock('./module', () => {
  const actual = jest.requireActual()

  return { ...actual, f: mockedF }
})

test('g calls f correctly', () => {
  g()
  expect(mockedF).toHaveBeenCalled()
})

It looks like you're on the right path. I'll recommend you try one of the following examples:

import { f, g } from './module'

jest.mock('./module', () => {
  const actual = jest.requireActual()

  return { ...actual, f: jest.fn() }
})

test('g calls f correctly', () => {
  g()
  expect(f).toHaveBeenCalled()
})

If you get an error saying that f should be a mock function, you could try this:

import { g } from './module'

// Create a reference of the mocked function to use later in the test.
// The name must start with "mock". It's a Jest rule.
const mockedF = jest.fn()

jest.mock('./module', () => {
  const actual = jest.requireActual()

  return { ...actual, f: mockedF }
})

test('g calls f correctly', () => {
  g()
  expect(mockedF).toHaveBeenCalled()
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文