Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(cn): translate jest/docs/BypassingModuleMocks.md #17

Open
wants to merge 1 commit into
base: cn
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions docs/BypassingModuleMocks.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
---
id: bypassing-module-mocks
title: Bypassing module mocks
title: 绕过模块模拟
---

Jest allows you to mock out whole modules in your tests, which can be useful for testing if your code is calling functions from that module correctly. However, sometimes you may want to use parts of a mocked module in your _test file_, in which case you want to access the original implementation, rather than a mocked version.
Jest允许你在测试中模拟出整个模块,如果你的代码正确地从该模块调用函数,这对于测试非常有用。但是,有时你可能希望在 _测试文件_ 中使用部分模拟模块,在这种情况下,你希望访问代码的原始实现,而非其模拟版本。

Consider writing a test case for this `createUser` function:
考虑为如下的 `createUser` 函数编写测试用例:

```javascript
// createUser.js
Expand All @@ -18,7 +18,7 @@ export const createUser = async () => {
};
```

Your test will want to mock the `fetch` function so that we can be sure that it gets called without actually making the network request. However, you'll also need to mock the return value of `fetch` with a `Response` (wrapped in a `Promise`), as our function uses it to grab the created user's ID. So you might initially try writing a test like this:
在你所编写的测试中,你将会模拟 `fetch` 函数,这样即可确保它在没有实际发出网络请求的情况下被调用。但是,你还需要模拟`fetch`的返回值,其包含`Response`(包装在`Promise`中),因为后面的函数使用它来获取所创建的用户的ID。因此,你可以先尝试编写这样的测试:

```javascript
jest.mock('node-fetch');
Expand All @@ -41,8 +41,9 @@ test('createUser calls fetch with the right args and returns the user id', async
```

However, if you ran that test you would find that the `createUser` function would fail, throwing the error: `TypeError: response.text is not a function`. This is because the `Response` class you've imported from `node-fetch` has been mocked (due to the `jest.mock` call at the top of the test file) so it no longer behaves the way it should.
然而,如果运行如上测试,你会发现`createUser`函数将失败,并抛出错误信息`TypeError: response.text is not a function`。这是因为从`node-fetch`导入的`Response`类已被模拟(由于`jest.mock`在测试文件的顶部调用),这样它就不再按照它规定的方式运行了。

To get around problems like this, Jest provides the `jest.requireActual` helper. To make the above test work, make the following change to the imports in the test file:
为了解决这样的问题,Jest提供了一个`jest.requireActual`辅助工具。要使上述测试正常工作,需对测试文件中的导入进行以下更改:

```javascript
// BEFORE
Expand All @@ -57,4 +58,4 @@ import fetch from 'node-fetch';
const {Response} = jest.requireActual('node-fetch');
```

This allows your test file to import the actual `Response` object from `node-fetch`, rather than a mocked version. This means the test will now pass correctly.
这允许测试文件从 `node-fetch` 导入实际的 `Response` 对象,而不是模拟版本。这意味着测试将正确通过。