Skip to content

Commit

Permalink
adds e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jonfriesen committed Aug 24, 2024
1 parent c545e2e commit 05e91fc
Show file tree
Hide file tree
Showing 11 changed files with 364 additions and 37 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/nightly-e2e.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Nightly E2E Tests

on:
schedule:
- cron: '0 2 * * *' # Runs at 2 AM UTC every day
workflow_dispatch: # Allows manual triggering

jobs:
e2e-tests:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: '20'

- name: Install dependencies
run: npm ci

- name: Install Playwright browsers
run: npx playwright install --with-deps

- name: Run E2E tests
run: npm run e2e

- name: Upload test results
if: always()
uses: actions/upload-artifact@v3
with:
name: playwright-report
path: playwright-report/
retention-days: 5
39 changes: 39 additions & 0 deletions .github/workflows/pull-request.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: PR Tests

on:
pull_request:
branches: [main]

jobs:
tests:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: '20'

- name: Install dependencies
run: npm ci

- name: Run unit tests
run: npm run test

- name: Install Playwright browsers
run: npx playwright install --with-deps

- name: Run E2E tests
run: npm run e2e

- name: Upload test results
if: always()
uses: actions/upload-artifact@v3
with:
name: test-reports
path: |
playwright-report/
coverage/
retention-days: 5
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dist.crx
# Build files
dist/
build/
test-results/

