Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Mocha parallel execution #636

Merged
merged 1 commit into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions qase-mocha/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,33 @@ A test run will be performed and available at:
https://app.qase.io/run/QASE_PROJECT_CODE
```

### Parallel execution

The reporter supports parallel execution of tests.

First, you need to create a new run in Qase.io. You can use
the [Qase CLI](https://github.com/qase-tms/qasectl):

```bash
# Create a new test run
qli testops run create --project DEMO --token token --title 'Mocha test run'

# Save the run ID to the environment variable
export QASE_TESTOPS_RUN_ID=$(< qase.env grep QASE_TESTOPS_RUN_ID | cut -d'=' -f2)
```

Then, you can run tests in parallel:

```bash
QASE_MODE=testops mocha --parallel
```

After the tests are finished, you can complete the run:

```bash
qli testops run complete --project DEMO --token token --run $(echo $QASE_TESTOPS_RUN_ID)
```

## Configuration

Qase Mocha reporter can be configured in multiple ways:
Expand Down
6 changes: 6 additions & 0 deletions qase-mocha/changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# [email protected]

## What's new

Support parallel execution of tests.

# [email protected]

## What's new
Expand Down
2 changes: 1 addition & 1 deletion qase-mocha/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mocha-qase-reporter",
"version": "1.0.0-beta.1",
"version": "1.0.0-beta.2",
"description": "Mocha Cypress Reporter",
"homepage": "https://github.com/qase-tms/qase-javascript",
"sideEffects": false,
Expand Down
20 changes: 20 additions & 0 deletions qase-mocha/src/parallel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

import type * as Mocha from "mocha";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore

Check warning on line 4 in qase-mocha/src/parallel.ts

View workflow job for this annotation

GitHub Actions / Project qase-mocha - Node 16

Use "@ts-expect-error" to ensure an error is actually being suppressed

Check warning on line 4 in qase-mocha/src/parallel.ts

View workflow job for this annotation

GitHub Actions / Project qase-mocha - Node 18

Use "@ts-expect-error" to ensure an error is actually being suppressed
import { default as ParallelBuffered } from "mocha/lib/nodejs/reporters/parallel-buffered.js";
import { MochaQaseReporter } from "./reporter.js";

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const originalCreateListeners: (runner: Mocha.Runner) => Mocha.reporters.Base =
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
ParallelBuffered.prototype.createListeners;

// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
ParallelBuffered.prototype.createListeners = function (runner: Mocha.Runner) {
console.log("createListeners");
const result = originalCreateListeners.call(this, runner);
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
new MochaQaseReporter(runner, this.options as Mocha.MochaOptions);
return result;
};
11 changes: 9 additions & 2 deletions qase-mocha/src/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
TestStepType,
} from 'qase-javascript-commons';
import deasyncPromise from 'deasync-promise';
import { extname, join } from 'node:path';

const Events = Runner.constants;

Expand All @@ -24,6 +25,9 @@
attachments: Attachment[] = [];
}

// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-return,@typescript-eslint/restrict-template-expressions
const resolveParallelModeSetupFile = () => join(__dirname, `parallel${extname(__filename)}`);

export class MochaQaseReporter extends reporters.Base {

/**
Expand Down Expand Up @@ -65,8 +69,11 @@
reporterName: 'mocha-qase-reporter',
});


this.applyListeners();
if (options.parallel) {
options.require = [...(options.require ?? []), resolveParallelModeSetupFile()];
} else {
this.applyListeners();
}
}

private applyListeners = () => {
Expand Down Expand Up @@ -177,7 +184,7 @@

if (file) {
const executionPath = process.cwd() + '/';
const path = file.replace(executionPath, '') ?? '';

Check warning on line 187 in qase-mocha/src/reporter.ts

View workflow job for this annotation

GitHub Actions / Project qase-mocha - Node 16

Unnecessary conditional, expected left-hand side of `??` operator to be possibly null or undefined

Check warning on line 187 in qase-mocha/src/reporter.ts

View workflow job for this annotation

GitHub Actions / Project qase-mocha - Node 18

Unnecessary conditional, expected left-hand side of `??` operator to be possibly null or undefined
signature = path.split('/').join('::');
}

Expand Down
Loading