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

chore: add basic e2e test #8

Merged
merged 1 commit into from
Nov 5, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ jobs:
${{ runner.os }}-pnpm-store-

- name: Install dependencies
run: pnpm install
run: |
pnpm i
pnpm playwright install chromium

- name: Run CI
run: pnpm run ci
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
/examples/*/src/.tnf
/examples/*/dist
/examples/*/node_modules
# Playwright
/test-results/
/playwright-report/
/playwright/.cache/
39 changes: 39 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Contributing

## Setup

```bash
# Clone the repository
$ git clone [email protected]:umijs/tnf.git
$ cd tnf
# Install dependencies
$ pnpm install
# Install Playwright browsers
$ pnpm playwright install chromium
```

## Development

```bash
# Start the development server
$ pnpm dev
```

## Build

```bash
# Build the project
$ pnpm build
```

## Test

```bash
# Run all tests in watch mode
$ pnpm test
# Run all tests
$ pnpm test --run
# Run e2e tests
$ pnpm test:e2e
```

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
"scripts": {
"build": "father build",
"check": "prettier --check .",
"ci": "npm run check && npm run build && npm run test --run && father doctor",
"ci": "npm run check && npm run build && npm run test --run && npm run test:e2e && father doctor",
"dev": "father dev",
"format": "prettier --write .",
"release": "tsx scripts/release.ts",
"test": "vitest"
"test": "vitest",
"test:e2e": "tsx scripts/e2e.ts"
},
"bin": {
"tnf": "bin/tnf.js"
Expand Down Expand Up @@ -53,13 +54,15 @@
"yargs-parser": "^21.1.1"
},
"devDependencies": {
"@playwright/test": "^1.42.1",
"@total-typescript/tsconfig": "^1.0.4",
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
"father": "^4.5.1",
"git-repo-info": "^2.1.1",
"husky": "^9.1.6",
"lint-staged": "^15.2.10",
"prettier": "^3.3.3",
"serve-handler": "^6.1.6",
"tsx": "^4.19.2",
"typescript": "^5.6.3",
"vitest": "^2.1.3",
Expand Down
13 changes: 13 additions & 0 deletions playwright.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { defineConfig } from '@playwright/test';

export default defineConfig({
testDir: './e2e',
use: {
baseURL: 'http://localhost:3000',
},
webServer: {
command: 'pnpm run serve',
port: 3000,
reuseExistingServer: !process.env.CI,
},
});
85 changes: 85 additions & 0 deletions pnpm-lock.yaml

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

47 changes: 47 additions & 0 deletions scripts/e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { chromium } from '@playwright/test';
import { exec } from 'child_process';
import http from 'http';
import path from 'path';
import handler from 'serve-handler';
import { promisify } from 'util';

const execAsync = promisify(exec);

async function runE2E() {
console.log('Building example project...');
await execAsync('pnpm run build', {
cwd: path.join(process.cwd(), 'examples/normal'),
});

const server = http.createServer((req, res) => {
handler(req, res, {
public: path.join(process.cwd(), 'examples/normal/dist'),
});
});
const port = 3000;
server.listen(port);
console.log(`Server running at http://localhost:${port}`);

try {
const browser = await chromium.launch();
const page = await browser.newPage();
await page.goto(`http://localhost:${port}`);
const content = await page.textContent('body');
if (!content) {
throw new Error('Page content not found');
}

console.log('E2E tests passed!');
process.exit(0);
} catch (error) {
console.error('E2E tests failed:', error);
process.exit(1);
} finally {
server.close();
}
}

runE2E().catch((error) => {
console.error('E2E test script failed:', error);
process.exit(1);
});
Loading