Skip to content

Commit

Permalink
[patch] fixed synchronous logs addition plus added tests for assertio…
Browse files Browse the repository at this point in the history
…ns (#164)
  • Loading branch information
mmisty authored Jul 1, 2024
1 parent cc77eb6 commit 1c4d9ed
Show file tree
Hide file tree
Showing 29 changed files with 1,012 additions and 70 deletions.
14 changes: 14 additions & 0 deletions src/common/command-names.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@ export const COMMAND_REQUEST = 'request';
export type CommandLog = {
attributes?: { name?: string; commandLogId?: string; consoleProps?: () => any; message?: string; error?: any };
};

export type CyLog = {
name?: string;
commandLogId?: string;
consoleProps?: () => any;
message?: string;
chainerId?: string;
error?: any;
err?: any;
end?: boolean;
ended?: boolean;
state?: string;
};

export type CommandT = {
state?: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
36 changes: 33 additions & 3 deletions src/setup/cypress-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
filterCommandLog,
ignoreAllCommands,
isGherkin,
CyLog,
stepMessage,
stringify,
withTry,
Expand All @@ -22,6 +23,7 @@ const UNCAUGHT_EXCEPTION_NAME = 'uncaught exception';
const UNCAUGHT_EXCEPTION_STATUS = 'broken' as Status;
const failedStatus = 'failed' as Status;
const passedStatus = 'passed' as Status;
const brokenStatus = 'broken' as Status;

type OneRequestConsoleProp = {
'Request Body': unknown;
Expand Down Expand Up @@ -255,14 +257,38 @@ export const handleCyLogEvents = (
gherkinLog.current = undefined;
});

Cypress.on('log:added', log => {
Cypress.on('log:added', (log: CyLog) => {
if (!allureLogCyCommands()) {
return;
}

withTry('report log:added', () => {
const cmdMessage = stepMessage(log.name, log.message === 'null' ? '' : log.message);
const logName = log.name;
const logName = log.name ?? 'no-log-name';
const logMessage = log.message;
const chainerId = log.chainerId;
const end = log.end || log.ended;
const logState = log.state;

const cmdMessage = stepMessage(logName, logMessage === 'null' ? '' : logMessage);

// console.log(log);

if (!chainerId && end) {
// synchronous log without commands
Cypress.Allure.startStep(cmdMessage);

let status = passedStatus;

if (logName === 'WARNING') {
status = brokenStatus;
}

if (logState === 'failed') {
status = failedStatus;
}

Cypress.Allure.endStep(status);
}

if (isGherkin(logName)) {
if (gherkinLog.current) {
Expand Down Expand Up @@ -327,6 +353,10 @@ export const handleCyLogEvents = (
details = { message: err?.message, trace: err?.stack };
}

if (logName === 'WARNING') {
state = brokenStatus;
}

Cypress.Allure.endStep(state, details);
});
};
Expand Down
20 changes: 8 additions & 12 deletions src/setup/process-tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ export const processTagsOnTestStart = (test: Mocha.Test) => {
const [urlOrId, name] = t.info ?? [];

if (!urlOrId) {
Cypress.log({ message: `WARN: tag @${tagNoAt} tag should have id: @${tagNoAt}("idOrUrl")` }).error(
new Error(),
);
const message = `tag @${tagNoAt} tag should have id: @${tagNoAt}("idOrUrl")`;
Cypress.log({ name: 'WARNING', message }).error(new Error());
break;
}
Cypress.Allure[tagNoAt](urlOrId, name);
Expand All @@ -31,9 +30,8 @@ export const processTagsOnTestStart = (test: Mocha.Test) => {
const [urlOrId, name, type] = t.info ?? [];

if (!urlOrId) {
Cypress.log({ message: `WARN: Tag @${tagNoAt} should have id or url: @${tagNoAt}("idOrUrl")` }).error(
new Error(),
);
const message = `tag @${tagNoAt} should have id or url: @${tagNoAt}("idOrUrl")`;
Cypress.log({ name: 'WARNING', message }).error(new Error());

break;
}
Expand Down Expand Up @@ -72,9 +70,8 @@ export const processTagsOnTestStart = (test: Mocha.Test) => {
const [singleValue] = t.info ?? [];

if (!singleValue) {
Cypress.log({ message: `WARN: tag @${tagNoAt} tag should have value: @${tagNoAt}("value")` }).error(
new Error(),
);
const message = `tag @${tagNoAt} tag should have value: @${tagNoAt}("value")`;
Cypress.log({ name: 'WARNING', message }).error(new Error());
break;
}
Cypress.Allure[tagNoAt](singleValue as Severity);
Expand All @@ -86,9 +83,8 @@ export const processTagsOnTestStart = (test: Mocha.Test) => {
const [name, value] = t.info ?? [];

if (!name) {
Cypress.log({
message: `WARN: tag @${tagNoAt} tag should have name and/or value: @${tagNoAt}("myLabel","value")`,
}).error(new Error());
const message = `tag @${tagNoAt} tag should have name and/or value: @${tagNoAt}("myLabel","value")`;
Cypress.log({ name: 'WARNING', message }).error(new Error());
break;
}
Cypress.Allure.label(name, value);
Expand Down
85 changes: 53 additions & 32 deletions tests/cy-helper/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,12 +298,22 @@ export const fullStepAttachment = (
export const fullStepMap = (
res: AllureTest,
mapStep?: (m: StepResult) => any,
filterStep?: (m: StepResult) => boolean,
) => {
const skipItems = ['generatereport', 'coverage'];

return mapSteps(res.steps as StepResult[], mapStep, z =>
skipItems.every(y => z.name?.toLowerCase().indexOf(y) === -1),
);
return mapSteps(res.steps as StepResult[], mapStep, z => {
const includesNoSkippedSteps = skipItems.every(
y => z.name?.toLowerCase().indexOf(y) === -1,
);
const isFiltered = filterStep?.(z);

if (isFiltered === undefined) {
return includesNoSkippedSteps;
}

return includesNoSkippedSteps && isFiltered;
});
};

// eslint-disable-next-line jest/no-export
Expand Down Expand Up @@ -458,17 +468,23 @@ export const createResTest2 = (
attempt(retries);
});
};
const video = parseBoolean(envConfig?.video ?? `${true}`);
// todo fix video
console.log(`video:${video}`);

return cy
.run({
spec,
spec: spec as string,
specPattern: 'integration/e2e/**/*.(cy|test|spec).ts',
port,
browser: 'chrome',
trashAssetsBeforeRuns: true,
env,
quiet: `${process.env.QUIET}` === 'true',
video: parseBoolean(envConfig?.video ?? `${true}`),
video,
// config: {
// ...config,
// trashAssetsBeforeRuns: true,
// },
})
.catch(e => {
err = e as Error;
Expand Down Expand Up @@ -582,17 +598,17 @@ export const covergeAfterAll = [
start: 1323475200000,
status: 'passed',
steps: [
// {
// attachments: [],
// name: 'Coverage: Generating report [@cypress/code-coverage]',
// parameters: [],
// stage: 'finished',
// start: 1323475200000,
// status: 'passed',
// statusDetails: {},
// steps: [],
// stop: 1323475200011,
// },
{
attachments: [],
name: 'Coverage: Generating report [@cypress/code-coverage]',
parameters: [],
stage: 'finished',
start: 1323475200000,
status: 'passed',
statusDetails: {},
steps: [],
stop: 1323475200011,
},
],
stop: 1323475200010,
},
Expand All @@ -607,17 +623,17 @@ export const covergeBeforeAll = [
start: 1323475200000,
status: 'passed',
steps: [
// {
// attachments: [],
// name: 'Coverage: Reset [@cypress/code-coverage]',
// parameters: [],
// stage: 'finished',
// start: 1323475200000,
// status: 'passed',
// statusDetails: {},
// steps: [],
// stop: 1323475200011,
// },
{
attachments: [],
name: 'Coverage: Reset [@cypress/code-coverage]',
parameters: [],
stage: 'finished',
start: 1323475200000,
status: 'passed',
statusDetails: {},
steps: [],
stop: 1323475200011,
},
],
stop: 1323475200010,
},
Expand Down Expand Up @@ -657,6 +673,7 @@ export type TestData = {
testName: string;
index?: number;
mapStep?: (m: StepResult) => any;
filterStep?: (m: StepResult) => boolean;
expected: any[];
}[];

Expand Down Expand Up @@ -813,10 +830,14 @@ export const generateChecksTests = (res: Result, testsForRun: TestData[]) => {
it(`steps for test ${testItem.testName} ${testItem.index ?? ''}`, () => {
const tests = getTest(resFixed, testItem.testName);

const obj = fullStepMap(tests[testItem.index ?? 0]!, m => ({
name: m.name,
...(testItem.mapStep?.(m) ?? {}),
}));
const obj = fullStepMap(
tests[testItem.index ?? 0]!,
m => ({
name: m.name,
...(testItem.mapStep?.(m) ?? {}),
}),
testItem.filterStep,
);

wrapError(() => expect(obj).toEqual(testItem.expected));
});
Expand Down
18 changes: 18 additions & 0 deletions tests/test-folder/mocha-events/assertions/assertions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {
createResTest2,
generateChecksTests,
selectTestsToRun,
} from '@test-utils';
import { basename } from 'path';

describe(`suite: ${basename(__dirname)}`, () => {
const testsForOneCyRun = selectTestsToRun(__dirname);

const res = createResTest2(
testsForOneCyRun.map(x => x.spec),
// video false to speedup
{ allureAddVideoOnPass: 'false', video: 'false' /* DEBUG: 'true'*/ },
);

generateChecksTests(res, testsForOneCyRun);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { TestData } from '@test-utils';
import { basename } from 'path';
import { visitHtmlCode } from './visit-html';

const rootSuite = `${basename(__filename)}`;

const data: TestData = {
name: rootSuite,
rootSuite,
fileName: __filename,
spec: `
describe('${rootSuite}', { defaultCommandTimeout: 300 },() => {
${visitHtmlCode}
it('test 1', () => {
cy.get('[data-test-id="item"]').should('contain', 'NonExistent');
});
});
`,

expect: {
testsNames: [`${rootSuite} test 1`],

testStatuses: [
{
testName: 'test 1',
index: 0,
status: 'failed',
statusDetails: {
message: [
"Timed out retrying after 300ms: expected '[ <div>, 4 more... ]' to contain 'NonExistent'",
],
},
},
],

testAttachments: [
{
expectMessage: 'should have no attachments',
testName: 'test 1',
index: 0,
attachments: [
{
name: 'data-assertions-one-failed-for-command.ts -- test 1 (failed).png',
source: 'source.png',
type: 'image/png',
},
],
},
],

testSteps: [
{
testName: 'test 1',
index: 0,
mapStep: m => ({
status: m.status,
attachments: m.attachments,
statusDetails: m.statusDetails,
}),
filterStep: m =>
['before each', 'after each'].every(
x => m.name && m.name.indexOf(x) === -1,
),
expected: [
{
attachments: [],
name: 'get: [data-test-id="item"]',
status: 'failed',
statusDetails: {},
steps: [
{
attachments: [],
name: 'assert: expected **[ <div>, 4 more... ]** to contain **NonExistent**',
status: 'failed',
statusDetails: {},
steps: [],
},
],
},
],
},
],
},
};

export default data;
Loading

0 comments on commit 1c4d9ed

Please sign in to comment.