From f8a5ad3569f84d3777155404dd44025e49e8541d Mon Sep 17 00:00:00 2001 From: Phil Burrows Date: Thu, 30 Apr 2020 10:35:02 -0500 Subject: [PATCH] implement requested changes from PR feedback --- cypress/types/plugin.spec.ts | 13 ++++++++++++- index.d.ts | 9 +++++++-- src/index.js | 5 ++++- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/cypress/types/plugin.spec.ts b/cypress/types/plugin.spec.ts index 4b27808..eb49850 100644 --- a/cypress/types/plugin.spec.ts +++ b/cypress/types/plugin.spec.ts @@ -18,7 +18,18 @@ cy.waitUntil(() => Promise.resolve(false), { timeout: 500 }); cy.waitUntil(() => true, { errorMsg: "Custom error message" }); cy.waitUntil(() => false, { errorMsg: "Custom error message" }); cy.waitUntil(() => Promise.resolve(true), { errorMsg: "Custom error message" }); -cy.waitUntil(() => Promise.resolve(false), { errorMsg: "Custom error message" }); +cy.waitUntil(() => Promise.resolve(false), { + errorMsg: "Custom error message", +}); + +cy.waitUntil(() => true, { errorMsg: () => "Custom error message" }); +cy.waitUntil(() => false, { errorMsg: () => "Custom error message" }); +cy.waitUntil(() => Promise.resolve(true), { + errorMsg: () => "Custom error message", +}); +cy.waitUntil(() => Promise.resolve(false), { + errorMsg: () => "Custom error message", +}); cy.waitUntil(() => true, { description: "Custom description" }); diff --git a/index.d.ts b/index.d.ts index 548e939..5f67523 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,11 +1,16 @@ /// -type WaitUntilLog = Pick; +type WaitUntilLog = Pick< + Cypress.LogConfig, + "name" | "message" | "consoleProps" +>; + +type ErrorMsgCallback = (result: any, options: any) => string; interface WaitUntilOptions { timeout?: number; interval?: number; - errorMsg?: string; + errorMsg?: string | ErrorMsgCallback; description?: string; customMessage?: string; verbose?: boolean; diff --git a/src/index.js b/src/index.js index 21ff917..201de48 100644 --- a/src/index.js +++ b/src/index.js @@ -57,7 +57,10 @@ const waitUntil = (subject, checkFunction, originalOptions = {}) => { return result; } if (retries < 1) { - const msg = options.errorMsg.call ? options.errorMsg() : options.errorMsg; + const msg = + options.errorMsg instanceof Function + ? options.errorMsg({ result, options }) + : options.errorMsg; throw new Error(msg); } cy.wait(options.interval, { log: false }).then(() => {