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

feat(test): e2e #162

Merged
merged 2 commits into from
Jan 28, 2025
Merged
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
37 changes: 37 additions & 0 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Playwright Tests
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main, develop ]
jobs:
test:
timeout-minutes: 60
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Install pnpm
uses: pnpm/action-setup@v4.0.0

- name: Install dependencies
run: pnpm install

- name: Build vitepress-openapi
run: pnpm build

- name: Install Playwright Browsers
run: pnpm exec playwright install --with-deps

- name: Run Playwright tests
run: pnpm exec playwright test

- uses: actions/upload-artifact@v4
if: always()
with:
name: playwright-report
path: |
playwright-report/
test-results/
retention-days: 30
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ docs/.vitepress/cache
docs/.vitepress/.temp
types
.idea

# Playwright
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
6 changes: 6 additions & 0 deletions e2e/dev/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules
dist
.temp
cache
.idea
.vscode
68 changes: 68 additions & 0 deletions e2e/dev/docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { fileURLToPath } from 'node:url'
import { defineConfig } from 'vitepress'
import { useSidebar } from 'vitepress-openapi'
import spec from '../../../../docs/public/openapi.json'

const sidebar = useSidebar({
spec,
// Optionally, you can specify a link prefix for all generated sidebar items.
linkPrefix: '/operations/',
})

// refer https://vitepress.dev/reference/site-config for details
export default defineConfig({
lang: 'en-US',
title: 'VitePress OpenAPI',
description: 'Generate documentation from OpenAPI specifications.',

themeConfig: {
nav: [{ text: 'API Reference', link: '/introduction' }],

sidebar: [
{
text: 'By Tags',
items: [
{
text: 'Introduction',
link: '/introduction',
},
...sidebar.itemsByTags(),
],
},
{
text: 'By Operations',
items: [
...sidebar.generateSidebarGroups(),
],
},
{
text: 'By Paths',
items: [
...sidebar.itemsByPaths(),
],
},
{
text: 'One Page',
items: [
{ text: 'One Page', link: '/one-page' },
{ text: 'Without Sidebar', link: '/without-sidebar' },
],
},
],
},

vite: {
resolve: {
alias: {
...(process.env.NODE_ENV === 'production'
? {}
: {
'vitepress-openapi/client': fileURLToPath(new URL('../../../../src/client', import.meta.url)),
'vitepress-openapi/dist/style.css': fileURLToPath(new URL('../../../../dist/vitepress-openapi.css', import.meta.url)),
'vitepress-openapi': fileURLToPath(new URL('../../../../src/index', import.meta.url)),
'@public': fileURLToPath(new URL('../../../../docs/public', import.meta.url)),
}),
},
},
},
})
18 changes: 18 additions & 0 deletions e2e/dev/docs/.vitepress/theme/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { Theme } from 'vitepress'
import spec from '@public/openapi.json'

import { theme, useOpenapi } from 'vitepress-openapi/client'
import DefaultTheme from 'vitepress/theme'
import 'vitepress-openapi/dist/style.css'

export default {
...DefaultTheme,
async enhanceApp({ app, router, siteData }) {
const openapi = useOpenapi({
spec,
config: {},
})

theme.enhanceApp({ app, openapi })
},
} satisfies Theme
14 changes: 14 additions & 0 deletions e2e/dev/docs/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
layout: home

hero:
name: "VitePress OpenAPI"
tagline: "Generate documentation from OpenAPI specifications."
actions:
- theme: brand
text: API Reference
link: /introduction
- theme: alt
text: Documentation
link: https://vitepress-openapi.vercel.app/
---
7 changes: 7 additions & 0 deletions e2e/dev/docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
title: vitepress-openapi
---

<OAInfo />

<OAServers />
12 changes: 12 additions & 0 deletions e2e/dev/docs/one-page.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
aside: false
title: vitepress-openapi
---

<script setup lang="ts">
import { useData } from 'vitepress'

const { isDark } = useData()
</script>

