I have a react project in this git repo created from scratch without using create-react-app, so I am using jest.config.js and jest.setup.js files as suggested by msw doc for mocking network request for testing. I get the below error when I executing the test.
(node:12207) ExperimentalWarning: The fs.promises API is experimental console.error Error: Error: connect ECONNREFUSED 127.0.0.1:5000
The same test code works fine when I create a new project using create-react-app containing setupTests.js, but I want to mock api request in a react project which was created manually from scratch.
Any suggestion?
In the tutorial for
msw
, the mock server is configured insetupTests.js
. Because CRA (create-react-app) already configurejest
for you and CRA will loadsetupTests.js
when you run test withreact-scripts test
.If you want to create a new project from scratch (supposed you already installed react, react-dom, ... ).
You have to config
jest
your self (refer this docs), steps needed:test
scripts inpackage.json
tojest
jest.config.js
(like you did, i also add jsdom as test environment)jest.setup.js
to load mock serverI created a PR that did above steps for you, to test just run
Output: