Skip to content

Commit

Permalink
Add support for fetching test suite configuration from graphql
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanjduffy committed Jul 25, 2023
1 parent 928af8c commit 555801c
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 6 deletions.
9 changes: 7 additions & 2 deletions packages/cypress/src/mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,17 @@ function getRepeatCount(stressCount = 10) {
}
}

export function getDiagnosticConfig(config: Cypress.PluginConfigOptions): {
export function getDiagnosticConfig(
config: Cypress.PluginConfigOptions,
extraEnv: NodeJS.ProcessEnv = {}
): {
noRecord: boolean;
env: NodeJS.ProcessEnv;
} {
let noRecord = false;
let env: NodeJS.ProcessEnv = {};
let env: NodeJS.ProcessEnv = {
...extraEnv,
};

const { cypress_repeat_k } = config.env;
const repeatIndex = cypress_repeat_k ? Number.parseInt(cypress_repeat_k) : undefined;
Expand Down
9 changes: 5 additions & 4 deletions packages/cypress/src/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ReplayReporter,
TestMetadataV2,
ReporterError,
fetchWorkspaceConfig,
} from "@replayio/test-utils";
import debug from "debug";

Expand Down Expand Up @@ -59,12 +60,12 @@ class CypressReporter {

async authenticate(apiKey: string) {
this.reporter.setApiKey(apiKey);
// TODO: we can fetch diagnostics from the cloud later here
this.configureDiagnostics();
const { env } = await fetchWorkspaceConfig(apiKey);
this.configureDiagnostics(env);
}

configureDiagnostics() {
this.diagnosticConfig = getDiagnosticConfig(this.config);
configureDiagnostics(extraEnv?: NodeJS.ProcessEnv) {
this.diagnosticConfig = getDiagnosticConfig(this.config, extraEnv);

// Mix diagnostic env into process env so it can be picked up by test
// metrics and reported to telemetry
Expand Down
68 changes: 68 additions & 0 deletions packages/test-utils/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import fetch from "node-fetch";
import dbg from "debug";

const debug = dbg("replay:test-utils:config");

async function query(apiKey: string, name: string, query: string, variables = {}) {
const options = {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${apiKey}`,
},
body: JSON.stringify({
query,
name,
variables,
}),
};

const server = process.env.REPLAY_API_SERVER || "https://api.replay.io";
debug("Querying %s graphql endpoint", server);
const result = await fetch(`${server}/v1/graphql`, options);

return await result.json();
}

async function fetchWorkspaceConfig(apiKey: string) {
const json = await query(
apiKey,
"GetWorkspaceConfig",
`
query GetWorkspaceConfig {
auth {
workspaces {
edges {
node {
id
settings {
features
}
}
}
}
}
}
`
);

if (json.errors) {
debug("GraphQL failed: %s", json.errors);
throw new Error(json.errors[0].message || "Unexpected error");
}

const edges = json.data?.auth.workspaces.edges;
if (!edges || edges.length !== 1) {
debug("Failed to find workspace: %o", json.data);
throw new Error("Failed to find a workspace for the given apiKey");
}

debug("Workspace settings: %o", edges[0].node.settings);
const features = edges[0].node.settings.features;

return {
env: features?.testSuites?.env || {},
};
}

export { fetchWorkspaceConfig };
1 change: 1 addition & 0 deletions packages/test-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ export type { TestMetadataV1, TestMetadataV2, ReplayReporterConfig } from "./rep
export { ReporterError } from "./reporter";
export { pingTestMetrics } from "./metrics";
export { removeAnsiCodes } from "./terminal";
export { fetchWorkspaceConfig } from "./config";
export { ReplayReporter };
export { getMetadataFilePath, initMetadataFile } from "./metadata";

0 comments on commit 555801c

Please sign in to comment.