Skip to content

Commit

Permalink
Merge pull request #1 from zhex900/master
Browse files Browse the repository at this point in the history
fix xhr.response.body.text is not a function
  • Loading branch information
archfz authored Feb 12, 2020
2 parents 42248bb + 61f0247 commit 26d19d8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 20 deletions.
7 changes: 7 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
trailingComma: "es5",
tabWidth: 2,
singleQuote: true,
printWidth: 100,
bracketSpacing: false,
};
61 changes: 41 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,27 @@ function pipeLogsToTerminal() {

Cypress.on('log:added', options => {
if (options.instrument === 'command' && options.consoleProps) {
if (options.name === 'log' || (options.name === 'task' && options.message.match(/terminalLogs/))) {
if (
options.name === 'log' ||
(options.name === 'task' && options.message.match(/terminalLogs/))
) {
return;
}
let detailMessage = '';
if (options.name === 'xhr') {
detailMessage = (options.consoleProps.Stubbed === 'Yes' ? 'STUBBED ' : '') + options.consoleProps.Method + ' ' + options.consoleProps.URL;
detailMessage =
(options.consoleProps.Stubbed === 'Yes' ? 'STUBBED ' : '') +
options.consoleProps.Method +
' ' +
options.consoleProps.URL;
}
const log = options.name + '\t' + options.message + (detailMessage !== '' ? ' ' + detailMessage : '');
logs.push(['cy:command', log])
const log =
options.name + '\t' + options.message + (detailMessage !== '' ? ' ' + detailMessage : '');
logs.push(['cy:command', log]);
}
});

Cypress.on('command:start', (command) => {
Cypress.on('command:start', command => {
if (command.get('name') !== 'server') {
return;
}
Expand All @@ -51,35 +59,50 @@ function pipeLogsToTerminal() {

cy.server({
...options,
onAnyResponse(route, xhr) {
async onAnyResponse(route, xhr) {
if (!route) {
return;
}

xhr.response.body.text().then(body => {
logs.push([String(xhr.status).match(/^2[0-9]+$/) ? 'cy:route:info' : 'cy:route:warn',
`Status: ${xhr.status} (${route.alias})\n\t\tMethod: ${xhr.method}\n\t\tUrl: ${xhr.url}\n\t\tResponse: ${body}`]);
});
}
logs.push([
String(xhr.status).match(/^2[0-9]+$/) ? 'cy:route:info' : 'cy:route:warn',
`Status: ${xhr.status} (${route.alias})\n\t\tMethod: ${xhr.method}\n\t\tUrl: ${
xhr.url
}\n\t\tResponse: ${await responseBodyParser(xhr.response.body)}`,
]);
},
});
});

Cypress.mocha.getRunner().on('test', () => {
logs = [];
});

afterEach(function () {
afterEach(function() {
if (this.currentTest.state !== 'passed') {
cy.task('terminalLogs', logs);
}
});
}

async function responseBodyParser(body) {
if (!body) {
return 'EMPTY_BODY';
} else if (typeof body === 'string') {
return body;
} else if (typeof body === 'object') {
if (typeof body.text === 'function') {
return await body.text();
}
return JSON.stringify(body);
}
return 'UNKNOWN_BODY';
}

function nodeAddLogsPrinter(on, options = {}) {
const chalk = require('chalk');

on('task', {
terminalLogs: (messages) => {
terminalLogs: messages => {
messages.forEach(([type, message], i) => {
let color,
typeString,
Expand Down Expand Up @@ -116,11 +139,11 @@ function nodeAddLogsPrinter(on, options = {}) {

if (i === messages.length - 1) {
color = 'red';
icon = '✘'
icon = '✘';
}

if (message.length > trim) {
processedMessage = message.substring(0, trim) + " ...";
processedMessage = message.substring(0, trim) + ' ...';
}

console.log(chalk[color](typeString + icon + ' '), processedMessage);
Expand All @@ -145,14 +168,12 @@ module.exports = {
* - defaultTrimLength?: Trim length for console and cy.log.
* - commandTrimLength?: Trim length for cy commands.
*/
installPlugin: (on, options= {}) =>
nodeAddLogsPrinter(on, options),
installPlugin: (on, options = {}) => nodeAddLogsPrinter(on, options),

/**
* Installs the logs collector for cypress.
*
* Needs to be added to support file.
*/
installSupport: () =>
pipeLogsToTerminal(),
installSupport: () => pipeLogsToTerminal(),
};

0 comments on commit 26d19d8

Please sign in to comment.