Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use dev server when running tests #172

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
</head>
<body>
<div id="root"></div>
<script>
let global = globalThis;
</script>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
3 changes: 2 additions & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
"dev": "concurrently \"npm:dev:*\"",
"dev:vite": "vite",
"dev:relay": "relay-compiler --watch",
"preview": "relay-compiler && vite preview --port 3000",
"build": "relay-compiler && tsc --noEmit && vite build",
"test": "concurrently \"npm:build\" && playwright test"
"test": "playwright test"
},
"dependencies": {
"react": "^17.0.0",
Expand Down
9 changes: 9 additions & 0 deletions example/playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { PlaywrightTestConfig } from "@playwright/test";

const config: PlaywrightTestConfig = {
webServer: {
command: "yarn dev",
port: 3000,
},
};
export default config;
27 changes: 4 additions & 23 deletions example/src/App.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { test, expect, Page } from "@playwright/test";
import { GraphQLResponseWithData as RelayGraphQLResponseWithData } from "relay-runtime";
import path from "path";
import { AppQueryResponse } from "./__generated__/AppQuery.graphql";

// Mutable removes the readonly property from a type. This is done because the Relay compiler outputs types with readonly fields.
Expand Down Expand Up @@ -28,25 +27,7 @@ async function modifyGraphQLResponse(
});
}

// This beforeEach hook ensures that the compiled Vite project is served at http://localhost/ for each test.
test.beforeEach(async ({ page }) => {
await page.route("http://localhost/**", (route) => {
if (route.request().resourceType() === "document") {
return route.fulfill({
status: 200,
path: path.join(__dirname, "../dist/index.html"),
});
} else {
const url = new URL(route.request().url());
return route.fulfill({
status: 200,
path: path.join(__dirname, "../dist", url.pathname),
});
}
});
});

test("renders list with many ships", async ({ page }) => {
test("renders list with many ships", async ({ page, baseURL }) => {
await modifyGraphQLResponse(
page,
"https://api.spacex.land/graphql",
Expand All @@ -66,7 +47,7 @@ test("renders list with many ships", async ({ page }) => {
},
);

await page.goto(`http://localhost`, { waitUntil: "networkidle" });
await page.goto(baseURL || "/", { waitUntil: "networkidle" });

// Check title exists and hence that page rendered correctly
const titleTxt = page.locator("h1");
Expand All @@ -82,7 +63,7 @@ test("renders list with many ships", async ({ page }) => {
await expect(await shipsListChildren.nth(1).textContent()).toBe("Ship Two");
});

test("renders list with no ships", async ({ page }) => {
test("renders list with no ships", async ({ page, baseURL }) => {
await modifyGraphQLResponse(
page,
"https://api.spacex.land/graphql",
Expand All @@ -92,7 +73,7 @@ test("renders list with no ships", async ({ page }) => {
},
);

await page.goto(`http://localhost`, { waitUntil: "networkidle" });
await page.goto(baseURL || "/", { waitUntil: "networkidle" });

// Check title exists and hence that page rendered correctly
const titleTxt = page.locator("h1");
Expand Down