简体   繁体   English

如何从 Jest 手动模拟中检查默认导出 function

[英]How to inspect default export function from Jest manual mock

The following manual mock file of isomorphic-fetch for Jest tests returns the expected values:以下用于 Jest 测试的isomorphic-fetch手动模拟文件返回预期值:

// __mocks__/isomorphic-fetch.ts

import * as req from "./requestData";

const Fetch = (url: string, options: any): Promise<any> => {
  const resultPromise = {
    text: () => {
      return Promise.resolve(req.generateMockResponseData(url, options.body));
    },
  };
  return Promise.resolve(resultPromise);
};

export default Fetch;

However I've ran into problems when trying to inspect the arguments with which it was called, as the mock property on fetch was undefined.但是,我在尝试检查调用它的 arguments 时遇到了问题,因为fetch上的mock属性未定义。 I've tried two things:我尝试了两件事:

First, I've wrapped the Fetch function in jest.fn like this:首先,我将Fetch function 包装在jest.fn中,如下所示:

const Fetch = jest.fn((url: string, options: any): Promise<any> => {
// implementation is same as above
});

export default Fetch;

Now after adding现在添加后

import _fetchMock  from "isomorphic-fetch"
const fetchMock = _fetchMock as jest.Mock<Promise<any>>;

in the test file, I can inspect calls to fetch with在测试文件中,我可以检查调用来获取

fetchMock.mock.calls[0]

But now fetch in the application code returns undefined during tests for some reason.但是现在由于某种原因,应用程序代码中的fetch在测试期间返回未定义。

The second thing I've tried was removing the jest.fn wrapper from Fetch and adding我尝试的第二件事是从Fetch中删除jest.fn包装器并添加

jest.mock("isomorphic-fetch")

to the test file.到测试文件。 Now fetch returns the expected values in the application code during tests, but fetchMock.mock is now undefined again.现在fetch在测试期间返回应用程序代码中的预期值,但fetchMock.mock现在再次未定义。

Jest config:笑话配置:

// jest.config.js
module.exports = {
  "clearMocks": true,
  "coverageDirectory": "../coverage",
  "resetMocks": true,
  "restoreMocks": true,
  "rootDir": "./src",
  "testEnvironment": "jsdom",
  "preset": "ts-jest",
  "coveragePathIgnorePatterns": ["Error.ts"],
  "testEnvironmentOptions": {
    "resources": "usable",
    "features": {
      "FetchExternalResources": ["script", "iframe"],
      "ProcessExternalResources": ["script", "iframe"],
    }
  }
}

There is a complete example on GitHub. GitHub上有一个完整的例子。

The reason why Jest spy and regular mocked function may behave differently with the same implementation is that Jest spy can be reset to be a no-op. Jest spy 和常规模拟 function 在相同的实现中可能表现不同的原因是 Jest spy 可以重置为无操作。

This exactly what happens in this Jest setup:这正是此 Jest 设置中发生的情况:

...
"resetMocks": true,
"restoreMocks": true,
...

restoreMocks option is generally desirable, it prevents test cross-contamination by resetting spied methods to original implementations. restoreMocks选项通常是可取的,它通过将间谍方法重置为原始实现来防止测试交叉污染。

resetMocks is generally undesirable and virtually unusable, it resets reusable spies from __mocks__ to no-op implementation. resetMocks通常是不可取的并且几乎无法使用,它将可重用的间谍从__mocks__重置为无操作实现。

TL;DR: resetMocks configuration option and jest.resetAllMocks() destroy the implementations of reusable spies and should be generally avoided. TL;DR: resetMocks配置选项和jest.resetAllMocks()破坏了可重用间谍的实现,通常应该避免。 If a specific spy needs to be reset, this can be done on a per-case basis with mockReset method.如果需要重置特定的间谍,可以使用mockReset方法在每个案例的基础上完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM