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..5f076b2 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: originalOptions }) + : options.errorMsg; throw new Error(msg); } cy.wait(options.interval, { log: false }).then(() => {