Connect, collaborate, and grow with a community of TypeScript developers
feat: setup playwright and example tests (#685)
Bill Chengd90fd15eeeb1
diff --git a/.github/workflows/quality.yml b/.github/workflows/quality.ymlindex d1f1b98e..b1c41eae 100644--- a/.github/workflows/quality.yml+++ b/.github/workflows/quality.yml@@ -68,3 +68,46 @@ jobs:- name: Run TypeScript type checkrun: turbo typecheck --continue++ e2e:+ name: E2E+ timeout-minutes: 30+ runs-on: ubuntu-latest+ steps:+ - name: Checkout+ uses: actions/checkout@v3++ - name: Install+ uses: ./tooling/github-actions/install++ - name: Install Playwright Browsers+ run: pnpm exec playwright install chromium --with-deps++ - name: Setup database+ run: |+ cp .env.example .env+ docker compose up -d --wait+ pnpm db:push+ pnpm db:seed++ - name: Build+ run: turbo build+ env:+ GITHUB_ID: ' '+ GITHUB_SECRET: ' '++ - name: Run Playwright tests+ run: turbo test:e2e --continue+ env:+ DATABASE_URL: mysql://dev:dev@localhost/typehero+ GITHUB_ID: ' '+ GITHUB_SECRET: ' '+ NEXTAUTH_SECRET: 'kGvjABB9ZZsxhYFyxMT4ApohaIvfuNSRMIthI+usjA0='+ NEXTAUTH_URL: http://localhost:3000++ - name: Upload report+ if: always()+ uses: actions/upload-artifact@v3+ with:+ name: report+ path: apps/web/playwright-report/diff --git a/.github/workflows/report.yml b/.github/workflows/report.ymlnew file mode 100644index 00000000..981a4820--- /dev/null+++ b/.github/workflows/report.yml@@ -0,0 +1,84 @@+name: Report++on:+ workflow_run:+ workflows: ['Quality']+ types:+ - completed++jobs:+ e2e:+ name: E2E+ timeout-minutes: 10+ runs-on: ubuntu-latest+ steps:+ - name: Download artifacts+ uses: actions/github-script@v6+ with:+ script: |+ const allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({+ owner: context.repo.owner,+ repo: context.repo.repo,+ run_id: context.payload.workflow_run.id,+ });++ const reportArtifact = allArtifacts.data.artifacts.find((artifact) => {+ return artifact.name == "report"+ });+ const report = await github.rest.actions.downloadArtifact({+ owner: context.repo.owner,+ repo: context.repo.repo,+ artifact_id: reportArtifact.id,+ archive_format: 'zip',+ });++ const fs = require('fs');+ fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/report.zip`, Buffer.from(report.data));++ - name: Unzip artifacts+ run: unzip report.zip -d report++ - name: Publish report+ if: always()+ id: report+ uses: cloudflare/pages-action@v1+ with:+ apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}+ accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}+ projectName: typehero+ directory: report+ wranglerVersion: '3'++ - name: Comment on PR+ if: always() && github.event.workflow_run.event == 'pull_request'+ uses: actions/github-script@v6+ with:+ github-token: ${{ secrets.GITHUB_TOKEN }}+ script: |+ const { data: comments } = await github.rest.issues.listComments({+ owner: context.repo.owner,+ repo: context.repo.repo,+ issue_number: context.payload.workflow_run.pull_requests[0].number+ });++ const botComment = comments.find(comment => {+ return comment.user.type === 'Bot' && comment.body.includes('E2E report:')+ });++ const output = 'E2E report: ${{ steps.report.outputs.url }}';++ if (botComment) {+ github.rest.issues.updateComment({+ owner: context.repo.owner,+ repo: context.repo.repo,+ comment_id: botComment.id,+ body: output+ });+ } else {+ github.rest.issues.createComment({+ issue_number: context.payload.workflow_run.pull_requests[0].number,+ owner: context.repo.owner,+ repo: context.repo.repo,+ body: output+ });+ }diff --git a/apps/web/.gitignore b/apps/web/.gitignoreindex 19baec36..c7262c3f 100644--- a/apps/web/.gitignore+++ b/apps/web/.gitignore@@ -1,3 +1,7 @@# Sentry Auth Token.sentryclirc+# Playwright+/test-results/+/playwright-report/+/playwright/.cache/diff --git a/apps/web/package.json b/apps/web/package.jsonindex c9e15c14..d8c1934d 100644--- a/apps/web/package.json+++ b/apps/web/package.json@@ -12,6 +12,8 @@"lint": "eslint . --cache --cache-location \"node_modules/.cache/.eslintcache\" --max-warnings 0","lint:fix": "eslint . --fix --cache --cache-location \"node_modules/.cache/.eslintcache\" --max-warnings 0","start": "next start",+ "test:e2e": "playwright test",+ "test:e2e:ui": "playwright test --ui","typecheck": "tsc --noEmit"},"dependencies": {@@ -20,6 +22,7 @@"@monaco-editor/loader": "^1.3.3","@monaco-editor/react": "^4.5.1","@next/bundle-analyzer": "^13.4.19",+ "@playwright/test": "^1.38.0","@radix-ui/react-dialog": "^1.0.4","@repo/auth": "workspace:*","@repo/db": "workspace:*",diff --git a/apps/web/playwright.config.ts b/apps/web/playwright.config.tsnew file mode 100644index 00000000..f7340be0--- /dev/null+++ b/apps/web/playwright.config.ts@@ -0,0 +1,29 @@+import { defineConfig, devices } from '@playwright/test';++export default defineConfig({+ testDir: './tests/e2e',+ timeout: 15 * 1000,+ expect: {+ timeout: 5 * 1000,+ },+ fullyParallel: true,+ forbidOnly: Boolean(process.env.CI),+ retries: process.env.CI ? 2 : 0,+ workers: process.env.CI ? 1 : undefined,+ reporter: [['html', { open: 'never' }]],+ use: {+ baseURL: 'http://127.0.0.1:3000',+ trace: 'on-first-retry',+ },+ projects: [+ {+ name: 'chromium',+ use: { ...devices['Desktop Chrome'] },+ },+ ],+ webServer: {+ command: 'pnpm start',+ url: 'http://127.0.0.1:3000',+ reuseExistingServer: !process.env.CI,+ },+});diff --git a/apps/web/src/app/[locale]/challenge/_components/reset-editor-button.tsx b/apps/web/src/app/[locale]/challenge/_components/reset-editor-button.tsxindex dd77c8d8..0f5d56e6 100644--- a/apps/web/src/app/[locale]/challenge/_components/reset-editor-button.tsx+++ b/apps/web/src/app/[locale]/challenge/_components/reset-editor-button.tsx@@ -16,7 +16,7 @@ const ResetEditorButton = () => {const { dispatch } = useResetEditor();return (<AlertDialog>- <AlertDialogTrigger>+ <AlertDialogTrigger name="reset"><Tooltip><TooltipTrigger asChild><RotateCcw className="stroke-zinc-500 stroke-1 hover:stroke-zinc-400" size={20} />diff --git a/apps/web/src/app/[locale]/feature-flag-provider.tsx b/apps/web/src/app/[locale]/feature-flag-provider.tsxindex 02f82aef..cab73a23 100644--- a/apps/web/src/app/[locale]/feature-flag-provider.tsx+++ b/apps/web/src/app/[locale]/feature-flag-provider.tsx@@ -7,13 +7,6 @@ interface Props {children: ReactNode;}-const mockFlags = {- loginButton: true,- exploreButton: true,- tracksButton: true,-};--const isProd = process.env.NODE_ENV === 'production';export function FeatureFlagProvider({ children }: Props) {const { data: featureFlags } = useQuery(['featureFlags'], () => getFeatureFlags());@@ -21,9 +14,5 @@ export function FeatureFlagProvider({ children }: Props) {}async function getFeatureFlags() {- if (isProd) {- return fetch('/api/flags').then((res) => res.json());- }-- return mockFlags;+ return fetch('/api/flags').then((res) => res.json());}diff --git a/apps/web/src/utils/feature-flags.ts b/apps/web/src/utils/feature-flags.tsindex d695451d..481bca28 100644--- a/apps/web/src/utils/feature-flags.ts+++ b/apps/web/src/utils/feature-flags.ts@@ -1,6 +1,11 @@import { createClient, type EdgeConfigItems } from '@vercel/edge-config';-const edgeConfig = createClient(process.env.EDGE_CONFIG);+const mockFlags = {+ loginButton: true,+ exploreButton: true,+ tracksButton: true,+};+/*** This will try to eval a feature flag on the prod app*@@ -10,6 +15,11 @@ const edgeConfig = createClient(process.env.EDGE_CONFIG);* ```*/export async function evaluateFlag(key: string): Promise<boolean> {+ if (!process.env.EDGE_CONFIG) {+ return mockFlags[key as keyof typeof mockFlags];+ }+ const edgeConfig = createClient(process.env.EDGE_CONFIG);+const featureFlag = await edgeConfig.get(key);const rawValue = featureFlag?.valueOf();@@ -17,6 +27,8 @@ export async function evaluateFlag(key: string): Promise<boolean> {}export async function getAllFlags(): Promise<EdgeConfigItems> {- const allFeatureFlag = await edgeConfig.getAll();+ const allFeatureFlag = process.env.EDGE_CONFIG+ ? await createClient(process.env.EDGE_CONFIG).getAll()+ : mockFlags;return allFeatureFlag;}diff --git a/apps/web/tests/e2e/challenge.test.ts b/apps/web/tests/e2e/challenge.test.tsnew file mode 100644index 00000000..23403f19--- /dev/null+++ b/apps/web/tests/e2e/challenge.test.ts@@ -0,0 +1,42 @@+import { test, expect } from '@playwright/test';++test('challenge page', async ({ page }) => {+ await page.goto('/challenge/4');++ await expect(page.getByRole('heading', { name: 'Pick' })).toBeVisible();+ await expect(+ page.getByText('Implement the built-in Pick<T, K> generic without using it.'),+ ).toBeVisible();++ await page.getByRole('button', { name: 'Comments' }).click();+ await expect(page.getByRole('button', { name: 'Preview' })).toBeVisible();+ await expect(page.getByRole('button', { name: 'Comment', exact: true })).toBeDisabled();++ await page.getByRole('button', { name: 'Comments' }).click();+ await expect(page.getByText('No comments yet, be the first to comment!')).toBeVisible();++ await page.getByRole('tab', { name: 'Solutions' }).click();+ await expect(+ page.getByText('No solutions yet, complete the challenge to submit one!'),+ ).toBeVisible();++ await page.getByRole('tab', { name: 'Submissions' }).click();+ await expect(page.getByText('No submissions yet, submit the challenge!')).toBeVisible();++ await page.getByRole('button', { name: 'Tests' }).click();++ await expect(page.getByRole('button', { name: 'Submit' })).toBeDisabled();+});++test('reset challenge', async ({ page }) => {+ await page.goto('/challenge/4');++ await expect(page.getByText('type MyPick<T, K> = any')).toBeVisible();++ await page.getByLabel('Editor content').first().fill('');+ await expect(page.getByText('type MyPick<T, K> = any')).not.toBeVisible();++ await page.locator('button[name="reset"]').click();+ await page.getByRole('button', { name: 'Continue' }).click();+ await expect(page.getByText('type MyPick<T, K> = any')).toBeVisible();+});diff --git a/apps/web/tests/e2e/explore.test.ts b/apps/web/tests/e2e/explore.test.tsnew file mode 100644index 00000000..152b97bc--- /dev/null+++ b/apps/web/tests/e2e/explore.test.ts@@ -0,0 +1,7 @@+import { test, expect } from '@playwright/test';++test('explore page', async ({ page }) => {+ await page.goto('/explore');++ await expect(page.getByRole('heading', { name: 'TypeHero Explore' })).toBeVisible();+});diff --git a/apps/web/tests/e2e/home.test.ts b/apps/web/tests/e2e/home.test.tsnew file mode 100644index 00000000..03d3205c--- /dev/null+++ b/apps/web/tests/e2e/home.test.ts@@ -0,0 +1,7 @@+import { test, expect } from '@playwright/test';++test('home page', async ({ page }) => {+ await page.goto('/');++ await expect(page.getByRole('heading', { name: 'type hero' })).toBeVisible();+});diff --git a/apps/web/tsconfig.json b/apps/web/tsconfig.jsonindex e4e3191a..b460e618 100644--- a/apps/web/tsconfig.json+++ b/apps/web/tsconfig.json@@ -26,5 +26,5 @@"../../packages/monaco/monaco-editor.d.ts","../../packages/monaco/monaco-vim.d.ts"],- "exclude": ["node_modules", ".next", "public"]+ "exclude": ["node_modules", ".next", "public", "playwright-report"]}diff --git a/docker-compose.yaml b/docker-compose.yamlindex 1c62b1cd..3000250f 100644--- a/docker-compose.yaml+++ b/docker-compose.yaml@@ -12,6 +12,10 @@ services:MYSQL_ROOT_USER: devMYSQL_ROOT_PASSWORD: devMYSQL_DATABASE: typehero+ healthcheck:+ test: ['CMD', 'mysqladmin', 'ping', '-h', 'localhost']+ timeout: 20s+ retries: 10# create a volume for the docker container_namevolumes:- typehero-data:/bitnami/mysql/datadiff --git a/package.json b/package.jsonindex f93ea73e..108b3634 100644--- a/package.json+++ b/package.json@@ -20,6 +20,8 @@"lint": "eslint . --cache --cache-location \"node_modules/.cache/.eslintcache\" --max-warnings 0","lint:fix": "eslint . --fix --cache --cache-location \"node_modules/.cache/.eslintcache\" --max-warnings 0","refresh": "turbo db:reset db:seed --concurrency=1",+ "test:e2e": "turbo test:e2e",+ "test:e2e:install": "playwright install","tunnel": "cloudflared tunnel --url http://localhost:3000","typecheck": "turbo typecheck --continue"},diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 07cad0c6..07c27857 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -210,6 +210,9 @@ importers:'@next/bundle-analyzer':specifier: ^13.4.19version: 13.4.19+ '@playwright/test':+ specifier: ^1.38.0+ version: 1.38.0'@radix-ui/react-dialog':specifier: ^1.0.4version: 1.0.4(@types/[email protected])(@types/[email protected])([email protected])([email protected])@@ -676,7 +679,7 @@ importers:dependencies:'@next/eslint-plugin-next':specifier: canary- version: 13.4.20-canary.0+ version: 13.4.20-canary.33'@repo/tsconfig':specifier: workspace:*version: link:../config-typescript@@ -688,7 +691,7 @@ importers:version: 5.62.0([email protected])([email protected])'@vercel/style-guide':specifier: ^4.0.2eslint:specifier: ^8.45.0version: 8.45.0@@ -1773,8 +1776,8 @@ packages:glob: 7.1.7dev: true- /@next/[email protected]:- resolution: {integrity: sha512-vFC1meiw/awh2xJZMWaoECsfZbgkt2fF3HIg8f2fSM/w41Z86L1SS7SjKmpbbD87yq3tiqgqHmFv0JCych4g4g==}+ /@next/[email protected]:+ resolution: {integrity: sha512-oVec/t9lnmxbKeWXsPag+llh1GItT9VqDdVq0+zyupHG7HQHUDik+6/QFzykwKDbZiSnzoQQZ00/Oj2w7Ad77w==}dependencies:glob: 7.1.7dev: false@@ -2251,6 +2254,14 @@ packages:picocolors: 1.0.0tslib: 2.6.0+ /@playwright/[email protected]:+ resolution: {integrity: sha512-xis/RXXsLxwThKnlIXouxmIvvT3zvQj1JE39GsNieMUrMpb3/GySHDh2j8itCG22qKVD4MYLBp7xB73cUW/UUw==}+ engines: {node: '>=16'}+ hasBin: true+ dependencies:+ playwright: 1.38.0+ dev: false+/@polka/[email protected]:resolution: {integrity: sha512-a5Sab1C4/icpTZVzZc5Ghpz88yQtGOyNqYXcZgOssB2uuAr+wF/MvN6bgtW32q7HHrvBki+BsZ0OuNv6EV3K9g==}dev: false@@ -4358,7 +4369,7 @@ packages:yoga-wasm-web: 0.3.0dev: false- /@vercel/[email protected](@next/[email protected])([email protected])([email protected])([email protected]):+ /@vercel/[email protected](@next/[email protected])([email protected])([email protected])([email protected]):resolution: {integrity: sha512-FroL+oOePzhw7n/I+f7zr4WNroGHT/+2TlW6WH9+CVSjMNsEyu7Qstj2mI5gWIBjT1Y2ZImKPppCzI2cIYmNZw==}engines: {node: '>=16'}peerDependencies:@@ -4378,7 +4389,7 @@ packages:dependencies:'@babel/core': 7.22.10'@babel/eslint-parser': 7.22.10(@babel/[email protected])([email protected])- '@next/eslint-plugin-next': 13.4.20-canary.0+ '@next/eslint-plugin-next': 13.4.20-canary.33'@rushstack/eslint-patch': 1.3.2'@typescript-eslint/eslint-plugin': 5.59.1(@typescript-eslint/[email protected])([email protected])([email protected])'@typescript-eslint/parser': 5.62.0([email protected])([email protected])@@ -5149,7 +5160,7 @@ packages:dev: false- resolution: {integrity: sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=}+ resolution: {integrity: sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==}resolution: {integrity: sha512-P7X+QL9Hb9B/c8HI5BFFKmjgBu2XpQuF98WZ9XkO+dBGgk5XgwiQz7o1SmpglNWId3581UcS0SFAWfoIhMHPfg==}@@ -8670,6 +8681,22 @@ packages:resolution: {integrity: sha512-saLsH7WeYYPiD25LDuLRRY/i+6HaPYr6G1OUlN39otzkSTxKnubR9RTxS3/Kk50s1g2JTgFwWQDQyplC5/SHZg==}engines: {node: '>= 6'}+ resolution: {integrity: sha512-f8z1y8J9zvmHoEhKgspmCvOExF2XdcxMW8jNRuX4vkQFrzV4MlZ55iwb5QeyiFQgOFCUolXiRHgpjSEnqvO48g==}+ engines: {node: '>=16'}+ hasBin: true+ dev: false++ resolution: {integrity: sha512-fJGw+HO0YY+fU/F1N57DMO+TmXHTrmr905J05zwAQE9xkuwP/QLDk63rVhmyxh03dYnEhnRbsdbH9B0UVVRB3A==}+ engines: {node: '>=16'}+ hasBin: true+ dependencies:+ playwright-core: 1.38.0+ optionalDependencies:+ fsevents: 2.3.2+ dev: false+resolution: {integrity: sha512-Nc3IT5yHzflTfbjgqWcCPpo7DaKy4FnpB0l/zCAW0Tc7jxAiuqSxHasntB3D7887LSrA93kDJ9IXovxJYxyLCA==}engines: {node: '>=4'}diff --git a/turbo.json b/turbo.jsonindex 3a4993a9..0f550436 100644--- a/turbo.json+++ b/turbo.json@@ -22,7 +22,8 @@"RESEND_API_KEY","PORT","USERNAME",- "PASSWORD"+ "PASSWORD",+ "CI"]},"topo": {@@ -77,6 +78,9 @@"dependsOn": ["@repo/db#db:generate"],"persistent": true,"cache": false+ },+ "test:e2e": {+ "outputs": ["playwright-report/**"]}}}