Skip to content

Commit

Permalink
#184: Add expected and actual logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
archfz committed May 27, 2023
1 parent 8026e27 commit 74ae352
Show file tree
Hide file tree
Showing 16 changed files with 70 additions and 20 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ directory. You should add `it.only` to the test case you are working on to speed
## Release Notes
- Add theoretical fix for nested output processor causing file to be rewritten with on after run logging pass. [issue](https://github.com/archfz/cypress-terminal-report/issues/185)
- Add extra logging for `assert` of the expected and the actual object. [issue](https://github.com/archfz/cypress-terminal-report/issues/184)
#### 5.1.1
Expand Down
15 changes: 13 additions & 2 deletions src/collector/LogCollectCypressCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,28 @@ module.exports = class LogCollectCypressCommand {
!['xhr', 'log', 'request'].includes(options.name) &&
!(options.name === 'task' && options.message.match(/ctrLogMessages/));

const formatLogMessage = (options) => {
let message = options.name + '\t' + options.message;

if (options.expected && options.actual) {
message += '\nActual: \t' + JSON.stringify(options.actual);
message += '\nExpected: \t' + JSON.stringify(options.expected);
}

return message;
};

Cypress.on('log:added', (options) => {
if (isOfInterest(options)) {
const log = options.name + '\t' + options.message;
const log = formatLogMessage(options);
const severity = options.state === 'failed' ? CONSTANTS.SEVERITY.ERROR : '';
this.collectorState.addLog([LOG_TYPE.CYPRESS_COMMAND, log, severity], options.id);
}
});

Cypress.on('log:changed', (options) => {
if (isOfInterest(options)) {
const log = options.name + '\t' + options.message;
const log = formatLogMessage(options);
const severity = options.state === 'failed' ? CONSTANTS.SEVERITY.ERROR : CONSTANTS.SEVERITY.SUCCESS;
this.collectorState.updateLog(log, severity, options.id);
}
Expand Down
12 changes: 12 additions & 0 deletions test/cypress/integration/expects.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/// <reference types="cypress" />
// remove no check once Cypress.sinon is typed
// https://github.com/cypress-io/cypress/issues/6720

context('Expects', () => {
it('expects', () => {
// https://on.cypress.io/spy
cy.visit('https://example.cypress.io/commands/spies-stubs-clocks')

expect([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]).to.eq([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
})
})
2 changes: 1 addition & 1 deletion test/cypress/integration/lateCommandUpdate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ describe("Late command update.", () => {
});
});

cy.get('.breaking-get', {timeout: 1});
cy.get('.breaking-get', {timeout: 5});
});
});
4 changes: 2 additions & 2 deletions test/output/out.spec.always.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
{
"type": "cy:command",
"severity": "success",
"message": "assert\texpected **200** to equal **200**"
"message": "assert\texpected **200** to equal **200**\nActual: \t200\nExpected: \t200"
},
{
"type": "cy:intercept",
Expand Down Expand Up @@ -129,7 +129,7 @@
{
"type": "cons:error",
"severity": "error",
"message": "Error: This is an error message with stack.\n at Context.eval (https://example.cypress.io/__cypress/tests?p=cypress/integration/happyFlow.spec.js:135:43)"
"message": "Error: This is an error message with stack.\n at Context.eval (https://example.cypress.io/__cypress/tests?p=cypress/integration/happyFlow.spec.js:140:43)"
},
{
"type": "cy:command",
Expand Down
4 changes: 3 additions & 1 deletion test/output/out.spec.always.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ cypress/integration/happyFlow.spec.js:
cy:command (K): wait @getComment
cy:command (K): its .response.statusCode
cy:command (K): assert expected **200** to equal **200**
Actual: 200
Expected: 200
cy:intercept (K): Method: POST
Matcher: "/comments"
cy:command (K): get .network-post
Expand Down Expand Up @@ -41,7 +43,7 @@ cypress/integration/happyFlow.spec.js:
cons:error (X): This is an error message
cy:command (K): window
cons:error (X): Error: This is an error message with stack.
at Context.eval (https://example.cypress.io/__cypress/tests?p=cypress/integration/happyFlow.spec.js:135:43)
at Context.eval (https://example.cypress.io/__cypress/tests?p=cypress/integration/happyFlow.spec.js:140:43)
cy:command (K): window
cons:log (K): This should console.log appear.
cy:command (K): window
Expand Down
4 changes: 2 additions & 2 deletions test/output/out.spec.onFail.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
{
"type": "cy:command",
"severity": "success",
"message": "assert\texpected **200** to equal **200**"
"message": "assert\texpected **200** to equal **200**\nActual: \t200\nExpected: \t200"
},
{
"type": "cy:intercept",
Expand Down Expand Up @@ -129,7 +129,7 @@
{
"type": "cons:error",
"severity": "error",
"message": "Error: This is an error message with stack.\n at Context.eval (https://example.cypress.io/__cypress/tests?p=cypress/integration/happyFlow.spec.js:135:43)"
"message": "Error: This is an error message with stack.\n at Context.eval (https://example.cypress.io/__cypress/tests?p=cypress/integration/happyFlow.spec.js:140:43)"
},
{
"type": "cy:command",
Expand Down
4 changes: 3 additions & 1 deletion test/output/out.spec.onFail.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ cypress/integration/happyFlow.spec.js:
cy:command (K): wait @getComment
cy:command (K): its .response.statusCode
cy:command (K): assert expected **200** to equal **200**
Actual: 200
Expected: 200
cy:intercept (K): Method: POST
Matcher: "/comments"
cy:command (K): get .network-post
Expand Down Expand Up @@ -41,7 +43,7 @@ cypress/integration/happyFlow.spec.js:
cons:error (X): This is an error message
cy:command (K): window
cons:error (X): Error: This is an error message with stack.
at Context.eval (https://example.cypress.io/__cypress/tests?p=cypress/integration/happyFlow.spec.js:135:43)
at Context.eval (https://example.cypress.io/__cypress/tests?p=cypress/integration/happyFlow.spec.js:140:43)
cy:command (K): window
cons:log (K): This should console.log appear.
cy:command (K): window
Expand Down
6 changes: 3 additions & 3 deletions test/output_nested_cucumber_spec/json/cucumber/Happy.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
{
"type": "cy:command",
"severity": "success",
"message": "assert\texpected **0** to be below **2**"
"message": "assert\texpected **0** to be below **2**\nActual: \t0\nExpected: \t2"
},
{
"type": "cy:command",
Expand Down Expand Up @@ -76,7 +76,7 @@
{
"type": "cy:command",
"severity": "success",
"message": "assert\texpected **1** to be below **2**"
"message": "assert\texpected **1** to be below **2**\nActual: \t1\nExpected: \t2"
},
{
"type": "cy:command",
Expand Down Expand Up @@ -130,4 +130,4 @@
}
]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
{
"type": "cy:command",
"severity": "error",
"message": "assert\texpected **1** to equal **2**"
"message": "assert\texpected **1** to equal **2**\nActual: \t1\nExpected: \t2"
}
],
"Calls function in another file that creates a sub-suite. -> sub-suite in different file -> subsuite test 2": [
{
"type": "cy:command",
"severity": "error",
"message": "assert\texpected **1** to equal **2**"
"message": "assert\texpected **1** to equal **2**\nActual: \t1\nExpected: \t2"
}
]
}
}
}
6 changes: 3 additions & 3 deletions test/output_nested_spec/json/happyFlow.spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
{
"type": "cy:command",
"severity": "success",
"message": "assert\texpected **200** to equal **200**"
"message": "assert\texpected **200** to equal **200**\nActual: \t200\nExpected: \t200"
},
{
"type": "cy:intercept",
Expand Down Expand Up @@ -129,7 +129,7 @@
{
"type": "cons:error",
"severity": "error",
"message": "Error: This is an error message with stack.\n at Context.eval (https://example.cypress.io/__cypress/tests?p=cypress/integration/happyFlow.spec.js:135:43)"
"message": "Error: This is an error message with stack.\n at Context.eval (https://example.cypress.io/__cypress/tests?p=cypress/integration/happyFlow.spec.js:140:43)"
},
{
"type": "cy:command",
Expand Down Expand Up @@ -213,4 +213,4 @@
}
]
}
}
}
4 changes: 4 additions & 0 deletions test/output_nested_spec/txt/callsSuiteInAnotherFile.spec.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
cypress/integration/callsSuiteInAnotherFile.spec.js:
Calls function in another file that creates a sub-suite. -> the test 2
cy:command (X): assert expected **1** to equal **2**
Actual: 1
Expected: 2

Calls function in another file that creates a sub-suite. -> sub-suite in different file -> subsuite test 2
cy:command (X): assert expected **1** to equal **2**
Actual: 1
Expected: 2

4 changes: 3 additions & 1 deletion test/output_nested_spec/txt/happyFlow.spec.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ cypress/integration/happyFlow.spec.js:
cy:command (K): wait @getComment
cy:command (K): its .response.statusCode
cy:command (K): assert expected **200** to equal **200**
Actual: 200
Expected: 200
cy:intercept (K): Method: POST
Matcher: "/comments"
cy:command (K): get .network-post
Expand Down Expand Up @@ -41,7 +43,7 @@ cypress/integration/happyFlow.spec.js:
cons:error (X): This is an error message
cy:command (K): window
cons:error (X): Error: This is an error message with stack.
at Context.eval (https://example.cypress.io/__cypress/tests?p=cypress/integration/happyFlow.spec.js:135:43)
at Context.eval (https://example.cypress.io/__cypress/tests?p=cypress/integration/happyFlow.spec.js:140:43)
cy:command (K): window
cons:log (K): This should console.log appear.
cy:command (K): window
Expand Down
13 changes: 13 additions & 0 deletions test/specs/commandsLogging.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,21 @@ describe('Commands logging.', () => {
const cleanStdout = clean(stdout, true);
expect(cleanStdout).to.contain(
`cy:command ${ICONS.success} assert\texpected **<a>** to have text **something else**
Actual: \t"something else"
Expected: \t"something else"
cy:command ${ICONS.error} get\tbreaking`,
);
});
}).timeout(60000);

it('Should log expected and actual for assert command.', async () => {
await runTest(commandBase([], ['expects.spec.js']), (error, stdout, stderr) => {
const cleanStdout = clean(stdout, true);
expect(cleanStdout).to.contain(
`cy:command ${ICONS.error} assert\texpected **[ Array(12) ]** to equal **[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]**
Actual: \t[1,2,3,4,5,6,7,8,9,10,11,12]
Expected: \t[1,2,3,4,5,6,7,8,9,10]`,
);
});
}).timeout(60000);
});
3 changes: 2 additions & 1 deletion test/specs/misc.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ describe('Misc.', () => {
});
}).timeout(60000);

it('Should filter and process late update logs correctly.', async () => {
it('Should filter and process late update logs correctly.', async function() {
this.retries(2);
await runTest(commandBase(['filterKeepOnlyWarningAndError=1,processAllLogs=1'], ['lateCommandUpdate.spec.js']), (error, stdout, stderr) => {
expect(stdout).to.contain(`cy:command ${ICONS.error} | get\t.breaking-get`);
expect(stdout).to.contain(`cy:xhr ${ICONS.warning} | STUBBED PUT https://example.cypress.io/comments/10
Expand Down
2 changes: 2 additions & 0 deletions test/specs/otherPluginIntegration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ describe('Other plugin integrations.', () => {
cy:command (K): get\t[href="https://on.cypress.io/request"]
cy:command (K): first
cy:command (K): assert\texpected **<a>** to have text **something else**
Actual: \t"something else"
Expected: \t"something else"
cy:command (X): get\tbreaking`);
});
}).timeout(90000);
Expand Down

0 comments on commit 74ae352

Please sign in to comment.