From 947610c28cb195ceeec51170ae5472303cd13478 Mon Sep 17 00:00:00 2001 From: Dmitrii Gridnev Date: Mon, 29 Jul 2024 15:48:37 +0200 Subject: [PATCH] fix: issues with sending results and completing a test run - fixed an issue with the reporter completing the test run before all results are sent to Qase - fixed an issue with the reporter not sending all results to Qase Fixes #640 #558 --- qase-cypress/README.md | 73 ++++++++++++++++++++++++++---------- qase-cypress/changelog.md | 19 ++++++++++ qase-cypress/package.json | 12 ++++-- qase-cypress/src/child.js | 16 ++++++-- qase-cypress/src/hooks.ts | 48 ++++++++++++++++++++++++ qase-cypress/src/plugin.js | 11 ++++++ qase-cypress/src/reporter.ts | 29 +++++++++----- 7 files changed, 171 insertions(+), 37 deletions(-) create mode 100644 qase-cypress/src/hooks.ts create mode 100644 qase-cypress/src/plugin.js diff --git a/qase-cypress/README.md b/qase-cypress/README.md index efdee1d7..a55577ef 100644 --- a/qase-cypress/README.md +++ b/qase-cypress/README.md @@ -8,19 +8,52 @@ To install the latest version, run: npm install -D cypress-qase-reporter ``` -## Updating from v1 +## Updating from v1 to v2.1 -You can update a test project from using version 1 to version 2 in several steps: +You can update a test project from using version 1 to version 2.1 in several steps: -1. Change import paths: +1. Change import paths: - ```diff - - import { qase } from 'cypress-qase-reporter/dist/mocha' - + import { qase } from 'cypress-qase-reporter/mocha' - ``` + ```diff + - import { qase } from 'cypress-qase-reporter/dist/mocha' + + import { qase } from 'cypress-qase-reporter/mocha' + ``` +2. Update reporter configuration in `cypress.config.js` and/or environment variables — + see the [configuration reference](#configuration) below. -2. Update reporter configuration in `cypress.config.js` and/or environment variables — - see the [configuration reference](#configuration) below. +3. Set the hooks in the `e2e` section in `cypress.config.js`: + + ```diff + ... + e2e: { + setupNodeEvents(on, config) { + + require('cypress-qase-reporter/plugin')(on, config); + } + } + ... + ``` + + If you are override before:run or after:run hooks, use this: + + ```diff + const { beforeRunHook, afterRunHook } = require('cypress-qase-reporter/hooks'); + + ... + e2e: { + setupNodeEvents(on, config) { + + on('before:run', async () => { + + console.log('override before:run'); + + await beforeRunHook(config); + + }); + + + on('after:run', async () => { + + console.log('override after:run'); + + await afterRunHook(config); + + }); + }, + }, + ... + ``` ## Getting started @@ -41,20 +74,20 @@ import { qase } from 'cypress-qase-reporter/mocha'; describe('My First Test', () => { qase(1, - it('Several ids', () => { - expect(true).to.equal(true); - }) + it('Several ids', () => { + expect(true).to.equal(true); + }) ); // a test can check multiple test cases qase([2, 3], - it('Correct test', () => { - expect(true).to.equal(true); - }) + it('Correct test', () => { + expect(true).to.equal(true); + }) ); qase(4, - it.skip('Skipped test', () => { - expect(true).to.equal(true); - }) + it.skip('Skipped test', () => { + expect(true).to.equal(true); + }) ); }); ``` @@ -86,11 +119,13 @@ https://app.qase.io/run/QASE_PROJECT_CODE ## Configuration Qase Cypress reporter can be configured in multiple ways: + - by adding configuration block in `cypress.config.js`, - using a separate config file `qase.config.json`, - using environment variables (they override the values from the configuration files). -For a full list of configuration options, see the [Configuration reference](../qase-javascript-commons/README.md#configuration). +For a full list of configuration options, see +the [Configuration reference](../qase-javascript-commons/README.md#configuration). Example `cypress.config.js` config: diff --git a/qase-cypress/changelog.md b/qase-cypress/changelog.md index 66412502..842c51c5 100644 --- a/qase-cypress/changelog.md +++ b/qase-cypress/changelog.md @@ -1,3 +1,22 @@ +# cypress-qase-reporter@2.1.0-beta.1 + +## What's new + +- fixed an issue with the reporter completing the test run before all results are sent to Qase +- fixed an issue with the reporter not sending all results to Qase + +Need to add the following to the `cypress.config.js` file: + +```diff +... + e2e: { + setupNodeEvents(on, config) { ++ require('cypress-qase-reporter/plugin')(on, config); + } + } +... +``` + # cypress-qase-reporter@2.0.3 ## What's new diff --git a/qase-cypress/package.json b/qase-cypress/package.json index c558ad63..a7fa4b8d 100644 --- a/qase-cypress/package.json +++ b/qase-cypress/package.json @@ -1,6 +1,6 @@ { "name": "cypress-qase-reporter", - "version": "2.0.3", + "version": "2.1.0-beta.1", "description": "Qase Cypress Reporter", "homepage": "https://github.com/qase-tms/qase-javascript", "sideEffects": false, @@ -10,7 +10,8 @@ ".": "./dist/index.js", "./mocha": "./dist/mocha.js", "./reporter": "./dist/reporter.js", - "./package.json": "./package.json" + "./package.json": "./package.json", + "./plugin": "./dist/plugin.js" }, "typesVersions": { "*": { @@ -44,7 +45,7 @@ "author": "Qase Team ", "license": "Apache-2.0", "dependencies": { - "qase-javascript-commons": "^2.0.0", + "qase-javascript-commons": "~2.1.0-beta.1", "uuid": "^9.0.1" }, "peerDependencies": { @@ -58,5 +59,8 @@ "jest": "^29.5.0", "mocha": "^10.2.0", "ts-jest": "^29.1.0" - } + }, + "files": [ + "plugin.js" + ] } diff --git a/qase-cypress/src/child.js b/qase-cypress/src/child.js index b659e032..263247cc 100644 --- a/qase-cypress/src/child.js +++ b/qase-cypress/src/child.js @@ -1,7 +1,15 @@ +import { QaseReporter } from 'qase-javascript-commons'; + + +const options = JSON.parse(process.env?.reporterConfig); +const results = JSON.parse(process.env?.results); + + const runChild = async () => { - setTimeout(() => { - // do nothing - }, 10000); -} + const reporter = QaseReporter.getInstance(options); + reporter.setTestResults(results); + + await reporter.publish(); +}; runChild(); diff --git a/qase-cypress/src/hooks.ts b/qase-cypress/src/hooks.ts new file mode 100644 index 00000000..e9fe49aa --- /dev/null +++ b/qase-cypress/src/hooks.ts @@ -0,0 +1,48 @@ +/// + +import { composeOptions, ConfigLoader, QaseReporter } from 'qase-javascript-commons'; +import { configSchema } from './configSchema'; +import PluginConfigOptions = Cypress.PluginConfigOptions; + +async function beforeRunHook(options: PluginConfigOptions) { + const configLoader = new ConfigLoader(configSchema); + const config = configLoader.load(); + const { reporterOptions } = options; + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment + const { ...composedOptions } = composeOptions(reporterOptions['cypressQaseReporterReporterOptions'], config); + + // eslint-disable-next-line @typescript-eslint/no-unsafe-argument + const reporter = QaseReporter.getInstance({ + ...composedOptions, + frameworkPackage: 'cypress', + frameworkName: 'cypress', + reporterName: 'cypress-qase-reporter', + }); + + // eslint-disable-next-line @typescript-eslint/no-unsafe-call + await reporter.startTestRunAsync(); +} + +async function afterRunHook(options: PluginConfigOptions) { + + const configLoader = new ConfigLoader(configSchema); + const config = configLoader.load(); + const { reporterOptions } = options; + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment + const { ...composedOptions } = composeOptions(reporterOptions['cypressQaseReporterReporterOptions'], config); + + // eslint-disable-next-line @typescript-eslint/no-unsafe-argument + const reporter = QaseReporter.getInstance({ + ...composedOptions, + frameworkPackage: 'cypress', + frameworkName: 'cypress', + reporterName: 'cypress-qase-reporter', + }); + + await reporter.complete(); +} + +module.exports = { + beforeRunHook, + afterRunHook, +}; diff --git a/qase-cypress/src/plugin.js b/qase-cypress/src/plugin.js new file mode 100644 index 00000000..149cd69e --- /dev/null +++ b/qase-cypress/src/plugin.js @@ -0,0 +1,11 @@ +import { afterRunHook, beforeRunHook } from './hooks'; + +module.exports = function (on, config) { + on('before:run', async () => { + await beforeRunHook(config); + }); + + on('after:run', async () => { + await afterRunHook(config); + }); +}; diff --git a/qase-cypress/src/reporter.ts b/qase-cypress/src/reporter.ts index 8071aefa..2babb63b 100644 --- a/qase-cypress/src/reporter.ts +++ b/qase-cypress/src/reporter.ts @@ -9,7 +9,11 @@ import { QaseReporter, ReporterInterface, TestStatusEnum, - composeOptions, TestResultType, Attachment, + composeOptions, + TestResultType, + Attachment, + FrameworkOptionsType, + ConfigType, } from 'qase-javascript-commons'; import { traverseDir } from './utils/traverse-dir'; @@ -21,7 +25,6 @@ const { EVENT_TEST_PASS, EVENT_TEST_PENDING, EVENT_RUN_END, - EVENT_RUN_BEGIN, } = Runner.constants; type CypressState = 'failed' | 'passed' | 'pending'; @@ -104,6 +107,8 @@ export class CypressQaseReporter extends reporters.Base { */ private reporter: ReporterInterface; + private options: Omit<(FrameworkOptionsType<'cypress', ReporterOptionsType> & ConfigType & ReporterOptionsType & NonNullable) | (null & ReporterOptionsType & NonNullable), 'framework'>; + /** * @param {Runner} runner * @param {CypressQaseOptionsType} options @@ -121,6 +126,7 @@ export class CypressQaseReporter extends reporters.Base { const { framework, ...composedOptions } = composeOptions(reporterOptions, config); this.screenshotsFolder = framework?.cypress?.screenshotsFolder; + this.options = composedOptions; this.reporter = QaseReporter.getInstance({ ...composedOptions, @@ -142,13 +148,16 @@ export class CypressQaseReporter extends reporters.Base { runner.on(EVENT_TEST_FAIL, (test: Test) => this.addTestResult(test)); // eslint-disable-next-line @typescript-eslint/no-misused-promises - runner.on(EVENT_RUN_BEGIN, () => this.reporter.startTestRun()); - - // eslint-disable-next-line @typescript-eslint/no-misused-promises - runner.once(EVENT_RUN_END, async () => { - await this.reporter.publish(); - - spawnSync('node', [`${__dirname}/child.js`], { stdio: 'inherit' }); + runner.once(EVENT_RUN_END, () => { + const results = this.reporter.getResults(); + + spawnSync('node', [`${__dirname}/child.js`], { + stdio: 'inherit', + env: Object.assign(process.env, { + reporterConfig: JSON.stringify(this.options), + results: JSON.stringify(results), + }), + }); }); } @@ -191,7 +200,7 @@ export class CypressQaseReporter extends reporters.Base { run_id: null, signature: this.getSignature(test, ids), steps: [], - id: test.id, + id: uuidv4(), execution: { status: test.state ? CypressQaseReporter.statusMap[test.state]