Skip to content

Commit

Permalink
feat: 🎸 Allow even more logging customization
Browse files Browse the repository at this point in the history
  • Loading branch information
NoriSte committed Oct 30, 2019
1 parent 238f33b commit ed852c3
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 9 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,15 @@ A function that must return a truthy value when the wait is over.

Pass in an options object to change the default behavior of `cy.waitUntil()`.

| Option | Default | Description |
| ------------- | -------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `errorMsg` | `Timed out retrying` | The error message to write. |
| `timeout` | `5000` | Time to wait for the `checkFunction` to return a truthy value before throwing an error. |
| `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. |
| Option | Default | Description |
| --------------- | -------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `errorMsg` | `Timed out retrying` | The error message to write. |
| `timeout` | `5000` | Time to wait for the `checkFunction` to return a truthy value before throwing an error. |
| `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. |
| `customMessage` | `undefined` | String logged after the `options.description`. |

<br />
<br />
Expand Down
17 changes: 17 additions & 0 deletions cypress/integration/plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,23 @@ context("Cypress Wait Until", () => {
});
});

it("Should accept a custom message", () => {
const checkFunction = () => true;
const customMessage = "custom message";

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

cy.waitUntil(checkFunction, options).then(() => {
expect(spy).to.have.been.called;
const lastCallArgs = spy.lastCall.args[0];
expect(lastCallArgs.message).deep.include(customMessage);
});
});

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

Expand Down
8 changes: 8 additions & 0 deletions cypress/types/plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,11 @@ cy.waitUntil(
log: false
}
);
cy.waitUntil(
function() {
return true;
},
{
customMessage: "custom message"
}
);
1 change: 1 addition & 0 deletions cypress/types/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ cy.waitUntil(() => true, {
});

cy.waitUntil(() => true, { log: false });
cy.waitUntil(() => true, { customMessage: "custom message" });
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ interface WaitUntilOptions {
interval?: number;
errorMsg?: string;
description?: string;
customMessage?: string;
logger?: (WaitUntilLog) => any;
log?: boolean;
}
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ function waitUntil(subject, checkFunction, options) {
const ERROR_MSG = options.errorMsg || "Timed out retrying";
const LOG_DESCRIPTION = options.description || "waitUntil";
const LOG = options.log === false ? false : true;
const MESSAGE = [options.customMessage, options].filter(Boolean);
let retries = Math.floor(TIMEOUT / TIMEOUT_INTERVAL);

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

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

0 comments on commit ed852c3

Please sign in to comment.