From a7ab3592a378acd5f307c7c0a5000ea52411f26d Mon Sep 17 00:00:00 2001 From: Dmitrii Gridnev Date: Fri, 12 Jul 2024 14:44:14 +0200 Subject: [PATCH] feature: parallel execution Support parallel execution of tests. --- qase-mocha/README.md | 27 +++++++++++++++++++++++++++ qase-mocha/changelog.md | 6 ++++++ qase-mocha/package.json | 2 +- qase-mocha/src/parallel.ts | 20 ++++++++++++++++++++ qase-mocha/src/reporter.ts | 11 +++++++++-- 5 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 qase-mocha/src/parallel.ts diff --git a/qase-mocha/README.md b/qase-mocha/README.md index c43735f9..30885431 100644 --- a/qase-mocha/README.md +++ b/qase-mocha/README.md @@ -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: diff --git a/qase-mocha/changelog.md b/qase-mocha/changelog.md index 1c85bd70..ece20759 100644 --- a/qase-mocha/changelog.md +++ b/qase-mocha/changelog.md @@ -1,3 +1,9 @@ +# qase-mocha@1.0.0-beta.2 + +## What's new + +Support parallel execution of tests. + # qase-mocha@1.0.0-beta.1 ## What's new diff --git a/qase-mocha/package.json b/qase-mocha/package.json index 95642221..16d1add7 100644 --- a/qase-mocha/package.json +++ b/qase-mocha/package.json @@ -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, diff --git a/qase-mocha/src/parallel.ts b/qase-mocha/src/parallel.ts new file mode 100644 index 00000000..f49635a2 --- /dev/null +++ b/qase-mocha/src/parallel.ts @@ -0,0 +1,20 @@ + +import type * as Mocha from "mocha"; +// eslint-disable-next-line @typescript-eslint/ban-ts-comment +// @ts-ignore +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; +}; diff --git a/qase-mocha/src/reporter.ts b/qase-mocha/src/reporter.ts index ec110456..9b5125c7 100644 --- a/qase-mocha/src/reporter.ts +++ b/qase-mocha/src/reporter.ts @@ -13,6 +13,7 @@ import { TestStepType, } from 'qase-javascript-commons'; import deasyncPromise from 'deasync-promise'; +import { extname, join } from 'node:path'; const Events = Runner.constants; @@ -24,6 +25,9 @@ class currentTest { 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 { /** @@ -65,8 +69,11 @@ export class MochaQaseReporter extends reporters.Base { reporterName: 'mocha-qase-reporter', }); - - this.applyListeners(); + if (options.parallel) { + options.require = [...(options.require ?? []), resolveParallelModeSetupFile()]; + } else { + this.applyListeners(); + } } private applyListeners = () => {