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

refactor tests #1587

Merged
merged 6 commits into from Nov 8, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 13 additions & 38 deletions .github/workflows/test.yml
@@ -1,25 +1,29 @@
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'
VITEST_SEGFAULT_RETRY: 3

on:
push:
branches:
- main
branches: [main]
pull_request:
branches: [main]
types: [opened, synchronize, reopened]
workflow_dispatch:

concurrency:
group: ${{ github.workflow }}-${{ github.event.number || github.sha }}
cancel-in-progress: true

jobs:
test:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [14, 16, 18]
node_version: [14, 16, 18]

steps:
- name: Checkout
Expand All @@ -32,42 +36,13 @@ jobs:
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node_version }}
cache: 'pnpm'
cache: pnpm

- 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 explicitly 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 unit
run: pnpm run test-unit

- name: Test serve
run: pnpm run test-serve

- name: Test build
run: pnpm run test-build
- name: Check
run: pnpm check
13 changes: 0 additions & 13 deletions __tests__/client/theme-default/support/utils.spec.ts

This file was deleted.

66 changes: 66 additions & 0 deletions __tests__/e2e/.vitepress/config.ts
@@ -0,0 +1,66 @@
import { defineConfig, type DefaultTheme } from 'vitepress'

const sidebar: DefaultTheme.Config['sidebar'] = {
'/': [
{
text: 'Frontmatter',
collapsible: true,
items: [
{
text: 'Multiple Levels Outline',
link: '/frontmatter/multiple-levels-outline'
}
]
},
{
text: '& &#60;Text Literals &> <code>code</code>',
items: [
{
text: '& &#60;Test Page &> <code>code</code>',
link: '/text-literals/'
}
]
},
{
text: 'Static Data',
items: [
{
text: 'Test Page',
link: '/static-data/data'
}
]
},
{
text: 'Multi Sidebar Test',
items: [
{
text: 'Test Page',
link: '/multi-sidebar/'
}
]
}
],
'/multi-sidebar/': [
{
text: 'Multi Sidebar',
items: [
{
text: 'Test Page',
link: '/multi-sidebar/'
},
{
text: 'Back',
link: '/'
}
]
}
]
}

export default defineConfig({
title: 'Example',
description: 'An example app using VitePress.',
themeConfig: {
sidebar
}
})
@@ -1,8 +1,7 @@
// Vitest Snapshot v1

exports[`render corrent content > main content 1`] = `
exports[`render correct 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.",
Expand Down
41 changes: 41 additions & 0 deletions __tests__/e2e/frontmatter/multiple-levels-outline.md
@@ -0,0 +1,41 @@
---
title: Multiple Levels Outline
editLink: true
outline: 'deep'
---

# h1 - 1

Lorem ipsum

## h2 - 1

Lorem ipsum

### h3 - 1

Lorem ipsum

#### h4 - 1

Lorem ipsum

### h3 - 2

Lorem ipsum

#### h4 - 2

Lorem ipsum

## h2 - 2

Lorem ipsum

### h3 - 3

Lorem ipsum

#### h4 - 3

Lorem ipsum
@@ -1,18 +1,10 @@
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()
await goto('/frontmatter/multiple-levels-outline')
})

