Repo to show issues related to using msw
and supertest
at the same time in GraphQL testing.
- Use
msw
to mock theget
API call in the filesrc/utils/vendor/api.ts
. - Do not mock the
post
to the GraphQL app viasupertest
.- The GraphQL resolver,
src/graphql/Website/resolvers.ts
, calls the API function that does theget
to be mocked.
- The GraphQL resolver,
-
Clone the Repo
-
yarn install
dependencies. -
Run
yarn test
.An error like this should be shown:
Error: cannot undefined undefined (500) at Response.toError (node_modules/superagent/lib/node/response.js:94:15) at ResponseBase._setStatusProperties (node_modules/superagent/lib/response-base.js:123:16) at new Response (node_modules/superagent/lib/node/response.js:41:8) at Test.Request._emitResponse (node_modules/superagent/lib/node/index.js:752:20) at IncomingMessage.<anonymous> (node_modules/superagent/lib/node/index.js:916:38) at IncomingMessage.emit (events.js:326:22) at endReadableNT (_stream_readable.js:1226:12) at processTicksAndRejections (internal/process/task_queues.js:80:21) { status: 500, text: 'POST body missing. Did you forget use body-parser middleware?', method: undefined, path: undefined }
-
Open the file
src/graphql/Website/__tests__/getWebsitesQuery.spec.ts
and comment all themsw
related code. -
Run
yarn test
againThe expected ECONNREFUSED error should be shown since the API call is not being mocked:
request to https://localhost:9000/sites/created failed, reason: connect ECONNREFUSED 127.0.0.1:9000