Skip to content

Commit

Permalink
fix: issues with sending results and completing a test run
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
gibiw committed Jul 30, 2024
1 parent 5f2ad7c commit 57dc8b5
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 37 deletions.
73 changes: 54 additions & 19 deletions qase-cypress/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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);
})
);
});
```
Expand Down Expand Up @@ -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:

Expand Down
19 changes: 19 additions & 0 deletions qase-cypress/changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
# [email protected]

## 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);
}
}
...
```

# [email protected]

## What's new
Expand Down
12 changes: 8 additions & 4 deletions qase-cypress/package.json
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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": {
"*": {
Expand Down Expand Up @@ -44,7 +45,7 @@
"author": "Qase Team <[email protected]>",
"license": "Apache-2.0",
"dependencies": {
"qase-javascript-commons": "^2.0.0",
"qase-javascript-commons": "~2.1.0-beta.1",
"uuid": "^9.0.1"
},
"peerDependencies": {
Expand All @@ -58,5 +59,8 @@
"jest": "^29.5.0",
"mocha": "^10.2.0",
"ts-jest": "^29.1.0"
}
},
"files": [
"plugin.js"
]
}
16 changes: 12 additions & 4 deletions qase-cypress/src/child.js
Original file line number Diff line number Diff line change
@@ -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();
48 changes: 48 additions & 0 deletions qase-cypress/src/hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/// <reference types="cypress" />

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,
};
11 changes: 11 additions & 0 deletions qase-cypress/src/plugin.js
Original file line number Diff line number Diff line change
@@ -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);
});
};
29 changes: 19 additions & 10 deletions qase-cypress/src/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -21,7 +25,6 @@ const {
EVENT_TEST_PASS,
EVENT_TEST_PENDING,
EVENT_RUN_END,
EVENT_RUN_BEGIN,
} = Runner.constants;

type CypressState = 'failed' | 'passed' | 'pending';
Expand Down Expand Up @@ -104,6 +107,8 @@ export class CypressQaseReporter extends reporters.Base {
*/
private reporter: ReporterInterface;

private options: Omit<(FrameworkOptionsType<'cypress', ReporterOptionsType> & ConfigType & ReporterOptionsType & NonNullable<unknown>) | (null & ReporterOptionsType & NonNullable<unknown>), 'framework'>;

/**
* @param {Runner} runner
* @param {CypressQaseOptionsType} options
Expand All @@ -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,
Expand All @@ -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),
}),
});
});
}

Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 57dc8b5

Please sign in to comment.