test('set outline to deep', async () => {
const outlineLinksLocator = await page.locator(
'.VPDocAsideOutline .root .outline-link'
)
const outlineLinksLocator = page.locator('.VPDocAsideOutline .outline-link')

const outlineLinksContent = await outlineLinksLocator.allTextContents()
expect(outlineLinksContent).toEqual([
Expand Down
File renamed without changes.
@@ -1,24 +1,21 @@
import { beforeAll, expect, test } from 'vitest'
import { page, waitForLayout } from '~utils'

describe('render corrent content', () => {
describe('render correct content', async () => {
beforeAll(async () => {
await waitForLayout()
await goto('/')
})

test('main content', async () => {
const h1Locator = page.locator('h1')
const h2Locator = page.locator('h2')
const pLocator = page.locator('.Layout p')
const h1Locator = page.locator('.VPContent h1')
const h2Locator = page.locator('.VPContent h2')
const pLocator = page.locator('.VPContent p')

const [h1Contents, h2Conetents, pContents] = await Promise.all([
const [h1Contents, h2Contents, pContents] = await Promise.all([
h1Locator.allTextContents(),
h2Locator.allTextContents(),
pLocator.allTextContents()
])

expect(h1Contents).toEqual(['Lorem Ipsum #'])
expect(h2Conetents).toEqual([
expect(h2Contents).toEqual([
'What is Lorem Ipsum? #',
'Where does it come from? #',
'Why do we use it? #',
Expand Down
1 change: 1 addition & 0 deletions __tests__/e2e/multi-sidebar/index.md
@@ -0,0 +1 @@
# Multi Sidebar
30 changes: 30 additions & 0 deletions __tests__/e2e/multi-sidebar/index.test.ts
@@ -0,0 +1,30 @@
describe('test multi sidebar sort root', () => {
beforeAll(async () => {
await goto('/frontmatter/multiple-levels-outline')
})

test('using / sidebar', async () => {
const sidebarLocator = page.locator('.VPSidebarGroup .title-text')

const sidebarContent = await sidebarLocator.allTextContents()
expect(sidebarContent).toEqual([
'Frontmatter',
'& <Text Literals &> code',
'Static Data',
'Multi Sidebar Test'
])
})
})

describe('test multi sidebar sort order', () => {
beforeAll(async () => {
await goto('/multi-sidebar/')
})

test('using /multi-sidebar/ sidebar', async () => {
const sidebarLocator = page.locator('.VPSidebarGroup .title-text')

const sidebarContent = await sidebarLocator.allTextContents()
expect(sidebarContent).toEqual(['Multi Sidebar'])
})
})
1 change: 0 additions & 1 deletion examples/package.json → __tests__/e2e/package.json
@@ -1,5 +1,4 @@
{
"name": "vitepress-examples",
"private": true,
"devDependencies": {
"vitepress": "workspace:*"
Expand Down
6 changes: 6 additions & 0 deletions __tests__/e2e/shims.ts
@@ -0,0 +1,6 @@
import { type Page } from 'playwright-chromium'

declare global {
var page: Page
var goto: (path: string) => Promise<void>
}
14 changes: 14 additions & 0 deletions __tests__/e2e/static-data/__snapshots__/data.test.ts.snap
@@ -0,0 +1,14 @@
// Vitest Snapshot v1

exports[`static data file support in vite 3 > render correct content 1`] = `
[
"[
{
\\"foo\\": true
},
{
\\"bar\\": true
}
]",
]
`;
@@ -1,4 +1,6 @@
<script setup>
# Static Data

<script setup lang="ts">
import { data } from './static.data.js'
</script>

Expand Down
12 changes: 12 additions & 0 deletions __tests__/e2e/static-data/data.test.ts
@@ -0,0 +1,12 @@
describe('static data file support in vite 3', () => {
beforeAll(async () => {
await goto('/static-data/data')
})

test('render correct content', async () => {
const pLocator = page.locator('.VPContent p')

const pContents = await pLocator.allTextContents()
expect(pContents).toMatchSnapshot()
})
})
Expand Up @@ -4,9 +4,12 @@ import { fileURLToPath } from 'url'

const dirname = path.dirname(fileURLToPath(import.meta.url))

type Data = Record<string, boolean>[]
export declare const data: Data

export default {
watch: ['./data/*'],
async load() {
async load(): Promise<Data> {
const foo = fs.readFileSync(
path.resolve(dirname, './data/foo.json'),
'utf-8'
Expand Down
1 change: 1 addition & 0 deletions __tests__/e2e/text-literals/index.md
@@ -0,0 +1 @@
# Text Literals
14 changes: 14 additions & 0 deletions __tests__/e2e/vitest.config.ts
@@ -0,0 +1,14 @@
import { defineConfig } from 'vitest/config'

const timeout = 60_000

export default defineConfig({
test: {
setupFiles: ['vitestSetup.ts'],
globalSetup: ['__tests__/e2e/vitestGlobalSetup.ts'],
testTimeout: timeout,
hookTimeout: timeout,
teardownTimeout: timeout,
globals: true
}
})