Skip to content

Commit

Permalink
Add option to run commands with exit code expectation
Browse files Browse the repository at this point in the history
This is in preperation of testing message merging.
  • Loading branch information
badeball committed Dec 17, 2023
1 parent d3ed34d commit d53c18f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
1 change: 1 addition & 0 deletions features/support/ICustomWorld.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export interface ExtraOptions {
extraArgs?: string[];
extraEnv?: Record<string, string>;
expectedExitCode?: number;
}

export default interface ICustomWorld {
Expand Down
32 changes: 29 additions & 3 deletions features/support/world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ export default class CustomWorld implements ICustomWorld {
}
| undefined;

runCypress({ extraArgs = [], extraEnv = {} }: ExtraOptions = {}) {
runCypress({
extraArgs = [],
extraEnv = {},
expectedExitCode,
}: ExtraOptions = {}) {
return this.runCommand({
cmd: path.join(
projectPath,
Expand All @@ -46,28 +50,36 @@ export default class CustomWorld implements ICustomWorld {
NO_COLOR: "1",
...extraEnv,
},
expectedExitCode,
});
}

runDiagnostics({ extraArgs = [], extraEnv = {} }: ExtraOptions = {}) {
runDiagnostics({
extraArgs = [],
extraEnv = {},
expectedExitCode,
}: ExtraOptions = {}) {
return this.runCommand({
cmd: "node",
args: [
path.join(projectPath, bin["cypress-cucumber-diagnostics"]),
...extraArgs,
],
extraEnv,
expectedExitCode,
});
}

async runCommand({
cmd,
args = [],
extraEnv = {},
expectedExitCode,
}: {
cmd: string;
args: string[];
extraEnv: Record<string, string>;
expectedExitCode?: number;
}) {
const child = childProcess.spawn(cmd, args, {
stdio: ["ignore", "pipe", "pipe"],
Expand Down Expand Up @@ -97,7 +109,21 @@ export default class CustomWorld implements ICustomWorld {
const stderr = stderrBuffer.getContentsAsString() || "";
const output = outputBuffer.getContentsAsString() || "";

this.verifiedLastRunError = false;
if (expectedExitCode != null) {
if (exitCode !== expectedExitCode) {
if (exitCode === 0) {
throw new Error(`Last run passed unexpectedly. Output:\n\n${output}`);
} else {
throw new Error(
`Last run errored unexpectedly. Output:\n\n${output}`
);
}
} else {
this.verifiedLastRunError = true;
}
} else {
this.verifiedLastRunError = false;
}

this.lastRun = {
stdout,
Expand Down

0 comments on commit d53c18f

Please sign in to comment.