Skip to content

Commit

Permalink
Initial E2E test setup with Playwright (#419)
Browse files Browse the repository at this point in the history
* Playwright setup and basic homepage test

* Run scripts

* Move git ignore entries to root

* Disable Testing Library linting on Playwright test files

* Allow dev dependency imports for e2e test files

* Typecheck fixes

* Target FE unit tests for Testing Library lint plugin (replaces disable mechanism)

* Clean out comments
  • Loading branch information
jonkafton authored Jan 30, 2024
1 parent 5a9d261 commit 0fb9978
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 3 deletions.
7 changes: 6 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ module.exports = {
extends: [
"eslint-config-mitodl",
"eslint-config-mitodl/jest",
"plugin:testing-library/react",
"plugin:import/typescript",
"plugin:mdx/recommended",
"prettier",
Expand Down Expand Up @@ -49,6 +48,7 @@ module.exports = {
"**/*.stories.ts",
"**/*.stories.tsx",
"**/*.mdx",
"e2e_testing/**/*.ts",
],
},
],
Expand Down Expand Up @@ -83,6 +83,11 @@ module.exports = {
...restrictedImports(),
},
},
{
files: ["./frontends/**/*.test.{ts,tsx}"],
plugins: ["testing-library"],
extends: ["plugin:testing-library/react"],
},
],
}

Expand Down
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,9 @@ github-pages/public

# Storybook
storybook-static/

/e2e_testing/test-results/
/e2e_testing/playwright-report/
/e2e_testing/blob-report/
/e2e_testing/playwright/.cache/
!/e2e_testing/.env
1 change: 1 addition & 0 deletions e2e_testing/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BASE_URL=https://mit-open-rc.odl.mit.edu/
1 change: 1 addition & 0 deletions e2e_testing/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# e2e_testing
13 changes: 13 additions & 0 deletions e2e_testing/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "e2e_testing",
"packageManager": "[email protected]",
"scripts": {
"test": "playwright test",
"start": "playwright test --ui"
},
"devDependencies": {
"@playwright/test": "^1.41.0",
"@types/node": "^20.11.5",
"dotenv": "^16.3.1"
}
}
69 changes: 69 additions & 0 deletions e2e_testing/playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { defineConfig, devices } from "@playwright/test"
import "dotenv/config"

/**
* See https://playwright.dev/docs/test-configuration.
*/
export default defineConfig({
testDir: "./tests",
/* 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",
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
/* 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 start',
// url: 'http://127.0.0.1:3000',
// reuseExistingServer: !process.env.CI,
// },
})
27 changes: 27 additions & 0 deletions e2e_testing/tests/001_home.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { test, expect } from "@playwright/test"

const { BASE_URL } = process.env

test("Home page loads and main elements are visible", async ({ page }) => {
await page.goto(BASE_URL!)

await expect(
page.getByRole("link", { name: "MIT Open" }),
"Header link is visible",
).toBeVisible()

await expect(
page.getByRole("heading", { name: "Learn from MIT" }),
"Main heading is visible",
).toBeVisible()

await expect(
page.getByPlaceholder("What do you want to learn?"),
"Main search input is visible",
).toBeVisible()

await expect(
page.getByRole("heading", { name: "Upcoming Courses" }),
"Upcoming courses header visible",
).toBeVisible()
})
6 changes: 6 additions & 0 deletions e2e_testing/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"baseUrl": "./",
"skipLibCheck": true
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"name": "root",
"private": true,
"workspaces": [
"frontends/*"
"frontends/*",
"e2e_testing"
],
"scripts": {
"foreach": "yarn workspaces foreach -pvi --exclude=root run",
Expand Down
75 changes: 74 additions & 1 deletion yarn.lock

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

0 comments on commit 0fb9978

Please sign in to comment.