From 08579d6cbb65399dec408f3b90bc9a9e285e6206 Mon Sep 17 00:00:00 2001 From: silverwind Date: Fri, 28 Jun 2024 18:15:51 +0200 Subject: Add initial typescript config and use it for eslint,vitest,playwright (#31186) This enables eslint to use the typescript parser and resolver which brings some benefits that eslint rules now have type information available and a tsconfig.json is required for the upcoming typescript migration as well. Notable changes done: - Add typescript parser and resolver - Move the vue-specific config into the root file - Enable `vue-scoped-css/enforce-style-type` rule, there was only one violation and I added a inline disable there. - Fix new lint errors that were detected because of the parser change - Update `i/no-unresolved` to remove now-unnecessary workaround for the resolver - Disable `i/no-named-as-default` as it seems to raise bogus issues in the webpack config - Change vitest config to typescript - Change playwright config to typescript - Add `eslint-plugin-playwright` and fix issues - Add `tsc` linting to `make lint-js` --- tests/e2e/README.md | 2 +- tests/e2e/e2e_test.go | 4 +-- tests/e2e/example.test.e2e.js | 57 ---------------------------------------- tests/e2e/example.test.e2e.ts | 56 +++++++++++++++++++++++++++++++++++++++ tests/e2e/utils_e2e.js | 60 ------------------------------------------ tests/e2e/utils_e2e.ts | 61 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 120 insertions(+), 120 deletions(-) delete mode 100644 tests/e2e/example.test.e2e.js create mode 100644 tests/e2e/example.test.e2e.ts delete mode 100644 tests/e2e/utils_e2e.js create mode 100644 tests/e2e/utils_e2e.ts (limited to 'tests/e2e') diff --git a/tests/e2e/README.md b/tests/e2e/README.md index e5fd1ca6c0..db083793d8 100644 --- a/tests/e2e/README.md +++ b/tests/e2e/README.md @@ -65,7 +65,7 @@ TEST_MSSQL_HOST=localhost:1433 TEST_MSSQL_DBNAME=gitea_test TEST_MSSQL_USERNAME= ## Running individual tests -Example command to run `example.test.e2e.js` test file: +Example command to run `example.test.e2e.ts` test file: _Note: unlike integration tests, this filtering is at the file level, not function_ diff --git a/tests/e2e/e2e_test.go b/tests/e2e/e2e_test.go index 60528a1a78..d6d27e66be 100644 --- a/tests/e2e/e2e_test.go +++ b/tests/e2e/e2e_test.go @@ -73,10 +73,10 @@ func TestMain(m *testing.M) { os.Exit(exitVal) } -// TestE2e should be the only test e2e necessary. It will collect all "*.test.e2e.js" files in this directory and build a test for each. +// TestE2e should be the only test e2e necessary. It will collect all "*.test.e2e.ts" files in this directory and build a test for each. func TestE2e(t *testing.T) { // Find the paths of all e2e test files in test directory. - searchGlob := filepath.Join(filepath.Dir(setting.AppPath), "tests", "e2e", "*.test.e2e.js") + searchGlob := filepath.Join(filepath.Dir(setting.AppPath), "tests", "e2e", "*.test.e2e.ts") paths, err := filepath.Glob(searchGlob) if err != nil { t.Fatal(err) diff --git a/tests/e2e/example.test.e2e.js b/tests/e2e/example.test.e2e.js deleted file mode 100644 index 57c69a2917..0000000000 --- a/tests/e2e/example.test.e2e.js +++ /dev/null @@ -1,57 +0,0 @@ -// @ts-check -import {test, expect} from '@playwright/test'; -import {login_user, save_visual, load_logged_in_context} from './utils_e2e.js'; - -test.beforeAll(async ({browser}, workerInfo) => { - await login_user(browser, workerInfo, 'user2'); -}); - -test('Load Homepage', async ({page}) => { - const response = await page.goto('/'); - await expect(response?.status()).toBe(200); // Status OK - await expect(page).toHaveTitle(/^Gitea: Git with a cup of tea\s*$/); - await expect(page.locator('.logo')).toHaveAttribute('src', '/assets/img/logo.svg'); -}); - -test('Test Register Form', async ({page}, workerInfo) => { - const response = await page.goto('/user/sign_up'); - await expect(response?.status()).toBe(200); // Status OK - await page.type('input[name=user_name]', `e2e-test-${workerInfo.workerIndex}`); - await page.type('input[name=email]', `e2e-test-${workerInfo.workerIndex}@test.com`); - await page.type('input[name=password]', 'test123test123'); - await page.type('input[name=retype]', 'test123test123'); - await page.click('form button.ui.primary.button:visible'); - // Make sure we routed to the home page. Else login failed. - await expect(page.url()).toBe(`${workerInfo.project.use.baseURL}/`); - await expect(page.locator('.secondary-nav span>img.ui.avatar')).toBeVisible(); - await expect(page.locator('.ui.positive.message.flash-success')).toHaveText('Account was successfully created. Welcome!'); - - save_visual(page); -}); - -test('Test Login Form', async ({page}, workerInfo) => { - const response = await page.goto('/user/login'); - await expect(response?.status()).toBe(200); // Status OK - - await page.type('input[name=user_name]', `user2`); - await page.type('input[name=password]', `password`); - await page.click('form button.ui.primary.button:visible'); - - await page.waitForLoadState('networkidle'); - - await expect(page.url()).toBe(`${workerInfo.project.use.baseURL}/`); - - save_visual(page); -}); - -test('Test Logged In User', async ({browser}, workerInfo) => { - const context = await load_logged_in_context(browser, workerInfo, 'user2'); - const page = await context.newPage(); - - await page.goto('/'); - - // Make sure we routed to the home page. Else login failed. - await expect(page.url()).toBe(`${workerInfo.project.use.baseURL}/`); - - save_visual(page); -}); diff --git a/tests/e2e/example.test.e2e.ts b/tests/e2e/example.test.e2e.ts new file mode 100644 index 0000000000..32813b3934 --- /dev/null +++ b/tests/e2e/example.test.e2e.ts @@ -0,0 +1,56 @@ +import {test, expect} from '@playwright/test'; +import {login_user, save_visual, load_logged_in_context} from './utils_e2e.ts'; + +test.beforeAll(async ({browser}, workerInfo) => { + await login_user(browser, workerInfo, 'user2'); +}); + +test('homepage', async ({page}) => { + const response = await page.goto('/'); + await expect(response?.status()).toBe(200); // Status OK + await expect(page).toHaveTitle(/^Gitea: Git with a cup of tea\s*$/); + await expect(page.locator('.logo')).toHaveAttribute('src', '/assets/img/logo.svg'); +}); + +test('register', async ({page}, workerInfo) => { + const response = await page.goto('/user/sign_up'); + await expect(response?.status()).toBe(200); // Status OK + await page.type('input[name=user_name]', `e2e-test-${workerInfo.workerIndex}`); + await page.type('input[name=email]', `e2e-test-${workerInfo.workerIndex}@test.com`); + await page.type('input[name=password]', 'test123test123'); + await page.type('input[name=retype]', 'test123test123'); + await page.click('form button.ui.primary.button:visible'); + // Make sure we routed to the home page. Else login failed. + await expect(page.url()).toBe(`${workerInfo.project.use.baseURL}/`); + await expect(page.locator('.secondary-nav span>img.ui.avatar')).toBeVisible(); + await expect(page.locator('.ui.positive.message.flash-success')).toHaveText('Account was successfully created. Welcome!'); + + save_visual(page); +}); + +test('login', async ({page}, workerInfo) => { + const response = await page.goto('/user/login'); + await expect(response?.status()).toBe(200); // Status OK + + await page.type('input[name=user_name]', `user2`); + await page.type('input[name=password]', `password`); + await page.click('form button.ui.primary.button:visible'); + + await page.waitForLoadState('networkidle'); // eslint-disable-line playwright/no-networkidle + + await expect(page.url()).toBe(`${workerInfo.project.use.baseURL}/`); + + save_visual(page); +}); + +test('logged in user', async ({browser}, workerInfo) => { + const context = await load_logged_in_context(browser, workerInfo, 'user2'); + const page = await context.newPage(); + + await page.goto('/'); + + // Make sure we routed to the home page. Else login failed. + await expect(page.url()).toBe(`${workerInfo.project.use.baseURL}/`); + + save_visual(page); +}); diff --git a/tests/e2e/utils_e2e.js b/tests/e2e/utils_e2e.js deleted file mode 100644 index d60c78b16e..0000000000 --- a/tests/e2e/utils_e2e.js +++ /dev/null @@ -1,60 +0,0 @@ -import {expect} from '@playwright/test'; - -const ARTIFACTS_PATH = `tests/e2e/test-artifacts`; -const LOGIN_PASSWORD = 'password'; - -// log in user and store session info. This should generally be -// run in test.beforeAll(), then the session can be loaded in tests. -export async function login_user(browser, workerInfo, user) { - // Set up a new context - const context = await browser.newContext(); - const page = await context.newPage(); - - // Route to login page - // Note: this could probably be done more quickly with a POST - const response = await page.goto('/user/login'); - await expect(response?.status()).toBe(200); // Status OK - - // Fill out form - await page.type('input[name=user_name]', user); - await page.type('input[name=password]', LOGIN_PASSWORD); - await page.click('form button.ui.primary.button:visible'); - - await page.waitForLoadState('networkidle'); - - await expect(page.url(), {message: `Failed to login user ${user}`}).toBe(`${workerInfo.project.use.baseURL}/`); - - // Save state - await context.storageState({path: `${ARTIFACTS_PATH}/state-${user}-${workerInfo.workerIndex}.json`}); - - return context; -} - -export async function load_logged_in_context(browser, workerInfo, user) { - let context; - try { - context = await browser.newContext({storageState: `${ARTIFACTS_PATH}/state-${user}-${workerInfo.workerIndex}.json`}); - } catch (err) { - if (err.code === 'ENOENT') { - throw new Error(`Could not find state for '${user}'. Did you call login_user(browser, workerInfo, '${user}') in test.beforeAll()?`); - } - } - return context; -} - -export async function save_visual(page) { - // Optionally include visual testing - if (process.env.VISUAL_TEST) { - await page.waitForLoadState('networkidle'); - // Mock page/version string - await page.locator('footer div.ui.left').evaluate((node) => node.innerHTML = 'MOCK'); - await expect(page).toHaveScreenshot({ - fullPage: true, - timeout: 20000, - mask: [ - page.locator('.secondary-nav span>img.ui.avatar'), - page.locator('.ui.dropdown.jump.item span>img.ui.avatar'), - ], - }); - } -} diff --git a/tests/e2e/utils_e2e.ts b/tests/e2e/utils_e2e.ts new file mode 100644 index 0000000000..5678c9c9d0 --- /dev/null +++ b/tests/e2e/utils_e2e.ts @@ -0,0 +1,61 @@ +import {expect} from '@playwright/test'; +import {env} from 'node:process'; + +const ARTIFACTS_PATH = `tests/e2e/test-artifacts`; +const LOGIN_PASSWORD = 'password'; + +// log in user and store session info. This should generally be +// run in test.beforeAll(), then the session can be loaded in tests. +export async function login_user(browser, workerInfo, user) { + // Set up a new context + const context = await browser.newContext(); + const page = await context.newPage(); + + // Route to login page + // Note: this could probably be done more quickly with a POST + const response = await page.goto('/user/login'); + await expect(response?.status()).toBe(200); // Status OK + + // Fill out form + await page.type('input[name=user_name]', user); + await page.type('input[name=password]', LOGIN_PASSWORD); + await page.click('form button.ui.primary.button:visible'); + + await page.waitForLoadState('networkidle'); // eslint-disable-line playwright/no-networkidle + + await expect(page.url(), {message: `Failed to login user ${user}`}).toBe(`${workerInfo.project.use.baseURL}/`); + + // Save state + await context.storageState({path: `${ARTIFACTS_PATH}/state-${user}-${workerInfo.workerIndex}.json`}); + + return context; +} + +export async function load_logged_in_context(browser, workerInfo, user) { + let context; + try { + context = await browser.newContext({storageState: `${ARTIFACTS_PATH}/state-${user}-${workerInfo.workerIndex}.json`}); + } catch (err) { + if (err.code === 'ENOENT') { + throw new Error(`Could not find state for '${user}'. Did you call login_user(browser, workerInfo, '${user}') in test.beforeAll()?`); + } + } + return context; +} + +export async function save_visual(page) { + // Optionally include visual testing + if (env.VISUAL_TEST) { + await page.waitForLoadState('networkidle'); // eslint-disable-line playwright/no-networkidle + // Mock page/version string + await page.locator('footer div.ui.left').evaluate((node) => node.innerHTML = 'MOCK'); + await expect(page).toHaveScreenshot({ + fullPage: true, + timeout: 20000, + mask: [ + page.locator('.secondary-nav span>img.ui.avatar'), + page.locator('.ui.dropdown.jump.item span>img.ui.avatar'), + ], + }); + } +} -- cgit v1.2.3