Skip to content

Commit e11ded3

Browse files
committed
Get rid of node-fetch requirement
1 parent 93c6e58 commit e11ded3

File tree

5 files changed

+36
-70
lines changed

5 files changed

+36
-70
lines changed

.changeset/good-crabs-applaud.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"sveltekit-playwright-fetch-mock": minor
3+
---
4+
5+
Remove node-fetch requirement

babel.config.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,10 @@
1919
"main": "dist/index.js",
2020
"module": "dist/index.mjs",
2121
"types": "dist/index.d.ts",
22-
"type": "module",
2322
"repository": "git@github.com:markjaquith/sveltekit-playwright-fetch-mock.git",
2423
"author": "Mark Jaquith <mark@jaquith.me>",
2524
"license": "MIT",
26-
"dependencies": {
27-
"node-fetch": "^3.3.0"
28-
},
25+
"dependencies": {},
2926
"peerDependencies": {
3027
"@playwright/test": "^1.29",
3128
"@sveltejs/kit": "^1.0.0",

src/mockFetch.ts

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,50 @@
1-
import { Headers } from 'node-fetch'
2-
import type { Page } from '@playwright/test'
3-
import { COOKIE_NAME } from './handle'
1+
import type { Page } from "@playwright/test";
2+
import { COOKIE_NAME } from "./handle";
43

54
type MockResponseData = {
6-
status: number
7-
statusText: string
8-
headers: [string, string][]
9-
body: string
10-
}
5+
status: number;
6+
statusText: string;
7+
headers: [string, string][];
8+
body: string;
9+
};
1110

12-
type Mocks = Record<string, MockResponseData>
11+
type Mocks = Record<string, MockResponseData>;
1312

1413
export default async function playwrightMockServerFetch(
1514
page: Page,
1615
pattern: RegExp,
1716
body: string | object | unknown[],
18-
responseInit: ResponseInit = {}
17+
responseInit: ResponseInit & { headers?: Record<string, string> } = {}
1918
) {
20-
const headers = new Headers(responseInit.headers ?? {})
21-
const status = responseInit.status ?? 200
22-
const statusText = responseInit.statusText ?? 'OK'
19+
const headers = responseInit.headers ?? {};
20+
const status = responseInit.status ?? 200;
21+
const statusText = responseInit.statusText ?? "OK";
2322

24-
if (typeof body !== 'string') {
25-
body = JSON.stringify(body)
26-
headers.set('Content-Type', 'application/json')
23+
if (typeof body !== "string") {
24+
body = JSON.stringify(body);
25+
headers["Content-Type"] = "application/json";
2726
}
2827

29-
const cookies = await page.context().cookies()
30-
const mocks: Mocks = JSON.parse(cookies.find(({ name }) => name === COOKIE_NAME)?.value ?? '{}')
31-
const mock = { status, statusText, headers: Array.from(headers), body } as MockResponseData
32-
const stringPattern = pattern.toString()
33-
mocks[stringPattern.slice(1, stringPattern.length - 1)] = mock
28+
const cookies = await page.context().cookies();
29+
const mocks: Mocks = JSON.parse(
30+
cookies.find(({ name }) => name === COOKIE_NAME)?.value ?? "{}"
31+
);
32+
const mock = {
33+
status,
34+
statusText,
35+
headers,
36+
body,
37+
} as MockResponseData;
38+
const stringPattern = pattern.toString();
39+
mocks[stringPattern.slice(1, stringPattern.length - 1)] = mock;
3440

3541
await page.context().addCookies([
3642
{
3743
name: COOKIE_NAME,
3844
value: JSON.stringify(mocks),
39-
domain: 'localhost',
40-
path: '/stay/',
45+
domain: "localhost",
46+
path: "/stay/",
4147
expires: 1707508488,
4248
},
43-
])
49+
]);
4450
}

yarn.lock

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -971,11 +971,6 @@ csv@^5.5.0:
971971
csv-stringify "^5.6.5"
972972
stream-transform "^2.1.3"
973973

974-
data-uri-to-buffer@^4.0.0:
975-
version "4.0.0"
976-
resolved "https://registry.yarnpkg.com/data-uri-to-buffer/-/data-uri-to-buffer-4.0.0.tgz#b5db46aea50f6176428ac05b73be39a57701a64b"
977-
integrity sha512-Vr3mLBA8qWmcuschSLAOogKgQ/Jwxulv3RNE4FXnYWRGujzrRWQI4m12fQqRkwX06C0KanhLr4hK+GydchZsaA==
978-
979974
debug@^4.1.0, debug@^4.3.1, debug@^4.3.4:
980975
version "4.3.4"
981976
resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.4.tgz#1319f6579357f2338d3337d2cdd4914bb5dcc865"
@@ -1339,14 +1334,6 @@ fastq@^1.6.0:
13391334
dependencies:
13401335
reusify "^1.0.4"
13411336

