Skip to content

Commit e1ccfbd

Browse files
committed
Provide example in README
1 parent 2539b29 commit e1ccfbd

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

.changeset/perfect-drinks-smile.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": patch
3+
---
4+
5+
Provide example in README

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,23 @@ Allows you to mock `fetch()` requests when running Playwright E2E tests on a Sve
44

55
Playwright already lets you mock browser `fetch()` requests. This allows you to tell the SvelteKit dev server to deliver mocked responses for `fetch()` calls it makes while doing Server-Side Rendering.
66

7-
This can speed up your end-to-end tests and make them more reliable, as well as not require credentials for external APIs to be input to your continuous integration environment.
7+
This can speed up your end-to-end tests and make them more reliable, as well as not require credentials for external APIs to be input to your continuous integration environment.
8+
9+
## Usage
10+
11+
In your Playwright test:
12+
13+
```ts
14+
import { expect, test } from '@playwright/test'
15+
import { mockFetch } from 'sveltekit-playwright-fetch-mock'
16+
17+
test('Example.com test', async ({ page }) => {
18+
await mockFetch(page, /^https:\/\/www\.example\.com\//, {
19+
somedata: 'example',
20+
})
21+
await page.goto('/some-local-route-that-fetches-example-dot-com')
22+
// Your Playwright assertions here.
23+
})
24+
```
25+
26+
So what this means is that when you go to `/some-local-route-that-fetches-example-dot-com` in your SvelteKit app, and the `+page.server.js` or `+page.server.ts` file for that route calls `fetch` (using the one that SvelteKit provides you) to get data from `https://www.example.com/`, this package will intercept that request and return `{ somedata: "example" }`

0 commit comments

Comments
 (0)