Reputation: 126
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?
Upvotes: 0
Views: 5290
Reputation: 2043
In the tutorial for msw
, the mock server is configured in setupTests.js
.
Because CRA (create-react-app) already configure jest
for you and CRA will load setupTests.js
when you run test with react-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 in package.json
to jest
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
npm run test App.test.js
Output:
Upvotes: 4