Skip to content

Commit

Permalink
feature: the ability to specify a test metadata in tests
Browse files Browse the repository at this point in the history
- `qase.title` - set the test title
- `qase.fields` - set the test fields
- `qase.suite` - set the test suite
- `qase.comment` - set the test comment
- `qase.parameters` - set the test parameters
- `qase.groupParameters` - set the test group parameters
- `qase.ignore` - ignore the test in Qase

```ts
it('test', () => {
  qase.title('Title');
  qase.fields({ field: 'value' });
  qase.suite('Suite');
  qase.comment('Comment');
  qase.parameters({ param: 'value' });
  qase.groupParameters({ group: { param: 'value' } });
  qase.ignore();

  cy.visit('https://example.com');
});
  • Loading branch information
gibiw committed Sep 16, 2024
1 parent b9c7af5 commit 1c53099
Show file tree
Hide file tree
Showing 9 changed files with 387 additions and 18 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 33 additions & 6 deletions qase-cypress/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Publish results simple and easy.

## Installation

To install the latest release version (2.0.x), run:
To install the latest release version (2.1.x), run:

```sh
npm install -D cypress-qase-reporter
Expand All @@ -13,7 +13,7 @@ npm install -D cypress-qase-reporter
<!-- if there's no current beta, comment the next block
-->

To install the latest beta version (2.1.x), run:
To install the latest beta version (2.2.x), run:

```sh
npm install -D cypress-qase-reporter@beta
Expand All @@ -30,7 +30,7 @@ run the following steps:
- 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.

Expand Down Expand Up @@ -68,6 +68,23 @@ run the following steps:
...
```

## Updating from v2.1 to v2.2

To update an existing test project using Qase reporter from version 2.1 to version 2.2,
run the following steps:

1. Add a metadata in the `e2e` section of `cypress.config.js`

