test: add E2E tests (#1012)

pull/1083/head
fi3ework 2 years ago committed by GitHub
parent f789932ffc
commit bc47197f7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,5 +1,12 @@
name: Test
env:
# 7 GiB by default on GitHub, setting to 6 GiB
# https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners#supported-runners-and-hardware-resources
NODE_OPTIONS: --max-old-space-size=6144
# install playwright binary manually (because pnpm only runs install script once)
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: '1'
on:
push:
branches:
@ -30,11 +37,37 @@ jobs:
- name: Install deps
run: pnpm install
# Install playwright's binary under custom directory to cache
- name: Set Playwright path (non-windows)
if: runner.os != 'Windows'
run: echo "PLAYWRIGHT_BROWSERS_PATH=$HOME/.cache/playwright-bin" >> $GITHUB_ENV
- name: Set Playwright path (windows)
if: runner.os == 'Windows'
run: echo "PLAYWRIGHT_BROWSERS_PATH=$HOME\.cache\playwright-bin" >> $env:GITHUB_ENV
- name: Cache Playwright's binary
uses: actions/cache@v3
with:
# Playwright removes unused browsers automatically
# So does not need to add playwright version to key
key: ${{ runner.os }}-playwright-bin-v1
path: ${{ env.PLAYWRIGHT_BROWSERS_PATH }}
- name: Install Playwright
# does not need to explictly set chromium after https://github.com/microsoft/playwright/issues/14862 is solved
run: pnpm playwright install chromium
- name: Build
run: pnpm run build
- name: Lint
run: pnpm run lint-fail
- name: Test
run: pnpm run test-run
- name: Test unit
run: pnpm run test-unit
- name: Test serve
run: pnpm run test-serve
- name: Test build
run: pnpm run test-build

@ -1,5 +1,4 @@
/docs
/examples
*.md
*.vue
dist

@ -0,0 +1,22 @@
import { defineConfig } from '../../../src/node'
export default defineConfig({
title: 'Configured Example',
description: 'Example of configured options of VitePress',
themeConfig: {
sidebar: {
'/': [
{
text: 'Frontmatter',
collapsible: true,
items: [
{
text: 'Multiple levels outline',
link: '/frontmatter/multiple-levels-outline'
}
]
}
]
}
}
})

@ -0,0 +1,32 @@
import { expect, test } from 'vitest'
import { page, vitePressTestUrl, waitForLayout } from '~utils'
describe('outline', () => {
beforeAll(async () => {
await page.goto(
vitePressTestUrl + '/frontmatter/multiple-levels-outline.html'
)
await waitForLayout()
})
test('set outline to deep', async () => {
const outlineLinksLocator = await page.locator(
'.VPDocAsideOutline .root .outline-link'
)
const outlineLinksContent = await outlineLinksLocator.allTextContents()
expect(outlineLinksContent).toEqual([
'h2 - 1',
'h3 - 1',
'h3 - 2',
'h2 - 2',
'h3 - 3'
])
const linkHrefs = await outlineLinksLocator.evaluateAll((element) =>
element.map((element) => element.getAttribute('href'))
)
expect(linkHrefs).toEqual(['#h2-1', '#h3-1', '#h3-2', '#h2-2', '#h3-3'])
})
})

@ -0,0 +1,41 @@
---
title: Multiple levels outline
editLink: true
outline: 'deep'
---
# h1 - 1
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
## h2 - 1
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
### h3 - 1
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
#### h4 - 1
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
### h3 - 2
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
#### h4 - 2
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
## h2 - 2
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
### h3 - 3
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
#### h4 - 3
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

@ -0,0 +1,3 @@
# Full Configured VitePress Example
WIP

@ -0,0 +1,12 @@
{
"private": true,
"name": "vitepress-example-configured",
"scripts": {
"dev": "vitepress dev",
"build": "vitepress build",
"serve": "vitepress serve"
},
"devDependencies": {
"vitepress": "workspace:*"
}
}

@ -0,0 +1,13 @@
// Vitest Snapshot v1
exports[`render corrent content > main content 1`] = `
[
"Appearance",
"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.",
"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.",
"Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of \\"de Finibus Bonorum et Malorum\\" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, \\"Lorem ipsum dolor sit amet..\\", comes from a line in section 1.10.32.",
"The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from \\"de Finibus Bonorum et Malorum\\" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.",
"It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).",
"There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.",
]
`;

@ -0,0 +1,38 @@
import { beforeAll, expect, test } from 'vitest'
import { page, waitForLayout } from '~utils'
describe('render corrent content', () => {
beforeAll(async () => {
await waitForLayout()
})
test('main content', async () => {
const h1Locator = await page.locator('h1')
const h2Locator = await page.locator('h2')
const pLocator = await page.locator('.Layout p')
const [h1Contents, h2Conetents, pContents] = await Promise.all([
h1Locator.allTextContents(),
h2Locator.allTextContents(),
pLocator.allTextContents()
])
expect(h1Contents).toEqual(['Lorem Ipsum #'])
expect(h2Conetents).toEqual([
'What is Lorem Ipsum? #',
'Where does it come from? #',
'Why do we use it? #',
'Where can I get some? #'
])
expect(pContents).toMatchSnapshot()
})
test('outline', async () => {
const outlineLinksLocator = await page.locator(
'.VPDocAsideOutline .root .outline-link'
)
const outlineLinksCount = await outlineLinksLocator.count()
expect(outlineLinksCount).toEqual(0)
})
})

@ -0,0 +1,7 @@
{
"name": "vitepress-examples",
"private": true,
"devDependencies": {
"vitepress": "workspace:*"
}
}

@ -0,0 +1,7 @@
import { page } from './vitestSetup'
export * from './vitestSetup'
export async function waitForLayout() {
await page.waitForSelector('#app .Layout')
}

@ -0,0 +1,21 @@
{
"include": ["."],
"exclude": ["**/dist/**"],
"compilerOptions": {
"target": "ES2020",
"module": "ESNext",
"outDir": "dist",
"baseUrl": ".",
"allowJs": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"moduleResolution": "Node",
"skipLibCheck": true,
"noUnusedLocals": true,
"jsx": "preserve",
"types": ["vitepress/client", "vitest/globals", "node"],
"paths": {
"~utils": ["./test-utils.ts"]
}
}
}

@ -0,0 +1,49 @@
import os from 'os'
import path from 'path'
import fs from 'fs-extra'
import type { BrowserServer } from 'playwright-chromium'
import { chromium } from 'playwright-chromium'
const DIR = path.join(os.tmpdir(), 'vitest_playwright_global_setup')
let browserServer: BrowserServer | undefined
export async function setup(): Promise<void> {
browserServer = await chromium.launchServer({
headless: !process.env.VITE_DEBUG_SERVE,
args: process.env.CI
? ['--no-sandbox', '--disable-setuid-sandbox']
: undefined
})
await fs.mkdirp(DIR)
await fs.writeFile(path.join(DIR, 'wsEndpoint'), browserServer.wsEndpoint())
const tempDir = path.resolve(__dirname, '../examples-temp')
await fs.ensureDir(tempDir)
await fs.emptyDir(tempDir)
await fs
.copy(path.resolve(__dirname, '../examples'), tempDir, {
dereference: false,
filter(file) {
file = file.replace(/\\/g, '/')
return !file.includes('__tests__') && !file.match(/dist(\/|$)/)
}
})
.catch(async (error) => {
if (error.code === 'EPERM' && error.syscall === 'symlink') {
throw new Error(
'Could not create symlinks. On Windows, consider activating Developer Mode to allow non-admin users to create symlinks by following the instructions at https://docs.microsoft.com/en-us/windows/apps/get-started/enable-your-device-for-development.'
)
} else {
throw error
}
})
}
export async function teardown(): Promise<void> {
browserServer?.close()
if (!process.env.VITE_PRESERVE_BUILD_ARTIFACTS) {
fs.removeSync(path.resolve(__dirname, '../examples-temp'))
}
}

@ -0,0 +1,233 @@
import fs from 'fs-extra'
import * as http from 'http'
import os from 'os'
import path, { dirname, join, resolve } from 'path'
import type { Browser, Page } from 'playwright-chromium'
import { chromium } from 'playwright-chromium'
import type { RollupWatcher } from 'rollup'
import sirv from 'sirv'
import type { ResolvedConfig, ServerOptions, ViteDevServer } from 'vite'
import { build, createServer } from 'vitepress'
import type { File } from 'vitest'
import { beforeAll } from 'vitest'
type VitePressBuildOptions = Parameters<typeof build>[1]
// #region env
export const workspaceRoot = resolve(__dirname, '../')
export const isBuild = !!process.env.VITE_TEST_BUILD
export const isServe = !isBuild
export const isWindows = process.platform === 'win32'
export const vitePressBinPath = path.posix.join(
workspaceRoot,
'bin/vitepress.js'
)
// #endregion
// #region context
let server: ViteDevServer | http.Server
/**
* Vite Dev Server when testing serve
*/
export let viteServer: ViteDevServer
/**
* Root of the VitePress fixture
*/
export let rootDir: string
/**
* Path to the current test file
*/
export let testPath: string
/**
* Path to the test folder
*/
export let testDir: string
/**
* Test folder name
*/
export let testName: string
export const serverLogs: string[] = []
export const browserLogs: string[] = []
export const browserErrors: Error[] = []
export let resolvedConfig: ResolvedConfig = undefined!
export let page: Page = undefined!
export let browser: Browser = undefined!
export let vitePressTestUrl: string = ''
export let watcher: RollupWatcher | undefined = undefined
declare module 'vite' {
interface InlineConfig {
testConfig?: {
// relative base output use relative path
// rewrite the url to truth file path
baseRoute: string
}
}
}
export function setViteUrl(url: string): void {
vitePressTestUrl = url
}
// #endregion
const DIR = join(os.tmpdir(), 'vitest_playwright_global_setup')
beforeAll(async (s) => {
const suite = s as File
// skip browser setup for non-examples tests
if (!suite.filepath.includes('examples')) {
return
}
const wsEndpoint = fs.readFileSync(join(DIR, 'wsEndpoint'), 'utf-8')
if (!wsEndpoint) {
throw new Error('wsEndpoint not found')
}
browser = await chromium.connect(wsEndpoint)
page = await browser.newPage()
const globalConsole = global.console
const warn = globalConsole.warn
globalConsole.warn = (msg, ...args) => {
// suppress @vue/reactivity-transform warning
if (msg.includes('@vue/reactivity-transform')) return
if (msg.includes('Generated an empty chunk')) return
warn.call(globalConsole, msg, ...args)
}
try {
page.on('console', (msg) => {
browserLogs.push(msg.text())
})
page.on('pageerror', (error) => {
browserErrors.push(error)
})
testPath = suite.filepath!
testName = slash(testPath).match(/examples\/([\w-]+)\//)?.[1]
testDir = dirname(testPath)
// if this is a test placed under examples/xxx/__tests__
// start a vite server in that directory.
if (testName) {
testDir = resolve(workspaceRoot, 'examples-temp', testName)
// when `root` dir is present, use it as vite's root
const testCustomRoot = resolve(testDir, 'root')
rootDir = fs.existsSync(testCustomRoot) ? testCustomRoot : testDir
await startDefaultServe()
}
} catch (e) {
// Closing the page since an error in the setup, for example a runtime error
// when building the examples should skip further tests.
// If the page remains open, a command like `await page.click(...)` produces
// a timeout with an exception that hides the real error in the console.
await page.close()
await server?.close()
throw e
}
return async () => {
serverLogs.length = 0
await page?.close()
await server?.close()
watcher?.close()
if (browser) {
await browser.close()
}
}
})
export async function startDefaultServe(): Promise<void> {
const options: ServerOptions = undefined
setupConsoleWarnCollector(serverLogs)
if (!isBuild) {
viteServer = server = await (await createServer(rootDir, options)).listen()
const devBase = server.config.base
vitePressTestUrl = `http://localhost:${server.config.server.port}${
devBase === '/' ? '' : devBase
}`
await page.goto(vitePressTestUrl)
// TODO: A manual reload is needed bacause the first load of page will crash
// because of multiple vue instances. (see https://github.com/vuejs/vitepress/issues/1016)
// Try to remove this after migrating to Vite3.
if (isServe) {
await page.reload()
}
} else {
const options: VitePressBuildOptions = {}
await build(rootDir, options)
vitePressTestUrl = await startStaticServer()
await page.goto(vitePressTestUrl)
}
}
function startStaticServer(config?: VitePressBuildOptions): Promise<string> {
if (!config) {
// check if the test project has base config
const configFile = resolve(rootDir, '.vitepress/config.ts')
try {
config = require(configFile)
} catch (e) {}
}
// fallback internal base to ''
let base = config?.base
if (!base || base === '/' || base === './') {
base = ''
}
// start static file server
const serve = sirv(resolve(rootDir, '.vitepress/dist'))
const baseDir = config?.base
const httpServer = (server = http.createServer((req, res) => {
if (req.url === '/ping') {
res.statusCode = 200
res.end('pong')
} else {
if (baseDir) {
req.url = path.posix.join(baseDir, req.url)
}
serve(req, res)
}
}))
let port = 4173
return new Promise((resolve, reject) => {
const onError = (e: any) => {
if (e.code === 'EADDRINUSE') {
httpServer.close()
httpServer.listen(++port)
} else {
reject(e)
}
}
httpServer.on('error', onError)
httpServer.listen(port, () => {
httpServer.removeListener('error', onError)
resolve(`http://localhost:${port}${base}`)
})
})
}
function setupConsoleWarnCollector(logs: string[]) {
const warn = console.warn
console.warn = (...args) => {
serverLogs.push(args.join(' '))
return warn.call(console, ...args)
}
}
export function slash(p: string): string {
return p.replace(/\\/g, '/')
}

@ -58,7 +58,11 @@
"lint": "pnpm run format",
"lint-fail": "pnpm run format-fail",
"test": "vitest -r __tests__",
"test-run": "vitest run -r __tests__",
"test-unit": "vitest run -r __tests__",
"test-serve": "vitest run -c vitest.config.e2e.ts",
"test-build": "VITE_TEST_BUILD=1 vitest run -c vitest.config.e2e.ts",
"debug-serve": "VITE_DEBUG_SERVE=1 vitest run -c vitest.config.e2e.ts",
"debug-build": "VITE_TEST_BUILD=1 VITE_PRESERVE_BUILD_ARTIFACTS=1 vitest run -c vitest.config.e2e.ts",
"check": "run-s lint test-run docs-build",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
"release": "node scripts/release.js",
@ -127,6 +131,7 @@
"npm-run-all": "^4.1.5",
"ora": "^5.4.0",
"picocolors": "^1.0.0",
"playwright-chromium": "^1.23.4",
"polka": "^0.5.2",
"prettier": "^2.3.0",
"prompts": "^2.4.2",

@ -55,6 +55,7 @@ importers:
npm-run-all: ^4.1.5
ora: ^5.4.0
picocolors: ^1.0.0
playwright-chromium: ^1.23.4
polka: ^0.5.2
prettier: ^2.3.0
prompts: ^2.4.2
@ -127,6 +128,7 @@ importers:
npm-run-all: 4.1.5
ora: 5.4.1
picocolors: 1.0.0
playwright-chromium: 1.23.4
polka: 0.5.2
prettier: 2.4.1
prompts: 2.4.2
@ -144,6 +146,18 @@ importers:
docs:
specifiers: {}
examples:
specifiers:
vitepress: workspace:*
devDependencies:
vitepress: link:..
examples/configured:
specifiers:
vitepress: workspace:*
devDependencies:
vitepress: link:../..
examples/minimal:
specifiers:
vitepress: workspace:*
@ -3257,6 +3271,21 @@ packages:
engines: {node: '>=4'}
dev: true
/playwright-chromium/1.23.4:
resolution: {integrity: sha512-aqiOpX76107knxZqydyd0snNHiU6wgGwmQUFA8MEbG6sVjvjF8fS8Uy6n39z9U52pSpRFGPBP8dfkTkXBokkqQ==}
engines: {node: '>=14'}
hasBin: true
requiresBuild: true
dependencies:
playwright-core: 1.23.4
dev: true
/playwright-core/1.23.4:
resolution: {integrity: sha512-h5V2yw7d8xIwotjyNrkLF13nV9RiiZLHdXeHo+nVJIYGVlZ8U2qV0pMxNJKNTvfQVT0N8/A4CW6/4EW2cOcTiA==}
engines: {node: '>=14'}
hasBin: true
dev: true
/please-upgrade-node/3.2.0:
resolution: {integrity: sha512-gQR3WpIgNIKwBMVLkpMUeR3e1/E1y42bqDQZfql+kDeXd8COYfM8PQA4X6y7a8u9Ua9FHmsrrmirW2vHs45hWg==}
dependencies:

@ -1,3 +1,4 @@
packages:
- examples/*
- examples
- docs

@ -0,0 +1,28 @@
import { resolve } from 'path'
import { defineConfig } from 'vitest/config'
const timeout = process.env.CI ? 50000 : 30000
export default defineConfig({
resolve: {
alias: {
'~utils': resolve(__dirname, './examples/test-utils')
}
},
test: {
include: ['./examples/**/*.spec.[tj]s'],
setupFiles: ['./examples/vitestSetup.ts'],
globalSetup: ['./examples/vitestGlobalSetup.ts'],
testTimeout: timeout,
hookTimeout: timeout,
globals: true,
reporters: 'dot',
onConsoleLog(log) {
if (log.match(/experimental|jit engine|emitted file|tailwind/i))
return false
}
},
esbuild: {
target: 'node14'
}
})
Loading…
Cancel
Save