Skip to content

Commit

Permalink
#113 #114 #116: Fix for skip tests and collect logs validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
archfz committed Aug 18, 2021
1 parent f182881 commit e77a385
Show file tree
Hide file tree
Showing 20 changed files with 162 additions and 46 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,10 @@ directory. You should add `it.only` to the test case you are working on to speed
## Release Notes
- Fix issue `cy:intercept` not between the allowed configuration options. [issue](https://github.com/archfz/cypress-terminal-report/issues/113)
- Fix issue with plugin breaking cypress with skipped tests. [issue1](https://github.com/archfz/cypress-terminal-report/issues/116) [issue2](https://github.com/archfz/cypress-terminal-report/issues/114)
Update cypress to 8.3.0 in tests to confirm support.
#### 3.3.0
- Added support for logging command from skipped tests. [issue](https://github.com/archfz/cypress-terminal-report/issues/111)
Expand Down
20 changes: 14 additions & 6 deletions src/collector/LogCollectCypressXhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,39 @@ module.exports = class LogCollectCypressXhr {
Cypress.on('log:changed', async (options) => {
if (
options.instrument === 'command' &&
options.name === 'xhr' &&
['request', 'xhr'].includes(options.name) &&
options.consoleProps &&
options.state !== 'pending'
) {
const [, statusCode, statusText] = /^(\d{3})\s\((.+)\)$/.exec(options.consoleProps.Status) || [];
const isSuccess = statusCode && statusCode[0] === '2';
let statusCode, statusText;

if (!options.consoleProps.XHR) {
[, statusCode, statusText] = /^(\d{3})\s\((.+)\)$/.exec(options.consoleProps.Status) || [];
} else {
statusCode = options.consoleProps.XHR.status;
statusText = options.consoleProps.XHR.statusText;
}

const isSuccess = statusCode && (statusCode + '')[0] === '2';
const severity = isSuccess ? CONSTANTS.SEVERITY.SUCCESS : CONSTANTS.SEVERITY.WARNING;
let log = formatXhr(options);

if (options.consoleProps.Duration) {
log += ` (${formatDuration(options.consoleProps.Duration)})`;
}
if (options.consoleProps.Status) {
if (statusCode && statusText) {
log += `\nStatus: ${statusCode} - ${statusText}`;
}
if (options.err && options.err.message.match(/abort/)) {
log += ' - ABORTED';
}
if (
!isSuccess &&
options.consoleProps.Response &&
options.consoleProps.XHR &&
options.consoleProps.XHR.response.size &&
!this.collectorState.hasXhrResponseBeenLogged(options.consoleProps.XHR.id)
) {
log += `\nResponse body: ${await this.format.formatXhrBody(options.consoleProps.Response.body)}`;
log += `\nResponse body: ${await this.format.formatXhrBody(options.consoleProps.XHR.response)}`;
}

this.collectorState.updateLog(log, severity, options.id);
Expand Down
6 changes: 5 additions & 1 deletion src/collector/LogCollectExtendedControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,11 @@ module.exports = class LogCollectExtendedControl extends LogCollectBaseControl {
// Logs commands if test was manually skipped.
Cypress.mocha.getRunner().on('pending', function (test) {
if (self.collectorState.getCurrentTest()) {
sendLogsToPrinterForATest(self.collectorState.getCurrentTest());
// In case of fully skipped tests we might not yet have a log stack.
if (!self.collectorState.hasLogsCurrentStack()) {
self.collectorState.addNewLogStack();
}
sendLogsToPrinterForATest(test);
}
});
}
Expand Down
45 changes: 34 additions & 11 deletions src/collector/LogCollectSimpleControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,42 @@ module.exports = class LogCollectSimpleControl extends LogCollectBaseControl {
return this.prepareLogs(logStackIndex, {mochaRunnable, testState, testTitle, testLevel});
};

// Need to wait for command log update debounce.
cy.wait(wait, {log: false})
.then(() => {
cy.task(
CONSTANTS.TASK_NAME,
{
if (options.noQueue) {
Promise.resolve().then(() => {
Cypress.backend('task', {
task: CONSTANTS.TASK_NAME,
arg: {
spec: spec,
test: testTitle,
messages: prepareLogs(),
state: testState,
level: testLevel,
consoleTitle: options.consoleTitle,
isHook: options.isHook,
},
{log: false}
);
});
}
})
// For some reason cypress throws empty error although the task indeed works.
.catch((error) => {/* noop */})
}).catch(console.error);
} else {
// Need to wait for command log update debounce.
cy.wait(wait, {log: false})
.then(() => {
cy.task(
CONSTANTS.TASK_NAME,
{
spec: spec,
test: testTitle,
messages: prepareLogs(),
state: testState,
level: testLevel,
consoleTitle: options.consoleTitle,
isHook: options.isHook,
},
{log: false}
);
});
}
}

registerState() {
Expand Down Expand Up @@ -86,7 +105,11 @@ module.exports = class LogCollectSimpleControl extends LogCollectBaseControl {
// Logs commands if test was manually skipped.
Cypress.mocha.getRunner().on('pending', function (test) {
if (self.collectorState.getCurrentTest()) {
self.sendLogsToPrinter(self.collectorState.getCurrentLogStackIndex(), self.collectorState.getCurrentTest());
// In case of fully skipped tests we might not yet have a log stack.
if (!self.collectorState.hasLogsCurrentStack()) {
self.collectorState.addNewLogStack();
}
self.sendLogsToPrinter(self.collectorState.getCurrentLogStackIndex(), self.collectorState.getCurrentTest(), {noQueue: true});
}
});
}
Expand Down
1 change: 1 addition & 0 deletions src/installLogsCollector.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"cy:log",
"cy:xhr",
"cy:request",
"cy:intercept",
"cy:route",
"cy:command"
]
Expand Down
37 changes: 37 additions & 0 deletions test/cypress/integration/skipTest.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
describe('Describe 1', () => {
before(() => {
cy.visit(`https://www.google.co.uk`);
});

it.skip('Skipped test 1', () => {
cy.log('first skip');
});

it('Test 2', () => {
cy.log('test 2');
});
});

describe('Describe 2', () => {
beforeEach(() => {
cy.visit(`https://www.google.co.uk`);
});

// The tests gets hang here
it.skip('Skipped test 2', () => {
cy.log('Skipped test 2');
});

//This second skipped test seems to be the culprit
it.skip('Skipped test 3', () => {
cy.log('Skipped test 3');
});

it('Test 3', () => {
cy.log('Test 3');
});

it('Test 4', () => {
cy.log('Test 4');
});
});
5 changes: 5 additions & 0 deletions test/cypress/support/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ if (env.supportBadConfig == '1') {
shouldNotBeHere: ""
};
}
if (env.supportGoodConfig == '1') {
config = {
collectTypes: ['cons:log','cons:info', 'cons:warn', 'cons:error', 'cy:log', 'cy:xhr', 'cy:request', 'cy:route', 'cy:intercept', 'cy:command']
};
}
if (env.enableExtendedCollector == '1') {
config.enableExtendedCollector = true;
}
Expand Down
4 changes: 2 additions & 2 deletions test/output/out.spec.always.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
{
"type": "cy:xhr",
"severity": "success",
"message": "GET https://jsonplaceholder.cypress.io/comments/1 (361 ms)\nStatus: 200 - OK"
"message": "GET https://jsonplaceholder.cypress.io/comments/1\nStatus: 200 - OK"
},
{
"type": "cy:command",
Expand Down Expand Up @@ -54,7 +54,7 @@
{
"type": "cy:xhr",
"severity": "success",
"message": "POST https://jsonplaceholder.cypress.io/comments (395 ms)\nStatus: 201 - Created"
"message": "POST https://jsonplaceholder.cypress.io/comments\nStatus: 201 - Created"
},
{
"type": "cy:command",
Expand Down
4 changes: 2 additions & 2 deletions test/output/out.spec.always.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ cypress/integration/happyFlow.spec.js:
cy:command (K): visit /commands/network-requests
cy:command (K): get .network-btn
cy:command (K): click
cy:xhr (K): GET https://jsonplaceholder.cypress.io/comments/1 (361 ms)
cy:xhr (K): GET https://jsonplaceholder.cypress.io/comments/1
Status: 200 - OK
cy:command (K): wait @getComment
cy:route (K): (getComment) GET https://jsonplaceholder.cypress.io/comments/1
Expand All @@ -19,7 +19,7 @@ cypress/integration/happyFlow.spec.js:
cy:command (K): assert expected **200** to equal **200**
cy:command (K): get .network-post
cy:command (K): click
cy:xhr (K): POST https://jsonplaceholder.cypress.io/comments (395 ms)
cy:xhr (K): POST https://jsonplaceholder.cypress.io/comments
Status: 201 - Created
cy:command (K): wait @postComment
cy:route (K): (postComment) POST https://jsonplaceholder.cypress.io/comments
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 @@ -19,7 +19,7 @@
{
"type": "cy:xhr",
"severity": "success",
"message": "GET https://jsonplaceholder.cypress.io/comments/1 (578 ms)\nStatus: 200 - OK"
"message": "GET https://jsonplaceholder.cypress.io/comments/1\nStatus: 200 - OK"
},
{
"type": "cy:command",
Expand Down Expand Up @@ -54,7 +54,7 @@
{
"type": "cy:xhr",
"severity": "success",
"message": "POST https://jsonplaceholder.cypress.io/comments (357 ms)\nStatus: 201 - Created"
"message": "POST https://jsonplaceholder.cypress.io/comments\nStatus: 201 - Created"
},
{
"type": "cy:command",
Expand Down
6 changes: 3 additions & 3 deletions test/output/out.spec.onFail.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ cypress/integration/happyFlow.spec.js:
cy:command (K): visit /commands/network-requests
cy:command (K): get .network-btn
cy:command (K): click
cy:xhr (K): GET https://jsonplaceholder.cypress.io/comments/1 (492 ms)
cy:xhr (K): GET https://jsonplaceholder.cypress.io/comments/1
Status: 200 - OK
cy:command (K): wait @getComment
cy:route (K): (getComment) GET https://jsonplaceholder.cypress.io/comments/1
Expand All @@ -19,7 +19,7 @@ cypress/integration/happyFlow.spec.js:
cy:command (K): assert expected **200** to equal **200**
cy:command (K): get .network-post
cy:command (K): click
cy:xhr (K): POST https://jsonplaceholder.cypress.io/comments (424 ms)
cy:xhr (K): POST https://jsonplaceholder.cypress.io/comments
Status: 201 - Created
cy:command (K): wait @postComment
cy:route (K): (postComment) POST https://jsonplaceholder.cypress.io/comments
Expand Down Expand Up @@ -71,7 +71,7 @@ cypress/integration/happyFlow.spec.js:
cons:debug (K): This should console.debug appear.
cy:command (K): get .network-put
cy:command (K): click
cy:xhr (!): STUBBED PUT https://jsonplaceholder.cypress.io/comments/1 (507 ms)
cy:xhr (!): STUBBED PUT https://jsonplaceholder.cypress.io/comments/1 (100 ms)
Status: 404 - Not Found
cy:command (K): wait @putComment
cy:route (!): (putComment) PUT https://jsonplaceholder.cypress.io/comments/1
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 @@ -34,7 +34,7 @@
{
"type": "cy:xhr",
"severity": "success",
"message": "GET https://jsonplaceholder.cypress.io/comments/1 (471 ms)\nStatus: 200 - OK"
"message": "GET https://jsonplaceholder.cypress.io/comments/1\nStatus: 200 - OK"
},
{
"type": "cy:command",
Expand Down Expand Up @@ -91,7 +91,7 @@
{
"type": "cy:xhr",
"severity": "success",
"message": "POST https://jsonplaceholder.cypress.io/comments (394 ms)\nStatus: 201 - Created"
"message": "POST https://jsonplaceholder.cypress.io/comments\nStatus: 201 - Created"
},
{
"type": "cy:command",
Expand Down Expand Up @@ -125,4 +125,4 @@
}
]
}
}
}
4 changes: 2 additions & 2 deletions test/output_nested_cucumber_spec/txt/cucumber/Happy.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ cypress/integration/cucumber/Happy.feature:
cy:command (K): step **I can load comments**
cy:command (K): get .network-btn
cy:command (K): click
cy:xhr (K): GET https://jsonplaceholder.cypress.io/comments/1 (471 ms)
cy:xhr (K): GET https://jsonplaceholder.cypress.io/comments/1
Status: 200 - OK
cy:command (K): wait @getComment
cy:route (K): (getComment) GET https://jsonplaceholder.cypress.io/comments/1
Expand All @@ -28,7 +28,7 @@ cypress/integration/cucumber/Happy.feature:
cy:command (K): step **I can post comment**
cy:command (K): get .network-post
cy:command (K): click
cy:xhr (K): POST https://jsonplaceholder.cypress.io/comments (394 ms)
cy:xhr (K): POST https://jsonplaceholder.cypress.io/comments
Status: 201 - Created
cy:command (K): wait @postComment
cy:route (K): (postComment) POST https://jsonplaceholder.cypress.io/comments
Expand Down
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 @@ -19,7 +19,7 @@
{
"type": "cy:xhr",
"severity": "success",
"message": "GET https://jsonplaceholder.cypress.io/comments/1 (521 ms)\nStatus: 200 - OK"
"message": "GET https://jsonplaceholder.cypress.io/comments/1\nStatus: 200 - OK"
},
{
"type": "cy:command",
Expand Down Expand Up @@ -54,7 +54,7 @@
{
"type": "cy:xhr",
"severity": "success",
"message": "POST https://jsonplaceholder.cypress.io/comments (317 ms)\nStatus: 201 - Created"
"message": "POST https://jsonplaceholder.cypress.io/comments\nStatus: 201 - Created"
},
{
"type": "cy:command",
Expand Down Expand Up @@ -218,4 +218,4 @@
}
]
}
}
}
4 changes: 2 additions & 2 deletions test/output_nested_spec/txt/happyFlow.spec.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ cypress/integration/happyFlow.spec.js:
cy:command (K): visit /commands/network-requests
cy:command (K): get .network-btn
cy:command (K): click
cy:xhr (K): GET https://jsonplaceholder.cypress.io/comments/1 (521 ms)
cy:xhr (K): GET https://jsonplaceholder.cypress.io/comments/1
Status: 200 - OK
cy:command (K): wait @getComment
cy:route (K): (getComment) GET https://jsonplaceholder.cypress.io/comments/1
Expand All @@ -19,7 +19,7 @@ cypress/integration/happyFlow.spec.js:
cy:command (K): assert expected **200** to equal **200**
cy:command (K): get .network-post
cy:command (K): click
cy:xhr (K): POST https://jsonplaceholder.cypress.io/comments (317 ms)
cy:xhr (K): POST https://jsonplaceholder.cypress.io/comments
Status: 201 - Created
cy:command (K): wait @postComment
cy:route (K): (postComment) POST https://jsonplaceholder.cypress.io/comments
Expand Down
12 changes: 6 additions & 6 deletions test/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"@babel/preset-env": "^7.4.5",
"@babel/register": "^7.13.16",
"chai": "^4.2.0",
"cypress": "8.1.0",
"cypress": "8.3.0",
"cypress-cucumber-preprocessor": "^4.0.1",
"fs-extra": "^9.0.1",
"glob": "^7.1.6",
Expand Down
Loading

0 comments on commit e77a385

Please sign in to comment.