Skip to content

Commit

Permalink
test: add unit and e2e (#13)
Browse files Browse the repository at this point in the history
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
Barbapapazes and autofix-ci[bot] authored Jul 18, 2024
1 parent 31a4690 commit 8310014
Show file tree
Hide file tree
Showing 22 changed files with 2,313 additions and 37 deletions.
47 changes: 30 additions & 17 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,36 @@ jobs:
- name: Lint files
run: pnpm run lint

# test:
# name: Test
# runs-on: ubuntu-latest

# steps:
# - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4
# - run: corepack enable
# - uses: actions/setup-node@8f152de45cc393bb48ce5d89d36b731f54556e65 # v4
# with:
# node-version: 20.5
# cache: pnpm

# - name: Install dependencies
# run: pnpm install

# - name: Test files
# run: pnpm run test
test:
name: Test
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4
- run: corepack enable
- uses: actions/setup-node@8f152de45cc393bb48ce5d89d36b731f54556e65 # v4
with:
node-version: 20.5
cache: pnpm

- name: Install dependencies
run: pnpm install

- name: Unit tests
run: pnpm run test --coverage

- name: Install Playwright Browsers
run: pnpm exec playwright install --with-deps

- name: Playwright tests
run: pnpm exec playwright test

- uses: actions/upload-artifact@v4
if: always()
with:
name: playwright-report
path: playwright-report/
retention-days: 30

build:
name: Build
Expand Down
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
.nyc_output
build
node_modules
test
src/**.js
coverage
*.log
dist
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,19 @@
"lint:fix": "eslint . --fix",
"release": "bumpp",
"test": "vitest",
"test:e2e": "playwright test",
"dev": "pnpm run --filter playground dev",
"prepublishOnly": "pnpm run build"
},
"devDependencies": {
"@antfu/eslint-config": "^2.16.0",
"@playwright/test": "^1.45.1",
"@types/node": "^18.19.31",
"@vitest/coverage-v8": "^1.5.2",
"bumpp": "^9.4.0",
"eslint": "^8.57.0",
"happy-dom": "^13.10.1",
"jsdom": "^24.1.0",
"typescript": "^5.4.5",
"unbuild": "^2.0.0",
"vitest": "^1.5.2"
Expand Down
27 changes: 27 additions & 0 deletions playground/auto-outbound-tracking.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>
Auto Outbound Tracking
</title>
</head>
<body>
<!--
This playground page is meant to be used by the end-to-end tests. Please do not remove any of the following elements.
-->

<a href="https://example.com">External Link</a>

<a href="/">Internal Link</a>

<a href="https://example.com" rel="noopener noreferrer">External Link (noopener noreferrer)</a>

<a href="https://example.com" target="_blank">External Link (_blank)</a>

<button id="cleanup">Cleanup</button>

<script type="module" src="/src/auto-outbound-tracking.ts"></script>
</body>
</html>
28 changes: 28 additions & 0 deletions playground/file-downloads-tracking.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>
File Downloads Tracking
</title>
</head>
<body>
<!--
This playground page is meant to be used by the end-to-end tests. Please do not remove any of the following elements.
-->

<a download href="/file.svg">Download SVG</a>
<a href="/file.svg">Download SVG without download attribute</a>
<a href="/file.svg" target="_blank">Download SVG targeting blank</a>
<a href="https://localhost:5173/file.svg">Download SVG with full URL</a>

<a download href="/document.pdf">Download PDF</a>

<a href="/">Simple link</a>

<button id="cleanup">Cleanup</button>

<script type="module" src="/src/file-downloads-tracking.ts"></script>
</body>
</html>
14 changes: 14 additions & 0 deletions playground/src/auto-outbound-tracking.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { createPlausibleTracker } from '@barbapapazes/plausible-tracker'
import { useAutoOutboundTracking } from '@barbapapazes/plausible-tracker/extensions/auto-outbound-tracking'

const plausible = createPlausibleTracker({
ignoredHostnames: [],
})

const autoOutboundTracking = useAutoOutboundTracking(plausible)

autoOutboundTracking.install()

document.getElementById('cleanup')!.addEventListener('click', () => {
autoOutboundTracking.cleanup()
})
16 changes: 16 additions & 0 deletions playground/src/file-downloads-tracking.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { createPlausibleTracker } from '@barbapapazes/plausible-tracker'
import { useAutoFileDownloadsTracking } from '@barbapapazes/plausible-tracker/extensions/file-downloads-tracking'

const plausible = createPlausibleTracker({
ignoredHostnames: [],
})

const fileDownloadsTracking = useAutoFileDownloadsTracking(plausible, {
fileTypes: ['svg'],
})

fileDownloadsTracking.install()

document.getElementById('cleanup')!.addEventListener('click', () => {
fileDownloadsTracking.cleanup()
})
80 changes: 80 additions & 0 deletions playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import process from 'node:process'
import { defineConfig, devices } from '@playwright/test'

/**
* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
// import dotenv from 'dotenv';
// dotenv.config({ path: path.resolve(__dirname, '.env') });

/**
* See https://playwright.dev/docs/test-configuration.
*/
export default defineConfig({
testDir: './test/e2e',
/* 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',
timeout: 5000,
/* 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://localhost:5173',

/* 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 dev',
url: 'http://localhost:5173',
reuseExistingServer: !process.env.CI,
},
})
Loading

0 comments on commit 8310014

Please sign in to comment.