Skip to content

Commit

Permalink
feat: use bun serve in http tests
Browse files Browse the repository at this point in the history
Drops a dependency and simplifies serving assets related to e2e tests.
  • Loading branch information
barbmarcio authored Jul 24, 2024
1 parent ce16f32 commit 1be8541
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1 +1 @@
PLAYWRIGHT_PORT=
SERVER_PORT=
Binary file modified bun.lockb
Binary file not shown.
6 changes: 3 additions & 3 deletions e2e/auctions.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, test } from "@playwright/test";
import { apis, baseURL } from "../src/constants/apis.constant";
import { playwrightConstants } from "./constants";
import { playwrightConstants } from "./config";

test.describe("Create Auction via Topsort SDK", () => {
test("should create an auction successfully", async ({ page }) => {
Expand All @@ -23,7 +23,7 @@ test.describe("Create Auction via Topsort SDK", () => {
await route.fulfill({ json: mockAPIResponse });
});

await page.goto(playwrightConstants.url);
await page.goto(playwrightConstants.host);
const result = await page.evaluate(() => {
const config = {
apiKey: "rando-api-key",
Expand Down Expand Up @@ -59,7 +59,7 @@ test.describe("Create Auction via Topsort SDK", () => {

test("should fail to call with missing apiKey", async ({ page }) => {
const expectedError = { status: 401, statusText: "API Key is required.", body: {} };
await page.goto(playwrightConstants.url);
await page.goto(playwrightConstants.host);
const result = await page.evaluate(() => {
const config = {
apiKey: null,
Expand Down
3 changes: 3 additions & 0 deletions e2e/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const playwrightConstants = {
host: `http://localhost:${process.env.SERVER_PORT || 8080}/index.html`,
};
3 changes: 0 additions & 3 deletions e2e/constants.ts

This file was deleted.

6 changes: 3 additions & 3 deletions e2e/events.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, test } from "@playwright/test";
import { apis, baseURL } from "../src/constants/apis.constant";
import { playwrightConstants } from "./constants";
import { playwrightConstants } from "./config";

test.describe("Report Events via Topsort SDK", () => {
test("should report an successfully", async ({ page }) => {
Expand All @@ -12,7 +12,7 @@ test.describe("Report Events via Topsort SDK", () => {
await route.fulfill({ json: mockAPIResponse });
});

await page.goto(playwrightConstants.url);
await page.goto(playwrightConstants.host);
const result = await page.evaluate(() => {
const config = {
apiKey: "rando-api-key",
Expand Down Expand Up @@ -45,7 +45,7 @@ test.describe("Report Events via Topsort SDK", () => {

test("should fail to call with missing apiKey", async ({ page }) => {
const expectedError = { status: 401, statusText: "API Key is required.", body: {} };
await page.goto(playwrightConstants.url);
await page.goto(playwrightConstants.host);
const result = await page.evaluate(() => {
const config = {
apiKey: null,
Expand Down
2 changes: 1 addition & 1 deletion e2e/index.html → e2e/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Topsort SDK Test Application</title>
<script src="../dist/index.global.js"></script>
<script src="./index.global.js"></script>
</head>
<body>
<h1>Test Topsort.js Integration</h1>
Expand Down
22 changes: 22 additions & 0 deletions e2e/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { file } from "bun";

const PORT = process.env.SERVER_PORT || 8080;

Bun.serve({
fetch(req) {
const url = new URL(req.url);
const pathname = url.pathname === "/" ? "/index.html" : url.pathname;
const filePath = `./dist${pathname}`;

try {
if (filePath.endsWith(".ico")) {
return new Response("", { status: 204 });
}

return new Response(file(filePath));
} catch (e) {
return new Response("Not Found", { status: 404 });
}
},
port: PORT,
});
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"CHANGELOG.md",
"LICENSE",
"README.md",
"dist/index.global.js",
"dist/index.d.mts",
"dist/index.d.ts",
"dist/index.js",
Expand All @@ -36,13 +35,13 @@
"test:e2e": "playwright test",
"format": "biome check",
"format:fix": "biome check --write",
"prepare": "lefthook install"
"prepare": "lefthook install",
"serve:e2e": "bun run ./e2e/server.ts"
},
"devDependencies": {
"@biomejs/biome": "1.8.3",
"@playwright/test": "^1.45.2",
"@types/bun": "1.1.6",
"http-server": "^14.1.1",
"lefthook": "1.7.2",
"msw": "2.3.1",
"tsup": "8.1.0",
Expand Down
4 changes: 2 additions & 2 deletions playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { defineConfig, devices } from "@playwright/test";
export default defineConfig({
testDir: "./e2e",
timeout: 30000,
retries: 1,
retries: 0,
reporter: [["list"], ["json", { outputFile: "test-results.json" }]],
use: {
trace: "on-first-retry",
Expand All @@ -23,7 +23,7 @@ export default defineConfig({
},
],
webServer: {
command: `http-server ./ -p ${process.env.PLAYWRIGHT_PORT || 8080}`,
command: "bun run serve:e2e",
reuseExistingServer: !process.env.CI,
stdout: "ignore",
stderr: "pipe",
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
"allowJs": true,
"types": ["node"]
},
"include": ["src/*.ts", "src/**/*.ts"],
"include": ["src/*.ts", "src/**/*.ts", "e2e/server.ts"],
"exclude": ["node_modules"]
}
1 change: 1 addition & 0 deletions tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export default defineConfig({
options.keepNames = true;
options.globalName = "Topsort";
},
onSuccess: "cp -r ./e2e/public/* dist",
});

0 comments on commit 1be8541

Please sign in to comment.