Skip to content

Commit

Permalink
chore: remove "node-fetch" (#1864)
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito authored Nov 16, 2023
1 parent 276157a commit 28efd32
Show file tree
Hide file tree
Showing 34 changed files with 44 additions and 62 deletions.
1 change: 0 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ Let's replicate the same `GET /books` integration test in Node.js.

```ts
// test/node/example.test.ts
import fetch from 'node-fetch'
import { http, HttpResponse } from 'msw'
import { setupServer } from 'msw/node'

Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@
"inquirer": "^8.2.0",
"is-node-process": "^1.2.0",
"js-levenshtein": "^1.1.6",
"node-fetch": "^2.6.7",
"outvariant": "^1.4.0",
"path-to-regexp": "^6.2.0",
"strict-event-emitter": "^0.5.0",
Expand All @@ -146,7 +145,6 @@
"@types/glob": "^8.1.0",
"@types/json-bigint": "^1.0.1",
"@types/node": "18.x",
"@types/node-fetch": "^2.5.11",
"@types/puppeteer": "^5.4.4",
"@typescript-eslint/eslint-plugin": "^5.11.0",
"@typescript-eslint/parser": "^5.11.0",
Expand Down
19 changes: 3 additions & 16 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion test/node/graphql-api/anonymous-operations.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* @vitest-environment node
*/
import fetch from 'node-fetch'
import { HttpServer } from '@open-draft/test-server/http'
import { HttpResponse, graphql } from 'msw'
import { setupServer } from 'msw/node'
Expand Down
1 change: 0 additions & 1 deletion test/node/graphql-api/cookies.node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* @vitest-environment node
*/
import cookieUtils from '@bundled-es-modules/cookie'
import fetch from 'node-fetch'
import { graphql as executeGraphql, buildSchema } from 'graphql'
import { graphql, HttpResponse } from 'msw'
import { setupServer } from 'msw/node'
Expand Down
1 change: 0 additions & 1 deletion test/node/graphql-api/extensions.node.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* @vitest-environment node
*/
import fetch from 'node-fetch'
import type { ExecutionResult } from 'graphql'
import { buildSchema, graphql as executeGraphql } from 'graphql'
import { graphql, HttpResponse } from 'msw'
Expand Down
1 change: 0 additions & 1 deletion test/node/msw-api/context/delay.node.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* @vitest-environment node
*/
import fetch from 'node-fetch'
import { delay, HttpResponse, http } from 'msw'
import { setupServer } from 'msw/node'
import { performance } from 'perf_hooks'
Expand Down
1 change: 0 additions & 1 deletion test/node/msw-api/req/passthrough.node.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* @vitest-environment node
*/
import fetch from 'node-fetch'
import { HttpResponse, passthrough, http } from 'msw'
import { setupServer } from 'msw/node'
import { HttpServer } from '@open-draft/test-server/http'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* @vitest-environment node
*/
import fetch from 'node-fetch'
import { HttpResponse, http } from 'msw'
import { setupServer } from 'msw/node'
import { HttpServer } from '@open-draft/test-server/http'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* @vitest-environment node
*/
import fetch from 'node-fetch'
import { HttpResponse, http } from 'msw'
import { setupServer } from 'msw/node'
import { HttpServer } from '@open-draft/test-server/http'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* @vitest-environment node
*/
import fetch from 'node-fetch'
import { HttpResponse, http } from 'msw'
import { setupServer } from 'msw/node'
import { HttpServer } from '@open-draft/test-server/http'
Expand Down
1 change: 0 additions & 1 deletion test/node/msw-api/setup-server/resetHandlers.node.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* @vitest-environment node
*/
import fetch from 'node-fetch'
import { HttpResponse, http } from 'msw'
import { setupServer } from 'msw/node'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* @vitest-environment node
*/
import fetch from 'node-fetch'
import { HttpResponse, http } from 'msw'
import { setupServer } from 'msw/node'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import fetch from 'node-fetch'
import * as JSONbig from 'json-bigint'
import { http, HttpResponse } from 'msw'
import { setupServer } from 'msw/node'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fetch from 'node-fetch'
// @vitest-environment node
import { setupServer } from 'msw/node'
import { HttpResponse, http } from 'msw'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* @vitest-environment node
*/
import fetch from 'node-fetch'
import { HttpResponse, http } from 'msw'
import { setupServer } from 'msw/node'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import fetch from 'node-fetch'
import { HttpResponse, http } from 'msw'
import { setupServer } from 'msw/node'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* @vitest-environment node
*/
import fetch from 'node-fetch'
import { HttpResponse, http } from 'msw'
import { setupServer } from 'msw/node'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* @vitest-environment node
*/
import fetch from 'node-fetch'
import { HttpServer } from '@open-draft/test-server/http'
import { HttpResponse, http } from 'msw'
import { setupServer } from 'msw/node'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* @vitest-environment node
*/
import fetch from 'node-fetch'
import { setupServer } from 'msw/node'
import { HttpResponse, http } from 'msw'

Expand Down Expand Up @@ -29,10 +28,19 @@ afterAll(() => {
})

test('prevents a request when a custom callback throws an exception', async () => {
const getResponse = () => fetch('https://example.com')
const makeRequest = () => {
return fetch('https://example.com')
.then(() => {
throw new Error('Must not resolve')
})
.catch<Error>((error) => error)
}

const requestError = await makeRequest()

// Request should be cancelled with a fetch error, since the callback threw.
await expect(getResponse()).rejects.toThrow(
'request to https://example.com/ failed, reason: Custom error for GET https://example.com/',
expect(requestError.message).toBe('Failed to fetch')
expect(requestError.cause).toEqual(
new Error('Custom error for GET https://example.com/'),
)
})
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* @vitest-environment node
*/
import fetch from 'node-fetch'
import { setupServer } from 'msw/node'
import { HttpResponse, http } from 'msw'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* @vitest-environment node
*/
import fetch from 'node-fetch'
import { setupServer } from 'msw/node'
import { HttpResponse, http } from 'msw'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* @vitest-environment node
*/
import fetch from 'node-fetch'
import { HttpServer } from '@open-draft/test-server/http'
import { HttpResponse, http } from 'msw'
import { setupServer } from 'msw/node'
Expand Down Expand Up @@ -56,11 +55,29 @@ afterAll(async () => {

test('errors on unhandled request when using the "error" value', async () => {
const endpointUrl = httpServer.http.url('/')
const makeRequest = () => fetch(endpointUrl)
const makeRequest = () => {
return fetch(endpointUrl)
.then(() => {
throw new Error('Must not resolve')
})
.catch<Error>((error) => {
return error
})
}

await expect(() => makeRequest()).rejects.toThrow(
`request to ${endpointUrl} failed, reason: [MSW] Cannot bypass a request when using the "error" strategy for the "onUnhandledRequest" option.`,
const requestError = await makeRequest()

expect(requestError.message).toBe('Failed to fetch')
/**
* @note Undici wraps fetch rejections in a generic "Failed to fetch" error,
* forwarding the actual rejection in the "error.cause" property.
*/
expect(requestError.cause).toEqual(
new Error(
'[MSW] Cannot bypass a request when using the "error" strategy for the "onUnhandledRequest" option.',
),
)

expect(console.error)
.toHaveBeenCalledWith(`[MSW] Error: intercepted a request without a matching request handler:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* @vitest-environment node
*/
import fetch from 'node-fetch'
import { setupServer } from 'msw/node'
import { HttpResponse, http } from 'msw'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* @vitest-environment node
*/
import fetch from 'node-fetch'
import { HttpResponse, http } from 'msw'
import { setupServer } from 'msw/node'

Expand Down
1 change: 0 additions & 1 deletion test/node/msw-api/setup-server/use.node.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* @vitest-environment node
*/
import fetch from 'node-fetch'
import { HttpResponse, http } from 'msw'
import { SetupServer, setupServer } from 'msw/node'
import { RequestHandler as ExpressRequestHandler } from 'express'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* @vitest-environment node
*/
import fetch from 'node-fetch'
import { http, HttpResponse } from 'msw'
import { setupServer } from 'msw/node'

Expand Down
1 change: 0 additions & 1 deletion test/node/rest-api/request/body/body-json.node.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* @vitest-environment node
*/
import fetch from 'node-fetch'
import { HttpResponse, http } from 'msw'
import { setupServer } from 'msw/node'
import { encodeBuffer } from '@mswjs/interceptors'
Expand Down
1 change: 0 additions & 1 deletion test/node/rest-api/request/body/body-text.node.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* @vitest-environment node
*/
import fetch from 'node-fetch'
import { HttpResponse, http } from 'msw'
import { setupServer } from 'msw/node'
import { encodeBuffer } from '@mswjs/interceptors'
Expand Down
1 change: 0 additions & 1 deletion test/node/rest-api/request/matching/all.node.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* @vitest-environment node
*/
import fetch, { Response } from 'node-fetch'
import { HttpServer } from '@open-draft/test-server/http'
import { HttpMethods, http, HttpResponse } from 'msw'
import { setupServer } from 'msw/node'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* @vitest-environment node
*/
import fetch from 'node-fetch'
import { HttpResponse, http } from 'msw'
import { setupServer } from 'msw/node'

Expand Down
Loading

0 comments on commit 28efd32

Please sign in to comment.