<OASpec :isDark="isDark" />
17 changes: 17 additions & 0 deletions e2e/dev/docs/operations/[operationId].md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
aside: false
outline: false
title: vitepress-openapi
---

<script setup lang="ts">
import { useRoute, useData } from 'vitepress'

const route = useRoute()

const { isDark } = useData()

const operationId = route.data.params.operationId
</script>

<OAOperation :operationId="operationId" :isDark="isDark" />
17 changes: 17 additions & 0 deletions e2e/dev/docs/operations/[operationId].paths.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { usePaths } from 'vitepress-openapi'
import spec from '../../../../docs/public/openapi.json'

export default {
paths() {
return usePaths({ spec })
.getPathsByVerbs()
.map(({ operationId, summary }) => {
return {
params: {
operationId,
pageTitle: `${summary} - vitepress-openapi`,
},
}
})
},
}
17 changes: 17 additions & 0 deletions e2e/dev/docs/tags/[tag].md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
aside: false
outline: false
title: vitepress-openapi
---

<script setup lang="ts">
import { useRoute, useData } from 'vitepress'

const route = useRoute()

const { isDark } = useData()

const tag = route.data.params.tag
</script>

<OASpec :tags="[tag]" :isDark="isDark" hide-info hide-servers hide-paths-summary />
17 changes: 17 additions & 0 deletions e2e/dev/docs/tags/[tag].paths.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { usePaths } from 'vitepress-openapi'
import spec from '../../../../docs/public/openapi.json'

export default {
paths() {
return usePaths({ spec })
.getTags()
.map(({ name }) => {
return {
params: {
tag: name,
pageTitle: `${name} - vitepress-openapi`,
},
}
})
},
}
14 changes: 14 additions & 0 deletions e2e/dev/docs/without-sidebar.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
sidebar: false
aside: true
outline: [1, 2]
title: vitepress-openapi
---

<script setup lang="ts">
import { useData } from 'vitepress'
import spec from '@public/openapi.json'
const { isDark } = useData()
</script>

<OASpec :spec="spec" :isDark="isDark" />
14 changes: 14 additions & 0 deletions e2e/dev/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"private": true,
"type": "module",
"scripts": {
"dev": "vitepress dev docs",
"build": "vitepress build docs",
"preview": "vitepress preview docs",
"start": "vitepress dev docs"
},
"devDependencies": {
"vitepress": "^1.6.3",
"vitepress-openapi": "workspace:*"
}
}
10 changes: 10 additions & 0 deletions e2e/home.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { expect, test } from '@playwright/test'

test('home', async ({ page }) => {
await page.goto('/')

await expect(page).toHaveTitle(/VitePress OpenAPI/)
await expect(page).toHaveScreenshot({
fullPage: true,
})
})
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions e2e/one-page.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { expect, test } from '@playwright/test'

test('one-page', async ({ page }) => {
await page.goto('/one-page')

await expect(page).toHaveScreenshot({
fullPage: true,
})
})
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
"docs:build": "pnpm run build && cd docs && pnpm run build",
"test": "vitest",
"test:run": "vitest --run",
"typecheck": "vue-tsc --noEmit"
"typecheck": "vue-tsc --noEmit",
"e2e:dev": "cd e2e/dev && vitepress dev docs --port 4173"
},
"peerDependencies": {
"vitepress": ">=1.0.0",
Expand All @@ -61,6 +62,7 @@
},
"devDependencies": {
"@antfu/eslint-config": "^4.1.0",
"@playwright/test": "^1.50.0",
"@scalar/openapi-types": "^0.1.6",
"@sindresorhus/slugify": "^2.2.1",
"@trojs/openapi-dereference": "^1.0.1",
Expand Down
77 changes: 77 additions & 0 deletions playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { defineConfig, devices } from '@playwright/test'

const port = 4173

const url = `http://localhost:${port}`

export default defineConfig({
testDir: './e2e',
/* 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: url,

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

timeout: 60 * 1000,
expect: {
timeout: 30 * 1000,
},

/* 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: 'pnpm e2e:dev',
port,
reuseExistingServer: !process.env.CI,
},
})
Loading