https://medium.com/geekculture/how-to-mock-network-request-in-jest-40958bb8d8e2
Photo by Taylor Vick on Unsplash
Nowadays, it is necessary to modify an older library to TS
and perform unit testing. If the library is modified to TS
, there is still a little bit better. Unit testing is purely a current study and sold now. For beginners to learn the Jest
framework, I think the more troublesome thing in unit testing is to test network requests. So record some of the ways that Mock
dropped Axios
to initiate network requests. This is my 39th Medium article.
The examples mentioned in the article are all in the jest-mock-server repository. You can start the example directly by installing the package manager, for example, installing through yarn
:
$ yarn install
Some commands are specified in the package.json
, which are as follows:
npm run build
: The packaging command of rollup
.npm run test:demo1
: Simply mock
the network request library encapsulated.npm run test:demo2
: Complete the mock
by re-implement hook
.npm run test:demo3
: Use the library in Jest
to complete the implementation of demo2
.npm run test:demo4-5
: Start a node
server, proxy
the network request through the proxy of axios
, and forward it to the started node
server. By setting the corresponding unit test request and response data, the corresponding relationship is used to realize the test, which is jest-mock-server
finished work.Here we encapsulate a layer of axios
, which is closer to the real scene. You can view the test/demo/wrap-request.ts
file. In fact, it simply creates an axios
instance internally and forwards the response data.
The test/demo/index.ts
file simply exports a counter
method, where these two parameters are processed to a certain extent before the network request is initiated. Then the response data is also processed to a certain extent, just to simulate related operations.
Here Jest
uses the browser environment simulated by JSDOM
, the startup file test/config/setup.js
is configured in the setupFiles
attribute configured in jest.config.js
, and JSDOM
is initialized here.
Simple mock
processing is performed in test/demo1.test.js
, and you can try to run it through npm run test:demo1
. In fact, a mock
operation is performed on the wrap-request
library that wraps axios
. wrap-request
will be compiled when Jest
is started. After the library is mocked here, all the files imported into the library afterward will get the mocked objects. In other words, we can think that this library has been rewritten, and the methods after rewriting are all JEST
’s Mock Functions
. You can use functions such as mockReturnValue
for data simulation. For Mock Functions
, please refer to this link.
Here we have completed the Mock
of the return value, which means that we can control the value returned by the request
in the wrap-request
library. However, it was mentioned before that there are also certain processes for the incoming parameters. We haven’t made any assertions on this part of the content, so we also need to try to deal with this.