```diff
...
e2e: {
setupNodeEvents(on, config) {
require('cypress-qase-reporter/plugin')(on, config)
+ require('cypress-qase-reporter/metadata')(on)
}
}
...

## Getting started

The Cypress reporter can auto-generate test cases
Expand All @@ -80,6 +97,16 @@ from Qase.io before executing tests. It's a more reliable way to bind
autotests to test cases, that persists when you rename, move, or
parameterize your tests.

### Metadata

- `qase.title` - set the title of the test case
- `qase.fields` - set the fields of the test case
- `qase.suite` - set the suite of the test case
- `qase.comment` - set the comment of the test case
- `qase.parameters` - set the parameters of the test case
- `qase.groupParameters` - set the group parameters of the test case
- `qase.ignore` - ignore the test case in Qase. The test will be executed, but the results will not be sent to Qase.

For example:

```typescript
Expand All @@ -88,6 +115,7 @@ import { qase } from 'cypress-qase-reporter/mocha';
describe('My First Test', () => {
qase(1,
it('Several ids', () => {
qase.title('My title');
expect(true).to.equal(true);
})
);
Expand Down Expand Up @@ -145,8 +173,6 @@ Example `cypress.config.js` config:
```js
import cypress from 'cypress';

import plugins from './cypress/plugins/index.js';

module.exports = cypress.defineConfig({
reporter: 'cypress-multi-reporters',
reporterOptions: {
Expand Down Expand Up @@ -180,7 +206,8 @@ module.exports = cypress.defineConfig({
video: false,
e2e: {
setupNodeEvents(on, config) {
return plugins(on, config);
require('cypress-qase-reporter/plugin')(on, config)
require('cypress-qase-reporter/metadata')(on)
},
},
});
Expand Down
34 changes: 31 additions & 3 deletions qase-cypress/changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
# [email protected]

## What's new

Added the ability to specify a test metadata in tests:

- `qase.title` - set the test title
- `qase.fields` - set the test fields
- `qase.suite` - set the test suite
- `qase.comment` - set the test comment
- `qase.parameters` - set the test parameters
- `qase.groupParameters` - set the test group parameters
- `qase.ignore` - ignore the test in Qase

```ts
it('test', () => {
qase.title('Title');
qase.fields({ field: 'value' });
qase.suite('Suite');
qase.comment('Comment');
qase.parameters({ param: 'value' });
qase.groupParameters({ group: { param: 'value' } });
qase.ignore();

cy.visit('https://example.com');
});
```

# [email protected]

## What's new
Expand Down Expand Up @@ -40,8 +68,8 @@ The reporter will wait for all results to be sent to Qase and will not block the

## What's new

1. Cypress kills the process after the last tests.
The reporter will wait for all results to be sent to Qase and will not block the process after sending.
1. Cypress kills the process after the last tests.
The reporter will wait for all results to be sent to Qase and will not block the process after sending.

2. The reporter will collect suites and add them to results.

Expand All @@ -66,7 +94,7 @@ For more information about the new features and a guide for migration from v1, r

# [email protected]

Fixed an issue with multiple test runs created when Cypress is running
Fixed an issue with multiple test runs created when Cypress is running
multiple tests in parallel.

# [email protected]
Expand Down
3 changes: 2 additions & 1 deletion qase-cypress/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cypress-qase-reporter",
"version": "2.1.1",
"version": "2.2.0-beta.1",
"description": "Qase Cypress Reporter",
"homepage": "https://github.com/qase-tms/qase-javascript",
"sideEffects": false,
Expand All @@ -12,6 +12,7 @@
"./reporter": "./dist/reporter.js",
"./package.json": "./package.json",
"./plugin": "./dist/plugin.js",
"./metadata": "./dist/metadata.js",
"./hooks": "./dist/hooks.js"
},
"typesVersions": {
Expand Down
52 changes: 52 additions & 0 deletions qase-cypress/src/metadata.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { MetadataManager } from './metadata/manager';

module.exports = function(on) {
on('task', {
qaseTitle(value) {
MetadataManager.setTitle(value);
return null;
},
});

on('task', {
qaseFields(value) {
MetadataManager.setFields(value);
return null;
},
});

on('task', {
qaseIgnore() {
MetadataManager.setIgnore();
return null;
},
});

on('task', {
qaseParameters(value) {
MetadataManager.setParameters(value);
return null;
},
});

on('task', {
qaseGroupParameters(value) {
MetadataManager.setGroupParams(value);
return null;
},
});

on('task', {
qaseSuite(value) {
MetadataManager.setSuite(value);
return null;
},
});

on('task', {
qaseComment(value) {
MetadataManager.setComment(value);
return null;
},
});
};
101 changes: 101 additions & 0 deletions qase-cypress/src/metadata/manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { Metadata } from './models';
import { readFileSync, existsSync, unlinkSync, writeFileSync } from 'fs';

const metadataPath = 'qaseMetadata';

// eslint-disable-next-line @typescript-eslint/no-extraneous-class
export class MetadataManager {
public static getMetadata(): Metadata | undefined {
if (!this.isExists()) {
return undefined;
}

let metadata: Metadata = {
title: undefined,
fields: {},
parameters: {},
groupParams: {},
ignore: false,
suite: undefined,
comment: undefined,
};

try {
const data = readFileSync(metadataPath, 'utf8');
metadata = JSON.parse(data) as Metadata;

return metadata;
} catch (err) {
console.error('Error reading metadata file:', err);
}

return undefined;
}

public static setIgnore(): void {
const metadata = this.getMetadata() ?? {};
metadata.ignore = true;
this.setMetadata(metadata);
}

public static setSuite(suite: string): void {
const metadata = this.getMetadata() ?? {};
metadata.suite = suite;
this.setMetadata(metadata);
}

public static setComment(comment: string): void {
const metadata = this.getMetadata() ?? {};
metadata.comment = comment;
this.setMetadata(metadata);
}

public static setTitle(title: string): void {
const metadata = this.getMetadata() ?? {};
metadata.title = title;
this.setMetadata(metadata);
}

public static setFields(fields: Record<string, string>): void {
const metadata = this.getMetadata() ?? {};
metadata.fields = fields;
this.setMetadata(metadata);
}

public static setParameters(parameters: Record<string, string>): void {
const metadata = this.getMetadata() ?? {};
metadata.parameters = parameters;
this.setMetadata(metadata);
}

public static setGroupParams(groupParams: Record<string, string>): void {
const metadata = this.getMetadata() ?? {};
metadata.groupParams = groupParams;
this.setMetadata(metadata);
}

private static setMetadata(metadata: Metadata): void {
try {
const data = JSON.stringify(metadata);
writeFileSync(metadataPath, data);
} catch (err) {
console.error('Error writing metadata file:', err);
}
}

public static clear() {
if (!this.isExists()) {
return;
}

try {
unlinkSync(metadataPath);
} catch (err) {
console.error('Error clearing state file:', err);
}
}

static isExists(): boolean {
return existsSync(metadataPath);
}
}
9 changes: 9 additions & 0 deletions qase-cypress/src/metadata/models.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface Metadata {
title?: string | undefined;
fields?: Record<string, string>;
parameters?: Record<string, string>;
groupParams?: Record<string, string>;
ignore?: boolean;
suite?: string | undefined;
comment?: string | undefined;
}
Loading

0 comments on commit 1c53099

Please sign in to comment.