PromucFlow_constructor/app/client/cypress/scripts/cypress-hooks.ts

148 lines
4.7 KiB
TypeScript
Raw Normal View History

ci: Split spec improvements for cypress ci runs (#26774) > Pull Request Template > > Use this template to quickly create a well written pull request. Delete all quotes before creating the pull request. > ## Description > Add a TL;DR when description is extra long (helps content team) > > Please include a summary of the changes and which issue has been fixed. Please also include relevant motivation > and context. List any dependencies that are required for this change > > Links to Notion, Figma or any other documents that might be relevant to the PR > > #### PR fixes following issue(s) Fixes # (issue number) > if no issue exists, please create an issue and ask the maintainers about this first > > #### Media > A video or a GIF is preferred. when using Loom, don’t embed because it looks like it’s a GIF. instead, just link to the video > > #### Type of change > Please delete options that are not relevant. - Bug fix (non-breaking change which fixes an issue) - New feature (non-breaking change which adds functionality) - Breaking change (fix or feature that would cause existing functionality to not work as expected) - Chore (housekeeping or task changes that don't impact user perception) - This change requires a documentation update > > > ## Testing > #### How Has This Been Tested? > Please describe the tests that you ran to verify your changes. Also list any relevant details for your test configuration. > Delete anything that is not relevant - [ ] Manual - [ ] JUnit - [ ] Jest - [ ] Cypress > > #### Test Plan > Add Testsmith test cases links that relate to this PR > > #### Issues raised during DP testing > Link issues raised during DP testing for better visiblity and tracking (copy link from comments dropped on this PR) > > > ## Checklist: #### Dev activity - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] PR is being merged under a feature flag #### QA activity: - [ ] [Speedbreak features](https://github.com/appsmithorg/TestSmith/wiki/Guidelines-for-test-plans#speedbreakers-) have been covered - [ ] Test plan covers all impacted features and [areas of interest](https://github.com/appsmithorg/TestSmith/wiki/Guidelines-for-test-plans#areas-of-interest-) - [ ] Test plan has been peer reviewed by project stakeholders and other QA members - [ ] Manually tested functionality on DP - [ ] We had an implementation alignment call with stakeholders post QA Round 2 - [ ] Cypress test cases have been added and approved by SDET/manual QA - [ ] Added `Test Plan Approved` label after Cypress tests were reviewed - [ ] Added `Test Plan Approved` label after JUnit tests were reviewed
2023-09-25 04:19:21 +00:00
import os from "os";
import util from "./util";
export async function cypressHooks(
on: Cypress.PluginEvents,
config: Cypress.PluginConfigOptions,
) {
const _ = new util();
const s3 = _.configureS3();
const dbClient = _.configureDbClient();
const runData: any = {
workflowId: _.getVars().runId,
attempt: _.getVars().attempt_number,
os: os.type(),
};
const matrix: any = {
matrixId: _.getVars().thisRunner,
matrixStatus: "started",
};
const specData: any = {};
on("before:run", async (runDetails: Cypress.BeforeRunDetails) => {
runData.browser = runDetails.browser?.name;
const client = await dbClient.connect();
try {
const attemptRes = await client.query(
`UPDATE public."attempt" SET "browser" = $1, "os" = $2 WHERE "workflowId" = $3 AND attempt = $4 RETURNING id`,
[runData.browser, runData.os, runData.workflowId, runData.attempt],
);
runData.attemptId = attemptRes.rows[0].id;
const matrixRes = await client.query(
`SELECT id FROM public."matrix" WHERE "attemptId" = $1 AND "matrixId" = $2`,
[runData.attemptId, matrix.matrixId],
);
matrix.id = matrixRes.rowCount > 0 ? matrixRes.rows[0].id : "";
} catch (err) {
console.log(err);
} finally {
client.release();
}
});
on("before:spec", async (spec: Cypress.Spec) => {
specData.name = spec.relative;
specData.matrixId = matrix.id;
const client = await dbClient.connect();
try {
if (!specData.name.includes("no_spec.ts")) {
const specResponse = await client.query(
`UPDATE public."specs" SET "status" = $1 WHERE "name" = $2 AND "matrixId" = $3 RETURNING id`,
["in-progress", specData.name, matrix.id],
);
specData.specId = specResponse.rows[0].id;
}
} catch (err) {
console.log(err);
} finally {
client.release();
}
});
on(
"after:spec",
async (spec: Cypress.Spec, results: CypressCommandLine.RunResult) => {
const client = await dbClient.connect();
try {
if (!specData.name.includes("no_spec.ts")) {
await client.query(
'UPDATE public.specs SET "testCount" = $1, "passes" = $2, "failed" = $3, "skipped" = $4, "pending" = $5, "status" = $6, "duration" = $7 WHERE id = $8',
[
results.stats.tests,
results.stats.passes,
results.stats.failures,
results.stats.skipped,
results.stats.pending,
results.stats.failures > 0 ? "fail" : "pass",
results.stats.duration,
specData.specId,
],
);
for (const test of results.tests) {
const testResponse = await client.query(
`INSERT INTO public.tests ("name", "specId", "status", "retries", "retryData") VALUES ($1, $2, $3, $4, $5) RETURNING id`,
[
test.title[1],
specData.specId,
test.state,
test.attempts.length,
test.displayError,
],
);
if (
test.attempts.some((attempt) => attempt.state === "failed") &&
results.screenshots.length > 0
) {
const out = results.screenshots.filter((scr) =>
scr.path.includes(test.title[1]),
);
console.log("Uploading screenshots...");
for (const scr of out) {
const attempt = scr.path.includes("attempt 2") ? 2 : 1;
const key = `${testResponse.rows[0].id}_${specData.specId}_${attempt}`;
await _.uploadToS3(s3, scr.path, key);
}
}
}
if (
results.tests.some((test) =>
test.attempts.some((attempt) => attempt.state === "failed"),
) &&
results.video
) {
console.log("Uploading video...");
const key = `${specData.specId}`;
await _.uploadToS3(s3, results.video, key);
}
}
} catch (err) {
console.log(err);
} finally {
client.release();
}
},
);
on("after:run", async (runDetails) => {
const client = await dbClient.connect();
try {
if (!specData.name.includes("no_spec.ts")) {
await client.query(
`UPDATE public.matrix SET "status" = $1 WHERE id = $2`,
["done", matrix.id],
);
await client.query(
`UPDATE public.attempt SET "endTime" = $1 WHERE "id" = $2`,
[new Date(), runData.attemptId],
);
}
} catch (err) {
console.log(err);
} finally {
client.release();
await dbClient.end();
}
});
}