Skip to content

Commit

Permalink
Provide example in README
Browse files Browse the repository at this point in the history
  • Loading branch information
markjaquith committed Sep 19, 2023
1 parent 2539b29 commit e1ccfbd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/perfect-drinks-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"sveltekit-playwright-fetch-mock": patch
---

Provide example in README
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,23 @@ Allows you to mock `fetch()` requests when running Playwright E2E tests on a Sve

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.

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.
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.

## Usage

In your Playwright test:

```ts
import { expect, test } from '@playwright/test'
import { mockFetch } from 'sveltekit-playwright-fetch-mock'

test('Example.com test', async ({ page }) => {
await mockFetch(page, /^https:\/\/www\.example\.com\//, {
somedata: 'example',
})
await page.goto('/some-local-route-that-fetches-example-dot-com')
// Your Playwright assertions here.
})
```

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 comments on commit e1ccfbd

Please sign in to comment.