Skip to content

Commit

Permalink
test: add E2E tests (#1012)
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Jul 26, 2022
1 parent f789932 commit bc47197
Show file tree
Hide file tree
Showing 18 changed files with 577 additions and 4 deletions.
37 changes: 35 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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 change: 0 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/docs
/examples
*.md
*.vue
dist
Expand Down
22 changes: 22 additions & 0 deletions examples/configured/.vitepress/config.js
Original file line number Diff line number Diff line change
@@ -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'
}
]
}
]
}
}
})
32 changes: 32 additions & 0 deletions examples/configured/__test__/outline.spec.ts
Original file line number Diff line number Diff line change
@@ -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'])
})
})
41 changes: 41 additions & 0 deletions examples/configured/frontmatter/multiple-levels-outline.md
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 3 additions & 0 deletions examples/configured/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Full Configured VitePress Example

WIP
12 changes: 12 additions & 0 deletions examples/configured/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"private": true,
"name": "vitepress-example-configured",
"scripts": {
"dev": "vitepress dev",
"build": "vitepress build",
"serve": "vitepress serve"
},
"devDependencies": {
"vitepress": "workspace:*"
}
}
13 changes: 13 additions & 0 deletions examples/minimal/__test__/__snapshots__/content.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -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.",
]
`;
38 changes: 38 additions & 0 deletions examples/minimal/__test__/content.spec.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
7 changes: 7 additions & 0 deletions examples/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "vitepress-examples",
"private": true,
"devDependencies": {
"vitepress": "workspace:*"
}
}
7 changes: 7 additions & 0 deletions examples/test-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { page } from './vitestSetup'

export * from './vitestSetup'

export async function waitForLayout() {
await page.waitForSelector('#app .Layout')
}
21 changes: 21 additions & 0 deletions examples/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"]
}
}
}
49 changes: 49 additions & 0 deletions examples/vitestGlobalSetup.ts
Original file line number Diff line number Diff line change
@@ -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'))
}
}
Loading

0 comments on commit bc47197

Please sign in to comment.