Skip to content
This repository has been archived by the owner on Nov 2, 2024. It is now read-only.

Commit

Permalink
Use msw mocking in e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
johnhunter committed Oct 23, 2023
1 parent f8031c9 commit 1beec6b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
18 changes: 18 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"eslint-plugin-react": "^7.33.2",
"jsdom": "^22.1.0",
"msw": "^1.2.1",
"playwright-msw": "^2.2.1",
"prettier": "^3.0.3",
"vite": "^4.4.11",
"vitest": "^0.34.6"
Expand Down
2 changes: 1 addition & 1 deletion src/mocks/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { server } from './server';
export { url } from './handlers';
export { url, handlers } from './handlers';
export * from './data';
11 changes: 5 additions & 6 deletions tests/home.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { test, expect } from '@playwright/test';
import { test, expect } from './msw-fixture';
import { posts } from '@/mocks/data';

test.describe('Home page', () => {
test('has title', async ({ page }) => {
Expand All @@ -15,10 +16,8 @@ test.describe('Home page', () => {
});

test('has a link to a post', async ({ page }) => {
// TODO: use mocked data rather than external API
const name =
'sunt aut facere repellat provident occaecati excepturi optio reprehenderit';
const length = 100;
const postsData = posts();
const name = postsData[0].title;

await page.goto('/');

Expand All @@ -27,7 +26,7 @@ test.describe('Home page', () => {
name,
});

await expect(list.getByRole('link')).toHaveCount(length);
await expect(list.getByRole('link')).toHaveCount(postsData.length);
await expect(thePost).toBeInViewport();

await thePost.click();
Expand Down
9 changes: 9 additions & 0 deletions tests/msw-fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { test as base } from '@playwright/test';
import type { MockServiceWorker } from 'playwright-msw';
import { createWorkerFixture } from 'playwright-msw';
import { handlers } from '../src/mocks/handlers';
export { expect } from '@playwright/test';

export const test = base.extend<{
worker: MockServiceWorker;
}>({ worker: createWorkerFixture(handlers) });

0 comments on commit 1beec6b

Please sign in to comment.