1342-
fetch-blob@^3.1.2, fetch-blob@^3.1.4:
1343-
version "3.2.0"
1344-
resolved "https://registry.yarnpkg.com/fetch-blob/-/fetch-blob-3.2.0.tgz#f09b8d4bbd45adc6f0c20b7e787e793e309dcce9"
1345-
integrity sha512-7yAQpD2UMJzLi1Dqv7qFYnPbaPx7ZfFK6PiIxQ4PfkGPyNyl2Ugx+a/umUonmKqjhM4DnfbMvdX6otXq83soQQ==
1346-
dependencies:
1347-
node-domexception "^1.0.0"
1348-
web-streams-polyfill "^3.0.3"
1349-
13501337
fill-range@^7.0.1:
13511338
version "7.0.1"
13521339
resolved "https://registry.yarnpkg.com/fill-range/-/fill-range-7.0.1.tgz#1919a6a7c75fe38b2c7c77e5198535da9acdda40"
@@ -1385,13 +1372,6 @@ for-each@^0.3.3:
13851372
dependencies:
13861373
is-callable "^1.1.3"
13871374

1388-
formdata-polyfill@^4.0.10:
1389-
version "4.0.10"
1390-
resolved "https://registry.yarnpkg.com/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz#24807c31c9d402e002ab3d8c720144ceb8848423"
1391-
integrity sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==
1392-
dependencies:
1393-
fetch-blob "^3.1.2"
1394-
13951375
fs-extra@^7.0.1:
13961376
version "7.0.1"
13971377
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-7.0.1.tgz#4f189c44aa123b895f722804f55ea23eadc348e9"
@@ -2031,20 +2011,6 @@ nanoid@^3.3.4:
20312011
resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.3.4.tgz#730b67e3cd09e2deacf03c027c81c9d9dbc5e8ab"
20322012
integrity sha512-MqBkQh/OHTS2egovRtLk45wEyNXwF+cokD+1YPf9u5VfJiRdAiRwB2froX5Co9Rh20xs4siNPm8naNotSD6RBw==
20332013

2034-
node-domexception@^1.0.0:
2035-
version "1.0.0"
2036-
resolved "https://registry.yarnpkg.com/node-domexception/-/node-domexception-1.0.0.tgz#6888db46a1f71c0b76b3f7555016b63fe64766e5"
2037-
integrity sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==
2038-
2039-
node-fetch@^3.3.0:
2040-
version "3.3.0"
2041-
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-3.3.0.tgz#37e71db4ecc257057af828d523a7243d651d91e4"
2042-
integrity sha512-BKwRP/O0UvoMKp7GNdwPlObhYGB5DQqwhEDQlNKuoqwVYSxkSZCSbHjnFFmUEtwSKRPU4kNK8PbDYYitwaE3QA==
2043-
dependencies:
2044-
data-uri-to-buffer "^4.0.0"
2045-
fetch-blob "^3.1.4"
2046-
formdata-polyfill "^4.0.10"
2047-
20482014
normalize-package-data@^2.5.0:
20492015
version "2.5.0"
20502016
resolved "https://registry.yarnpkg.com/normalize-package-data/-/normalize-package-data-2.5.0.tgz#e66db1838b200c1dfc233225d12cb36520e234a8"
@@ -2827,11 +2793,6 @@ wcwidth@^1.0.1:
28272793
dependencies:
28282794
defaults "^1.0.3"
28292795

2830-
web-streams-polyfill@^3.0.3:
2831-
version "3.2.1"
2832-
resolved "https://registry.yarnpkg.com/web-streams-polyfill/-/web-streams-polyfill-3.2.1.tgz#71c2718c52b45fd49dbeee88634b3a60ceab42a6"
2833-
integrity sha512-e0MO3wdXWKrLbL0DgGnUV7WHVuw9OUvL4hjgnPkIeEvESk74gAITi5G606JtZPp39cd8HA9VQzCIvA49LpPN5Q==
2834-
28352796
webidl-conversions@^4.0.2:
28362797
version "4.0.2"
28372798
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-4.0.2.tgz#a855980b1f0b6b359ba1d5d9fb39ae941faa63ad"

0 commit comments

Comments
 (0)