# Thumbnails
._*
Expand Down
60 changes: 60 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"build": "vite build",
"release": "bash scripts/bump_version.sh",
"release_verify": "bash scripts/prerelease_check.sh",
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --testPathIgnorePatterns=\\.e2e\\.test\\.js$",
"e2e": "playwright test"
},
"keywords": [
"github",
Expand All @@ -33,6 +34,7 @@
"homepage": "https://quickcite.link",
"devDependencies": {
"@crxjs/vite-plugin": "^1.0.14",
"@playwright/test": "^1.46.1",
"autoprefixer": "^10.4.20",
"concurrently": "^8.2.2",
"jest": "^29.7.0",
Expand Down
5 changes: 5 additions & 0 deletions playwright.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { defineConfig } from '@playwright/test'

export default defineConfig({
testMatch: /.*\.e2e\.js/,
})
66 changes: 66 additions & 0 deletions src/content/config.e2e.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { test as base, expect } from '@playwright/test'
import siteConfigs from './config.js'

// Define a common user agent
const COMMON_USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'

// Create a custom test fixture that sets the user agent
const test = base.extend({
page: async ({ page }, use) => {
await page.setExtraHTTPHeaders({
'User-Agent': COMMON_USER_AGENT,
})
await use(page)
},
})

// Helper function to test a specific page configuration
async function testPageConfig(page, url, config) {
await page.goto(url)

// Execute the getInfo function in the browser context
const info = await page.evaluate(config.getInfo)

// Verify that the info object is not null or undefined
expect(info).toBeTruthy()

// Verify that all expected properties are present and non-empty
for (const key in info) {
expect(info[key]).toBeTruthy()
}

// Test markdown and plaintext building functions
const markdown = config.buildMarkdown(info, url)
const plaintext = config.buildPlaintext(info, url)

expect(markdown).toBeTruthy()
expect(plaintext).toBeTruthy()
}

// Test cases for each site and page type
for (const [siteName, siteConfig] of Object.entries(siteConfigs)) {
// LinkedIn is hard to test because it requires auth
if (siteName === 'linkedin') continue

for (const [pageName, pageConfig] of Object.entries(siteConfig.pages)) {
test(`${siteName} - ${pageName}`, async ({ page }) => {
const testUrls = {
github: {
pr: 'https://github.com/microsoft/playwright/pull/20635',
issue: 'https://github.com/microsoft/playwright/issues/20636',
discussion: 'https://github.com/golang/go/discussions/61669',
repo: 'https://github.com/microsoft/playwright',
user: 'https://github.com/microsoft',
release: 'https://github.com/microsoft/playwright/releases/tag/v1.32.3',
commit: 'https://github.com/golang/go/commit/96d8ff00c2d6a88384863a656fb5e53716b614d3',
},
instagram: {
profile: 'https://www.instagram.com/jonfriesen/',
},
}

const testUrl = testUrls[siteName][pageName]
await testPageConfig(page, testUrl, pageConfig)
})
}
}
75 changes: 75 additions & 0 deletions src/content/github.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import siteConfigs from './config'

describe('GitHub URL pattern tests', () => {
const testCases = [
{ page: 'pr', url: 'https://github.com/user/repo/pull/123', shouldMatch: true },
{ page: 'issue', url: 'https://github.com/user/repo/issues/456', shouldMatch: true },
{ page: 'discussion', url: 'https://github.com/user/repo/discussions/789', shouldMatch: true },
{ page: 'repo', url: 'https://github.com/user/repo', shouldMatch: true },
{ page: 'user', url: 'https://github.com/username', shouldMatch: true },
{ page: 'release', url: 'https://github.com/user/repo/releases/tag/v1.0.0', shouldMatch: true },
{ page: 'commit', url: 'https://github.com/user/repo/commit/1234567890123456789012345678901234567890', shouldMatch: true },
{ page: 'pr', url: 'https://github.com/user/repo', shouldMatch: false },
]

test.each(testCases)('$page: $url should ${shouldMatch ? "match" : "not match"}', ({ page, url, shouldMatch }) => {
const pattern = siteConfigs.github.pages[page].urlPattern
if (shouldMatch) {
expect(url).toMatch(pattern)
} else {
expect(url).not.toMatch(pattern)
}
})
})

describe('GitHub markdown generator tests', () => {
const markdownTestCases = [
{
page: 'pr',
info: { number: '#123', title: 'Add new feature' },
url: 'https://github.com/user/repo/pull/123',
expected: '[#123: Add new feature](https://github.com/user/repo/pull/123)',
},
{
page: 'issue',
info: { number: '#456', title: 'Fix bug in login' },
url: 'https://github.com/user/repo/issues/456',
expected: '[#456: Fix bug in login](https://github.com/user/repo/issues/456)',
},
{
page: 'discussion',
info: { number: '#789', title: 'Propose new architecture' },
url: 'https://github.com/user/repo/discussions/789',
expected: '[#789: Propose new architecture](https://github.com/user/repo/discussions/789)',
},
{
page: 'repo',
info: { title: 'user/repo', description: 'A cool project' },
url: 'https://github.com/user/repo',
expected: '[user/repo](https://github.com/user/repo) - A cool project',
},
{
page: 'user',
info: { name: 'John Doe', bio: 'Software Developer' },
url: 'https://github.com/johndoe',
expected: '[John Doe](https://github.com/johndoe) - Software Developer',
},
{
page: 'release',
info: { title: 'v1.0.0: First Release', date: '2023-05-01T00:00:00Z' },
url: 'https://github.com/user/repo/releases/tag/v1.0.0',
expected: '[v1.0.0: First Release](https://github.com/user/repo/releases/tag/v1.0.0) - Released on ' + new Date('2023-05-01T00:00:00Z').toLocaleDateString(),
},
{
page: 'commit',
info: { title: 'Update README.md', hash: '1234567890abcdef', author: 'John Doe', date: '2023-05-01T00:00:00Z' },
url: 'https://github.com/user/repo/commit/1234567890abcdef',
expected: '[(1234567) - Update README.md](https://github.com/user/repo/commit/1234567890abcdef)\nBy John Doe on ' + new Date('2023-05-01T00:00:00Z').toLocaleString(),
},
]

test.each(markdownTestCases)('$page: should generate correct markdown', ({ page, info, url, expected }) => {
const buildMarkdown = siteConfigs.github.pages[page].buildMarkdown
expect(buildMarkdown(info, url)).toBe(expected)
})
})
33 changes: 33 additions & 0 deletions src/content/instagram.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import siteConfigs from './config'

describe('Instagram URL pattern tests', () => {
const testCases = [
{ page: 'profile', url: 'https://www.instagram.com/username/', shouldMatch: true },
{ page: 'profile', url: 'https://www.instagram.com/p/123456/', shouldMatch: false },
]

test.each(testCases)('$page: $url should ${shouldMatch ? "match" : "not match"}', ({ page, url, shouldMatch }) => {
const pattern = siteConfigs.instagram.pages[page].urlPattern
if (shouldMatch) {
expect(url).toMatch(pattern)
} else {
expect(url).not.toMatch(pattern)
}
})
})

describe('Instagram markdown generator tests', () => {
const markdownTestCases = [
{
page: 'profile',
info: { name: 'Travel Enthusiast' },
url: 'https://www.instagram.com/travelenthusiast/',
expected: '[Travel Enthusiast](https://www.instagram.com/travelenthusiast/)',
},
]

test.each(markdownTestCases)('$page: should generate correct markdown', ({ page, info, url, expected }) => {
const buildMarkdown = siteConfigs.instagram.pages[page].buildMarkdown
expect(buildMarkdown(info, url)).toBe(expected)
})
})
Loading

0 comments on commit 05e91fc

Please sign in to comment.