Skip to content

Commit

Permalink
Fixed Cypress exit code logic
Browse files Browse the repository at this point in the history
  • Loading branch information
sergii-nosachenko committed Jan 24, 2024
1 parent 878da65 commit 5c12c03
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
22 changes: 19 additions & 3 deletions mate-scripts/src/services/Cypress.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path';
import { execBashCodeAsync } from '../tools';
import { execBashCodeAsyncWithOutput } from '../tools';

export interface StartedServer {
stop: () => void;
Expand Down Expand Up @@ -83,10 +83,18 @@ export class CypressService {
? ` --config baseUrl=http://localhost:${startedServer.port}`
: '';

await execBashCodeAsync(
const { stdout, stderr, exitCode } = await execBashCodeAsyncWithOutput(
`${this.binDir}cypress run${baseUrl}`,
{ shouldBindStdout: this.shouldShowLogs },
);

// Cypress can return non-zero exit code
// because some tests are failed
if (exitCode !== 0) {
if (!stdout.includes('(Run Finished)')) {
throw new Error(stderr);
}
}
} catch (e2eError) {
errors.push(e2eError);
} finally {
Expand All @@ -98,10 +106,18 @@ export class CypressService {

if (this.shouldRunComponents) {
try {
await execBashCodeAsync(
const { stdout, stderr, exitCode } = await execBashCodeAsyncWithOutput(
`${this.binDir}cypress run-ct`,
{ shouldBindStdout: this.shouldShowLogs },
);

// Cypress can return non-zero exit code
// because some tests are failed
if (exitCode !== 0) {
if (!stdout.includes('(Run Finished)')) {
throw new Error(stderr);
}
}
} catch (componentsError) {
errors.push(componentsError);
}
Expand Down
55 changes: 55 additions & 0 deletions mate-scripts/src/tools/execBashCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ export type ExecResult<F extends boolean> = F extends true
? ChildProcess
: string;

type ExecBashCodeSyncOuputResult = {
stdout: string;
stderr: string;
exitCode: number;
}

export function execBashCodeSync(
bashCode: string,
shouldBindStdout = true,
Expand Down Expand Up @@ -89,6 +95,55 @@ export function execBashCodeAsync(
}));
}

export function execBashCodeAsyncWithOutput(
bashCode: string,
params: ExecBashCodeAsyncParams = defaultExecBashCodeAsyncParams,
): Promise<ExecBashCodeSyncOuputResult> {
const {
shouldBindStdout = true,
cwd,
} = params;

return new Promise<ExecBashCodeSyncOuputResult>(((resolve, reject) => {
const execOptions = { cwd };
const childProcess = exec(bashCode, execOptions);
let stdout = '';
let stderr = '';

if (childProcess.stdout) {
childProcess.stdout.on('data', (data) => {
stdout += data.toString();

if (shouldBindStdout) {
console.log(data);
}
});
}

if (childProcess.stderr) {
childProcess.stderr.on('data', (data) => {
stderr += data.toString();

if (shouldBindStdout) {
console.error(data);
}
});
}

childProcess.on('close', (code) => {
resolve({
stdout,
stderr,
exitCode: code as number,
});
});

childProcess.on('error', (error) => {
reject(new Error(`${bashCode}, error: ${error}`));
});
}));
}

export function execBashCodeControlled(
bashCode: string,
shouldBindStdout = true,
Expand Down

0 comments on commit 5c12c03

Please sign in to comment.