Skip to content

Commit

Permalink
test: api reference cdn (#1085)
Browse files Browse the repository at this point in the history
* feat: fastify server for static html cdn testing

* test: playwright ui test to validate cdn api reference

* chore: resolve lockfile

* chore: run testing server in background

* test: remove failing expect

* chore: clear request cache headers

* chore: move playwright test folder

* chore: only run playwright e2e tests

* chore: resolve lockfile
  • Loading branch information
tmastrom committed Feb 29, 2024
1 parent e9adcf1 commit 1f2921f
Show file tree
Hide file tree
Showing 11 changed files with 433 additions and 177 deletions.
46 changes: 46 additions & 0 deletions .github/workflows/test-ui.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Playwright Tests

on:
schedule:
# every hour
- cron: '0 * * * *'
push:

jobs:
test:
runs-on: ubuntu-20.04
strategy:
matrix:
node-version: [20]

steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v3
with:
version: 8
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'pnpm'
- name: Install dependencies
run: pnpm --filter cdn-api-reference install
- name: Turborepo cache
uses: dtinth/setup-github-actions-caching-for-turbo@v1
- name: Start static html server
run: pnpm --filter cdn-api-reference dev &
- name: Get installed Playwright version
id: playwright-version
run: echo "PLAYWRIGHT_VERSION=$(node -e "console.log(require('./package.json').devDependencies['@playwright/test'])")" >> $GITHUB_ENV
# TODO doesn't work
- name: Cache playwright binaries
uses: actions/cache@v4
id: playwright-cache
with:
path: '~/.cache/ms-playwright'
key: '${{ runner.os }}-playwright-${{ env.PLAYWRIGHT_VERSION }}'
restore-keys: ${{ runner.os }}-playwright-
- name: Install Playwright browser binaries & OS dependencies
run: pnpm exec playwright install --with-deps
- name: Run e2e tests
run: CI=1 pnpm test:e2e
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ tsconfig.tsbuildinfo

# Testing data
test-*.json
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
28 changes: 28 additions & 0 deletions examples/cdn-api-reference/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "@scalar-examples/cdn-api-reference",
"license": "MIT",
"author": "Scalar (https://github.com/scalar)",
"homepage": "https://github.com/scalar/scalar",
"bugs": "https://github.com/scalar/scalar/issues/new/choose",
"version": "0.0.1",
"private": true,
"engines": {
"node": ">=20"
},
"scripts": {
"dev": "nodemon --exec \"vite-node src/index.ts\" --ext ts --quiet --watch ./",
"types:check": "tsc --noEmit --skipLibCheck"
},
"type": "module",
"dependencies": {
"@fastify/static": "^7.0.1",
"fastify": "^4.23.2"
},
"devDependencies": {
"@types/node": "^20.6.3",
"nodemon": "^3.0.1",
"typescript": "^5.2.2",
"vite": "^5.1.1",
"vite-node": "^1.2.2"
}
}
22 changes: 22 additions & 0 deletions examples/cdn-api-reference/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import fastifyStatic from '@fastify/static'
import fastify from 'fastify'
import { join } from 'node:path'

const app = await fastify({ logger: true })

await app.register(fastifyStatic, {
root: join(__dirname, 'public'),
prefix: '/',
})

app.get('/', (_request, reply) => {
reply.sendFile('api-reference-cdn.html', { cacheControl: false }) // overriding the options disabling cache-control headers) // serving path.join(__dirname, 'public', 'myHtml.html') directly
})

// Run the server!
try {
await app.listen({ port: 3173 })
} catch (err) {
app.log.error(err)
process.exit(1)
}
35 changes: 35 additions & 0 deletions examples/cdn-api-reference/src/public/api-reference-cdn.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!doctype html>
<html>
<head>
<title>API Reference</title>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1" />
<style>
body {
margin: 0;
}
</style>
</head>
<body>
<!-- Add your own OpenAPI/Swagger spec file URL here: -->
<!-- Note: this includes our proxy, you can remove the following line if you do not need it -->
<!-- data-proxy-url="https://api.scalar.com/request-proxy" -->
<script
id="api-reference"
data-url="https://petstore3.swagger.io/api/v3/openapi.json"
data-proxy-url="https://api.scalar.com/request-proxy"></script>
<!-- You can also set a full configuration object like this -->
<!-- easier for nested objects -->
<script>
var configuration = {
theme: 'purple',
}

var apiReference = document.getElementById('api-reference')
apiReference.dataset.configuration = JSON.stringify(configuration)
</script>
<script src="https://cdn.jsdelivr.net/npm/@scalar/api-reference"></script>
</body>
</html>
7 changes: 7 additions & 0 deletions examples/cdn-api-reference/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"types": ["vite/client"]
},
"exclude": ["node_modules", "dist"]
}
17 changes: 17 additions & 0 deletions examples/cdn-api-reference/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import path from 'path'
import { defineConfig } from 'vite'

// https://vitejs.dev/config/
export default defineConfig({
resolve: {
alias: [
{
// Resolve the uncompiled source code for all @scalar packages
// @scalar/* -> packages/*/
// (not @scalar/*/style.css)
find: /^@scalar\/(?!(snippetz|components\/style\.css|components\b))(.+)/,
replacement: path.resolve(__dirname, '../../packages/$2/src/index.ts'),
},
],
},
})
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"name": "root",
"devDependencies": {
"@changesets/cli": "^2.27.1",
"@playwright/test": "^1.42.0",
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
"@types/node": "^20.6.3",
"@typescript-eslint/eslint-plugin": "^6.21.0",
"@typescript-eslint/parser": "^6.21.0",
"@vitest/coverage-v8": "^1.2.2",
Expand Down Expand Up @@ -47,6 +49,8 @@
"lint:fix": "pnpm -r lint:fix",
"preview:standalone": "pnpm --filter api-reference preview:standalone",
"test": "vitest",
"test:e2e": "playwright test",
"test:e2e:ui": "playwright test --ui",
"types:check": "pnpm -r types:check"
}
}
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'

/**
* 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',

/* 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: 'npm run start',
// url: 'http://127.0.0.1:3000',
// reuseExistingServer: !process.env.CI,
// },
})

0 comments on commit 1f2921f

Please sign in to comment.