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 66e0a8d
Show file tree
Hide file tree
Showing 14 changed files with 148 additions and 32 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
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
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
16 changes: 15 additions & 1 deletion test/specs/extendedController.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ describe('Extended controller.', () => {
});
}).timeout(60000);

it('Should work correctly with skip.', async function () {
it('Should work correctly with dynamic skip.', async function () {
await runTest(commandBase(['enableExtendedCollector=1'], ['dynamicSkip.spec.js']), (error, stdout, stderr) => {
expect(clean(stdout)).to.contain(`- test3
cy:log ${ICONS.info} before
Expand All @@ -200,4 +200,18 @@ describe('Extended controller.', () => {
cy:log ${ICONS.info} test3 3`);
});
}).timeout(60000);

it('Should work correctly with skipped tests.', async function () {
await runTest(commandBase(['enableExtendedCollector=1'], ['skipTest.spec.js']), (error, stdout, stderr) => {
expect(clean(stdout, true)).to.contain(` Describe 1
- Skipped test 1
✓ Test 2
Describe 2
- Skipped test 2
- Skipped test 3
✓ Test 3
✓ Test 4`);
});
}).timeout(60000);
});
16 changes: 15 additions & 1 deletion test/specs/misc.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
ICONS,
runTest,
commandBase, logLastRun,
commandBase, logLastRun, clean,
} from "../utils";

const {expect} = require('chai');
Expand Down Expand Up @@ -75,4 +75,18 @@ describe('Misc.', () => {
});
}).timeout(30000);

it('Should work correctly with skipped tests.', async function () {
await runTest(commandBase([''], ['skipTest.spec.js']), (error, stdout, stderr) => {
expect(clean(stdout, true)).to.contain(` Describe 1
- Skipped test 1
✓ Test 2
Describe 2
- Skipped test 2
- Skipped test 3
✓ Test 3
✓ Test 4`);
});
}).timeout(60000);

});
6 changes: 6 additions & 0 deletions test/specs/validation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ describe('Validation.', () => {
});
}).timeout(60000);

it('Should not fail with proper config.', async () => {
await runTest(commandBase(['supportGoodConfig=1'], ['happyFlow.spec.js']), (error, stdout, stderr) => {
expect(stdout).to.not.contain(`cypress-terminal-report: Invalid plugin install options:`);
});
}).timeout(60000);

it('Should print proper validation error on invalid support install options.', async () => {
await runTest(commandBase(['supportBadConfig=1'], ['happyFlow.spec.js']), (error, stdout, stderr) => {
expect(stdout).to.contain(`cypress-terminal-report: Invalid plugin install options:`);
Expand Down

0 comments on commit 66e0a8d

Please sign in to comment.