Skip to content

Commit

Permalink
✅ test: add e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nismit committed May 9, 2024
1 parent 4a90097 commit e94707d
Show file tree
Hide file tree
Showing 7 changed files with 310 additions and 9 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/playwright.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Playwright Tests
on:
pull_request:
branches: [develop, master]
jobs:
test:
timeout-minutes: 60
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: lts/*
- name: Install dependencies
run: npm ci
- name: Prepare temporary contents data
run: npm run prebuild
- name: Install Playwright Browsers
run: npx playwright install --with-deps
- name: Run Playwright tests
run: npx playwright test
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,12 @@ yarn-error.log*
.firebase
commandlist.md
tmp

# test data
content/post/2050/*

# playwright
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
73 changes: 65 additions & 8 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"prebuild": "node ./app/build.js",
"storybook": "storybook dev -p 6006",
"build-storybook": "storybook build",
"test": "echo \"Error: no test specified\" && exit 1",
"chromatic": "chromatic --exit-zero-on-changes --project-token=CHROMATIC_PROJECT_TOKEN"
},
"repository": {
Expand All @@ -29,7 +28,9 @@
},
"devDependencies": {
"@hono/vite-ssg": "^0.1.0",
"@playwright/test": "^1.44.0",
"@types/mdast": "^4.0.3",
"@types/node": "^20.12.11",
"fast-glob": "^3.3.2",
"gray-matter": "^4.0.3",
"remark": "^15.0.1",
Expand Down
78 changes: 78 additions & 0 deletions playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { defineConfig, devices } from "@playwright/test";

/**
* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
// require('dotenv').config();

/**
* See https://playwright.dev/docs/test-configuration.
*/
export default defineConfig({
testDir: "./tests",
/* Run tests in files in parallel */
fullyParallel: true,
/* Fail the build on CI if you accidentally left test.only in the source code. */
forbidOnly: !!process.env.CI,
/* Retry on CI only */
retries: process.env.CI ? 2 : 0,
/* Opt out of parallel tests on CI. */
workers: process.env.CI ? 1 : undefined,
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
reporter: "html",
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
/* Base URL to use in actions like `await page.goto('/')`. */
// baseURL: 'http://127.0.0.1:3000',
baseURL: "http://localhost:3000",

/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: "on-first-retry",
},

/* Configure projects for major browsers */
projects: [
{
name: "chromium",
use: { ...devices["Desktop Chrome"] },
},

{
name: "firefox",
use: { ...devices["Desktop Firefox"] },
},

{
name: "webkit",
use: { ...devices["Desktop Safari"] },
},

/* Test against mobile viewports. */
// {
// name: 'Mobile Chrome',
// use: { ...devices['Pixel 5'] },
// },
// {
// name: 'Mobile Safari',
// use: { ...devices['iPhone 12'] },
// },

/* Test against branded browsers. */
// {
// name: 'Microsoft Edge',
// use: { ...devices['Desktop Edge'], channel: 'msedge' },
// },
// {
// name: 'Google Chrome',
// use: { ...devices['Desktop Chrome'], channel: 'chrome' },
// },
],

/* Run your local dev server before starting the tests */
webServer: {
command: "npx vite dev --port 3000",
url: "http://localhost:3000",
reuseExistingServer: !process.env.CI,
},
});
98 changes: 98 additions & 0 deletions tests/details.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import fs from "fs";
import { test, expect } from "@playwright/test";

const TEST_DATA = `---
draft: false
date: "2050-01-01T00:00:00-0800"
title: テスト記事
description: これはテスト記事です。
eyecatch: /images/eyecatch/eye-move-to-hono.jpg
tags:
- blog
toc: false
---
これはテスト記事です。
# 見出し1
これはテキストです。`;

// Annotate entire file as serial.
test.describe.configure({ mode: "serial" });

test.describe("Detail Page general UI tests", () => {
test.beforeAll(async () => {
await fs.promises.mkdir("./content/post/2050/01", { recursive: true });
fs.writeFile(`./content/post/2050/01/test.md`, TEST_DATA, "utf8", (err) =>
console.log(err)
);
});

test("has specific article", async ({ page }) => {
await page.goto("/");
const locator = page.locator("article");
const article = locator.nth(0);
await expect(article.locator(".postCard__title")).toHaveText("テスト記事");
await expect(article.locator(".postCard__desc")).toHaveText(
"これはテスト記事です。"
);
await expect(article.locator("time")).toHaveText("2050/01/01");
});

test("navigate to the article", async ({ page }) => {
await page.goto("/");
const locator = page.locator("article");
const article = locator.nth(0).locator("a");
await article.click();
await page.waitForURL(/post\/2050\/01\/test/);
await expect(page).toHaveURL(/post\/2050\/01\/test/);
});

test("display proper article", async ({ page }) => {
await page.goto("/post/2050/01/test");
const locator = page.locator("article");
await expect(locator.locator("section")).toHaveCount(3);
});

test("display proper title", async ({ page }) => {
await page.goto("/post/2050/01/test");
const locator = page.getByRole("heading");
await expect(locator.first()).toHaveText("テスト記事");
});

test("display proper content", async ({ page }) => {
await page.goto("/post/2050/01/test");
const content = page.locator("article > section").nth(1);
await expect(content.locator("p").nth(0)).toHaveText(
"これはテスト記事です。"
);
await expect(content.locator("h1")).toHaveText("見出し1");
await expect(content.locator("p").nth(1)).toHaveText(
"これはテキストです。"
);
});

test("has tag link", async ({ page }) => {
await page.goto("/post/2050/01/test");
const locator = page.locator(".article__tags__item");
const link = locator.locator("a");
await expect(link).toHaveText("blog");
await expect(link).toHaveAttribute("href", "/tags/blog");
});

test("navigate tag page", async ({ page }) => {
await page.goto("/post/2050/01/test");
const locator = page.locator(".article__tags__item");
const link = locator.locator("a");
await link.click();
await page.waitForURL(/tags\/blog/);
await expect(page).toHaveURL(/tags\/blog/);
});

test("has related posts", async ({ page }) => {
await page.goto("/post/2050/01/test");
const locator = page.locator(".related-articles--container");
await expect(locator).toHaveCount(5);
});
});
37 changes: 37 additions & 0 deletions tests/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { test, expect } from "@playwright/test";

test.describe("Root Page general UI tests", () => {
test("has title", async ({ page }) => {
await page.goto("/");
await expect(page).toHaveTitle("NISLOG");
});

test("has proper articles", async ({ page }) => {
await page.goto("/");
const locator = page.locator("article");
await expect(locator).toHaveCount(10);
});

test("has logo with root page link", async ({ page }) => {
await page.goto("/");
const locator = page.locator(".logoLink");
await expect(locator).toHaveAttribute("href", "/");
await expect(locator).toHaveAttribute("title", "NISLOG");
});

test("has footer", async ({ page }) => {
await page.goto("/");
const locator = page.locator(".copyright");
const currentYear = new Date().getFullYear();
await expect(locator).toContainText(`© 2017 - ${currentYear} NISLOG`);
});

test("Pagination:link should work", async ({ page }) => {
await page.goto("/");
const locator = page.locator('[role="navigation"]:not(.tagCloud__list)');
const children = locator.locator("li").nth(1);
await children.click();
await page.waitForURL(/page\/2/);
await expect(page).toHaveURL(/page\/2/);
});
});

0 comments on commit e94707d

Please sign in to comment.