Skip to content

Commit

Permalink
implement requested changes from PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
peburrows committed Apr 30, 2020
1 parent 1a62da2 commit 389cc4c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
13 changes: 12 additions & 1 deletion cypress/types/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" });

Expand Down
9 changes: 7 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
/// <reference types="Cypress" />

type WaitUntilLog = Pick<Cypress.LogConfig, "name" | "message" | "consoleProps">;
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;
Expand Down
21 changes: 14 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const logCommand = ({ options, originalOptions }) => {
options.logger({
name: options.description,
message: options.customMessage,
consoleProps: () => originalOptions
consoleProps: () => originalOptions,
});
}
};
Expand All @@ -19,13 +19,15 @@ const logCommandCheck = ({ result, options, originalOptions }) => {
options.logger({
name: options.description,
message,
consoleProps: () => originalOptions
consoleProps: () => originalOptions,
});
};

const waitUntil = (subject, checkFunction, originalOptions = {}) => {
if (!(checkFunction instanceof Function)) {
throw new Error("`checkFunction` parameter should be a function. Found: " + checkFunction);
throw new Error(
"`checkFunction` parameter should be a function. Found: " + checkFunction
);
}

const defaultOptions = {
Expand All @@ -40,24 +42,29 @@ const waitUntil = (subject, checkFunction, originalOptions = {}) => {
customMessage: undefined,
logger: Cypress.log,
verbose: false,
customCheckMessage: undefined
customCheckMessage: undefined,
};
const options = { ...defaultOptions, ...originalOptions };

// filter out a falsy passed "customMessage" value
options.customMessage = [options.customMessage, originalOptions].filter(Boolean);
options.customMessage = [options.customMessage, originalOptions].filter(
Boolean
);

let retries = Math.floor(options.timeout / options.interval);

logCommand({ options, originalOptions });

const check = result => {
const check = (result) => {
logCommandCheck({ result, options, originalOptions });
if (result) {
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(() => {
Expand Down

0 comments on commit 389cc4c

Please sign in to comment.