Skip to content

Commit

Permalink
fix: accept various spec formats in API call (#119)
Browse files Browse the repository at this point in the history
Resolves #118
  • Loading branch information
agoldis committed Apr 6, 2023
1 parent 8e58421 commit fc3818e
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 2 deletions.
4 changes: 4 additions & 0 deletions examples/webapp/scripts/currents-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import { run } from "cypress-cloud";
recordKey,
});

if (result?.status === "failed") {
process.exit(1);
}

assert(result?.totalPassed === 1);
assert(result?.totalTests === 1);
})();
2 changes: 1 addition & 1 deletion packages/cypress-cloud/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export type { CurrentsRunAPI } from "./types";
* Run Cypress tests with a cloud service of your choice and return the results
*
* @augments CurrentsRunAPI
* @returns {CypressCommandLine.CypressRunResult | undefined} The test results, or undefined if no tests were run
* @returns {CypressCommandLine.CypressRunResult | CypressCommandLine.CypressFailedRunResult | undefined} The test results, or undefined if no tests were run
*/
export function run(params?: CurrentsRunAPI) {
return internalRun(params);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { describe, expect, it } from "@jest/globals";
import { preprocessParams } from "../params";
// generate unit tess for preprocessParams

describe("preprocessParams", () => {
it.each([
[
"should return params with spec as string",
{ spec: "test1,test2" },
["test1", "test2"],
],
[
"should return params for an array of comma-separated strings",
{ spec: ["test1,test2", "test3,test4"] },
["test1", "test2", "test3", "test4"],
],
[
"should return params with spec as array",
{ spec: ["test1", "test2"] },
["test1", "test2"],
],
[
"should return params with spec as single string",
{ spec: "test1" },
["test1"],
],
[
"should return params with undefined spec",
{ spec: undefined },
undefined,
],
["should return params with nullspec", { spec: null }, undefined],
["should return params with empty", { spec: [] }, []],
])("%s", (title, params, expected) => {
// @ts-expect-error
const result = preprocessParams(params);
expect(result.spec).toEqual(expected);
});
});
23 changes: 23 additions & 0 deletions packages/cypress-cloud/lib/config/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,26 @@ export function getCypressRunAPIParams(
record: false,
};
}

export function preprocessParams(
params: CurrentsRunParameters
): CurrentsRunParameters {
return {
...params,
spec: processSpecParam(params.spec),
};
}

function processSpecParam(
spec: CurrentsRunParameters["spec"]
): string[] | undefined {
if (!spec) {
return undefined;
}

if (Array.isArray(spec)) {
return _.flatten(spec.map((i) => i.split(",")));
}

return spec.split(",");
}
9 changes: 8 additions & 1 deletion packages/cypress-cloud/lib/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import { CurrentsRunParameters } from "../types";
import { createRun } from "./api";
import { cutInitialOutput } from "./capture";
import { getCI } from "./ciProvider";
import { getMergedConfig, isOffline, validateParams } from "./config";
import {
getMergedConfig,
isOffline,
preprocessParams,
validateParams,
} from "./config";
import { runBareCypress } from "./cypress";
import { getGitInfo } from "./git";
import { setAPIBaseUrl, setRunId } from "./httpClient";
Expand All @@ -19,6 +24,8 @@ const debug = Debug("currents:run");

export async function run(params: CurrentsRunParameters = {}) {
debug("run params %o", params);
params = preprocessParams(params);
debug("params after preprocess %o", params);

if (isOffline(params)) {
info(`Skipping cloud orchestration because --record is set to false`);
Expand Down

0 comments on commit fc3818e

Please sign in to comment.