Skip to content

Commit

Permalink
fix: 🐛 Allow to turn off logging
Browse files Browse the repository at this point in the history
  • Loading branch information
NoriSte committed Oct 30, 2019
1 parent 2983652 commit 238f33b
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 23 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ Pass in an options object to change the default behavior of `cy.waitUntil()`.
| `interval` | `200` | Time to wait between the `checkFunction` invocations. |
| `description` | `waitUntil` | The name logged into the Cypress Test Runner. |
| `logger` | `Cypress.log` | A custom logger in place of the default [Cypress.log](https://docs.cypress.io/api/cypress-api/cypress-log.html). It's useful just for debugging purposes. |
| `log` | `true` | Enable/disable logging. |

<br />
<br />
Expand Down
13 changes: 13 additions & 0 deletions cypress/integration/plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,17 @@ context("Cypress Wait Until", () => {
expect(lastCallArgs).deep.include({ name: description });
});
});

it("Should allow to turn off logging", () => {
const checkFunction = () => true;

const logger = {
log: (...params) => Cypress.log(...params)
};
const spy = cy.spy(logger, "log");

cy.waitUntil(checkFunction, { logger: logger.log, log: false }).then(() => {
expect(spy).not.to.have.been.called;
});
});
});
112 changes: 96 additions & 16 deletions cypress/types/plugin.spec.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,97 @@
/// <reference types="Cypress" />
cy.waitUntil(function () { return true; });
cy.waitUntil(function () { return false; });
cy.waitUntil(function () { return Promise.resolve(true); });
cy.waitUntil(function () { return Promise.resolve(false); });
cy.waitUntil(function () { return true; }, {});
cy.waitUntil(function () { return false; }, {});
cy.waitUntil(function () { return Promise.resolve(true); }, {});
cy.waitUntil(function () { return Promise.resolve(false); }, {});
cy.waitUntil(function () { return true; }, { timeout: 500 });
cy.waitUntil(function () { return false; }, { timeout: 500 });
cy.waitUntil(function () { return Promise.resolve(true); }, { timeout: 500 });
cy.waitUntil(function () { return Promise.resolve(false); }, { timeout: 500 });
cy.waitUntil(function () { return true; }, { errorMsg: "Custom error message" });
cy.waitUntil(function () { return false; }, { errorMsg: "Custom error message" });
cy.waitUntil(function () { return Promise.resolve(true); }, { errorMsg: "Custom error message" });
cy.waitUntil(function () { return Promise.resolve(false); }, { errorMsg: "Custom error message" });
cy.waitUntil(function() {
return true;
});
cy.waitUntil(function() {
return false;
});
cy.waitUntil(function() {
return Promise.resolve(true);
});
cy.waitUntil(function() {
return Promise.resolve(false);
});
cy.waitUntil(function() {
return true;
}, {});
cy.waitUntil(function() {
return false;
}, {});
cy.waitUntil(function() {
return Promise.resolve(true);
}, {});
cy.waitUntil(function() {
return Promise.resolve(false);
}, {});
cy.waitUntil(
function() {
return true;
},
{ timeout: 500 }
);
cy.waitUntil(
function() {
return false;
},
{ timeout: 500 }
);
cy.waitUntil(
function() {
return Promise.resolve(true);
},
{ timeout: 500 }
);
cy.waitUntil(
function() {
return Promise.resolve(false);
},
{ timeout: 500 }
);
cy.waitUntil(
function() {
return true;
},
{ errorMsg: "Custom error message" }
);
cy.waitUntil(
function() {
return false;
},
{ errorMsg: "Custom error message" }
);
cy.waitUntil(
function() {
return Promise.resolve(true);
},
{ errorMsg: "Custom error message" }
);
cy.waitUntil(
function() {
return Promise.resolve(false);
},
{ errorMsg: "Custom error message" }
);
cy.waitUntil(
function() {
return true;
},
{ description: "Custom description" }
);
cy.waitUntil(
function() {
return true;
},
{
logger: ({ name, message, consoleProps }) => {
console.log({ name, message, consoleProps });
}
}
);
cy.waitUntil(
function() {
return true;
},
{
log: false
}
);
2 changes: 2 additions & 0 deletions cypress/types/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ cy.waitUntil(() => true, {
console.log({ name, message, consoleProps });
}
});

cy.waitUntil(() => true, { log: false });
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ interface WaitUntilOptions {
errorMsg?: string;
description?: string;
logger?: (WaitUntilLog) => any;
log?: boolean;
}

declare namespace Cypress {
Expand Down
17 changes: 10 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,20 @@ function waitUntil(subject, checkFunction, options) {
const TIMEOUT = options.timeout || 5000;
const ERROR_MSG = options.errorMsg || "Timed out retrying";
const LOG_DESCRIPTION = options.description || "waitUntil";
const LOG = options.log === false ? false : true;
let retries = Math.floor(TIMEOUT / TIMEOUT_INTERVAL);

const logger = options.logger || Cypress.log;

logger({
name: LOG_DESCRIPTION,
message: options,
consoleProps: () => ({
options
})
});
if (LOG) {
logger({
name: LOG_DESCRIPTION,
message: options,
consoleProps: () => ({
options
})
});
}

const check = result => {
if (result) {
Expand Down

0 comments on commit 238f33b

Please sign in to comment.