From afbc283423c633240c9628308508c9c32ec69d8b Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Tue, 4 Oct 2022 22:23:57 +0800 Subject: [PATCH] Move Cypress directory and convert it to JavaScript (#2170) --- config/cypress.config.js | 26 +++++++++++++++++++ cypress.config.ts | 15 ----------- cypress/e2e/setup.cy.ts | 24 ----------------- cypress/support/actors/actor.ts | 8 ------ cypress/support/e2e.ts | 1 - cypress/support/tasks/setup-task.ts | 15 ----------- package.json | 3 ++- src/util.js | 6 ++--- test/cypress/e2e/setup.cy.js | 18 +++++++++++++ {cypress => test/cypress}/plugins/index.js | 0 test/cypress/support/actors/actor.js | 8 ++++++ .../cypress/support/commands.js | 0 .../cypress/support/const/user-data.js | 2 +- test/cypress/support/e2e.js | 1 + .../cypress/support/pages/dashboard-page.js | 2 +- .../cypress/support/pages/setup-page.js | 2 +- test/cypress/support/tasks/setup-task.js | 11 ++++++++ tsconfig.json | 8 +++--- 18 files changed, 75 insertions(+), 75 deletions(-) create mode 100644 config/cypress.config.js delete mode 100644 cypress.config.ts delete mode 100644 cypress/e2e/setup.cy.ts delete mode 100644 cypress/support/actors/actor.ts delete mode 100644 cypress/support/e2e.ts delete mode 100644 cypress/support/tasks/setup-task.ts create mode 100644 test/cypress/e2e/setup.cy.js rename {cypress => test/cypress}/plugins/index.js (100%) create mode 100644 test/cypress/support/actors/actor.js rename cypress/support/commands.ts => test/cypress/support/commands.js (100%) rename cypress/support/const/user-data.ts => test/cypress/support/const/user-data.js (62%) create mode 100644 test/cypress/support/e2e.js rename cypress/support/pages/dasboard-page.ts => test/cypress/support/pages/dashboard-page.js (62%) rename cypress/support/pages/setup-page.ts => test/cypress/support/pages/setup-page.js (90%) create mode 100644 test/cypress/support/tasks/setup-task.js diff --git a/config/cypress.config.js b/config/cypress.config.js new file mode 100644 index 00000000..4eeb6845 --- /dev/null +++ b/config/cypress.config.js @@ -0,0 +1,26 @@ +const { defineConfig } = require("cypress"); + +module.exports = defineConfig({ + e2e: { + setupNodeEvents(on, config) { + + }, + fixturesFolder: "test/cypress/fixtures", + screenshotsFolder: "test/cypress/screenshots", + videosFolder: "test/cypress/videos", + downloadsFolder: "test/cypress/downloads", + supportFile: "test/cypress/support/e2e.js", + baseUrl: "http://localhost:3002", + defaultCommandTimeout: 10000, + pageLoadTimeout: 60000, + viewportWidth: 1920, + viewportHeight: 1080, + specPattern: [ + "test/cypress/e2e/setup.cy.js", + "test/cypress/e2e/**/*.js" + ], + }, + env: { + baseUrl: "http://localhost:3002", + }, +}); diff --git a/cypress.config.ts b/cypress.config.ts deleted file mode 100644 index d97e0875..00000000 --- a/cypress.config.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { defineConfig } from "cypress"; - -export default defineConfig({ - e2e: { - baseUrl: "http://localhost:3002", - defaultCommandTimeout: 10000, - pageLoadTimeout: 60000, - viewportWidth: 1920, - viewportHeight: 1080, - specPattern: ["cypress/e2e/setup.cy.ts", "cypress/e2e/**/*.ts"], - }, - env: { - baseUrl: "http://localhost:3002", - }, -}); diff --git a/cypress/e2e/setup.cy.ts b/cypress/e2e/setup.cy.ts deleted file mode 100644 index 94e18ede..00000000 --- a/cypress/e2e/setup.cy.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { actor } from "../support/actors/actor"; -import { DEFAULT_USER_DATA } from "../support/const/user-data"; -import { DashboardPage } from "../support/pages/dasboard-page"; -import { SetupPage } from "../support/pages/setup-page"; - -describe("user can create a new account on setup page", () => { - before(() => { - cy.visit("/setup"); - }); - - it("user can create new account", () => { - cy.url().should("be.equal", SetupPage.url); - actor.setupTask.fillAndSubmitSetupForm( - DEFAULT_USER_DATA.username, - DEFAULT_USER_DATA.password, - DEFAULT_USER_DATA.password - ); - - cy.url().should("be.equal", DashboardPage.url); - cy.get('[role="alert"]') - .should("be.visible") - .and("contain.text", "Added Successfully."); - }); -}); diff --git a/cypress/support/actors/actor.ts b/cypress/support/actors/actor.ts deleted file mode 100644 index 680c26ce..00000000 --- a/cypress/support/actors/actor.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { SetupTask } from "../tasks/setup-task"; - -class Actor { - setupTask: SetupTask = new SetupTask(); -} - -const actor = new Actor(); -export { actor }; diff --git a/cypress/support/e2e.ts b/cypress/support/e2e.ts deleted file mode 100644 index f887c29a..00000000 --- a/cypress/support/e2e.ts +++ /dev/null @@ -1 +0,0 @@ -import "./commands"; diff --git a/cypress/support/tasks/setup-task.ts b/cypress/support/tasks/setup-task.ts deleted file mode 100644 index 866e3ca5..00000000 --- a/cypress/support/tasks/setup-task.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { SetupPage } from "../pages/setup-page"; - -export class SetupTask { - fillAndSubmitSetupForm( - username: string, - password: string, - passwordRepeat: string - ) { - cy.get(SetupPage.usernameInput).type(username); - cy.get(SetupPage.passWordInput).type(password); - cy.get(SetupPage.passwordRepeatInput).type(passwordRepeat); - - cy.get(SetupPage.submitSetupForm).click(); - } -} diff --git a/package.json b/package.json index 2357bfe4..479876a6 100644 --- a/package.json +++ b/package.json @@ -62,7 +62,8 @@ "build-dist-and-restart": "npm run build && npm run start-server-dev", "start-pr-test": "node extra/checkout-pr.js && npm install && npm run dev", "cy:test": "node test/prepare-test-server.js && node server/server.js --port=3002 --data-dir=./data/test/ --e2e", - "cy:run": "npx cypress run --browser chrome --headless" + "cy:run": "npx cypress run --browser chrome --headless --config-file ./config/cypress.config.js", + "cypress-open": "concurrently -k -r \"node test/prepare-test-server.js && node server/server.js --port=3002 --data-dir=./data/test/\" \"cypress open --config-file ./config/cypress.config.js\"" }, "dependencies": { "@louislam/sqlite3": "~15.0.6", diff --git a/src/util.js b/src/util.js index d5e3617f..d766feb7 100644 --- a/src/util.js +++ b/src/util.js @@ -280,9 +280,9 @@ function getCryptoRandomInt(min, max) { } exports.getCryptoRandomInt = getCryptoRandomInt; /** - * Generate a secret - * @param length Lenght of secret to generate - * @returns + * Generate a random alphanumeric string of fixed length + * @param length Length of string to generate + * @returns string */ function genSecret(length = 64) { let secret = ""; diff --git a/test/cypress/e2e/setup.cy.js b/test/cypress/e2e/setup.cy.js new file mode 100644 index 00000000..57960403 --- /dev/null +++ b/test/cypress/e2e/setup.cy.js @@ -0,0 +1,18 @@ +const actor = require("../support/actors/actor"); +const userData = require("../support/const/user-data"); +const dashboardPage = require("../support/pages/dashboard-page"); +const setupPage = require("../support/pages/setup-page"); + +describe("user can create a new account on setup page", () => { + before(() => { + cy.visit("/setup"); + }); + it("user can create new account", () => { + cy.url().should("be.equal", setupPage.SetupPage.url); + actor.actor.setupTask.fillAndSubmitSetupForm(userData.DEFAULT_USER_DATA.username, userData.DEFAULT_USER_DATA.password, userData.DEFAULT_USER_DATA.password); + cy.url().should("be.equal", dashboardPage.DashboardPage.url); + cy.get('[role="alert"]') + .should("be.visible") + .and("contain.text", "Added Successfully."); + }); +}); diff --git a/cypress/plugins/index.js b/test/cypress/plugins/index.js similarity index 100% rename from cypress/plugins/index.js rename to test/cypress/plugins/index.js diff --git a/test/cypress/support/actors/actor.js b/test/cypress/support/actors/actor.js new file mode 100644 index 00000000..9775880b --- /dev/null +++ b/test/cypress/support/actors/actor.js @@ -0,0 +1,8 @@ +const setupTask = require("../tasks/setup-task"); +class Actor { + constructor() { + this.setupTask = new setupTask.SetupTask(); + } +} +const actor = new Actor(); +exports.actor = actor; diff --git a/cypress/support/commands.ts b/test/cypress/support/commands.js similarity index 100% rename from cypress/support/commands.ts rename to test/cypress/support/commands.js diff --git a/cypress/support/const/user-data.ts b/test/cypress/support/const/user-data.js similarity index 62% rename from cypress/support/const/user-data.ts rename to test/cypress/support/const/user-data.js index ee2264dd..983597bd 100644 --- a/cypress/support/const/user-data.ts +++ b/test/cypress/support/const/user-data.js @@ -1,4 +1,4 @@ -export const DEFAULT_USER_DATA = { +exports.DEFAULT_USER_DATA = { username: "testuser", password: "testuser123", }; diff --git a/test/cypress/support/e2e.js b/test/cypress/support/e2e.js new file mode 100644 index 00000000..449ab857 --- /dev/null +++ b/test/cypress/support/e2e.js @@ -0,0 +1 @@ +require("./commands"); diff --git a/cypress/support/pages/dasboard-page.ts b/test/cypress/support/pages/dashboard-page.js similarity index 62% rename from cypress/support/pages/dasboard-page.ts rename to test/cypress/support/pages/dashboard-page.js index 48660dc1..fc2d67e1 100644 --- a/cypress/support/pages/dasboard-page.ts +++ b/test/cypress/support/pages/dashboard-page.js @@ -1,3 +1,3 @@ -export const DashboardPage = { +exports.DashboardPage = { url: Cypress.env("baseUrl") + "/dashboard", }; diff --git a/cypress/support/pages/setup-page.ts b/test/cypress/support/pages/setup-page.js similarity index 90% rename from cypress/support/pages/setup-page.ts rename to test/cypress/support/pages/setup-page.js index 8c1b9cfa..44a525a8 100644 --- a/cypress/support/pages/setup-page.ts +++ b/test/cypress/support/pages/setup-page.js @@ -1,4 +1,4 @@ -export const SetupPage = { +exports.SetupPage = { url: Cypress.env("baseUrl") + "/setup", usernameInput: '[data-cy="username-input"]', passWordInput: '[data-cy="password-input"]', diff --git a/test/cypress/support/tasks/setup-task.js b/test/cypress/support/tasks/setup-task.js new file mode 100644 index 00000000..205f78c2 --- /dev/null +++ b/test/cypress/support/tasks/setup-task.js @@ -0,0 +1,11 @@ +const setupPage = require("../pages/setup-page"); + +class SetupTask { + fillAndSubmitSetupForm(username, password, passwordRepeat) { + cy.get(setupPage.SetupPage.usernameInput).type(username); + cy.get(setupPage.SetupPage.passWordInput).type(password); + cy.get(setupPage.SetupPage.passwordRepeatInput).type(passwordRepeat); + cy.get(setupPage.SetupPage.submitSetupForm).click(); + } +} +exports.SetupTask = SetupTask; diff --git a/tsconfig.json b/tsconfig.json index cd5f7c5d..c5454642 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,11 +11,9 @@ "removeComments": false, "preserveConstEnums": true, "sourceMap": false, - "strict": true, - "types": ["cypress"] + "strict": true }, "files": [ - "./src/util.ts", - ], - "include": ["cypress/**/*.ts"] + "./src/util.ts" + ] }