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

feat: allow report generation when using the cypress module api #2

Merged
merged 4 commits into from
Feb 16, 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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ Simply run your Cypress tests as usual. The plugin will automatically generate a
cypress run
```

## API Usage

If you are using the Cypress module API, you can pass the Cypress results to the plugin directly:

```javascript
const cypress = require('cypress')
const {createJUnitReport} = require('@saucelabs/cypress-junit-plugin')

cypress.run({
reporter: 'spec',
browser: 'chrome',
}).then(r => {
createJUnitReport(r, { filename: 'path/to/my_junit.xml' });
})
```

## Contributing

Contributions to the `@saucelabs/cypress-junit-plugin` are welcome! Check out our contributing guidelines for more information on how to participate.
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "@saucelabs/cypress-junit-plugin",
"version": "0.1.0",
"description": "Sauce Cypress JUnit Plugin",
"main": "lib/index.js",
"scripts": {
Expand Down Expand Up @@ -54,6 +55,5 @@
"release-it": "^17.0.3",
"rimraf": "^5.0.5",
"typescript": "^5.3.3"
},
"version": "0.1.0"
}
}
39 changes: 36 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import BeforeRunDetails = Cypress.BeforeRunDetails;
import PluginConfigOptions = Cypress.PluginConfigOptions;
import PluginEvents = Cypress.PluginEvents;
import Spec = Cypress.Spec;
import { ConfigOption } from './type';
import Reporter, { TestCase, TestSuite } from './reporter';
import { ConfigOptions } from './type';
import { Reporter, TestCase, TestSuite } from './reporter';

let reporter: Reporter;

Expand Down Expand Up @@ -121,10 +121,14 @@ function parseErrorType(err: string): string {
return '';
}

/**
* Set up the JUnit plugin for Cypress. A JUnit report will be generated at the
* end of the Cypress run.
*/
export function setupJUnitPlugin(
on: PluginEvents,
config: PluginConfigOptions,
opts?: ConfigOption,
opts?: ConfigOptions,
) {
reporter = new Reporter(
opts || {
Expand All @@ -142,3 +146,32 @@ export function setupJUnitPlugin(
on('after:spec', onAfterSpec);
return config;
}

/**
* Create a JUnit report from the results of a Cypress run. You can use this
* function when running Cypress via its module API.
* If you are using the Cypress CLI, call `setupJUnitPlugin()` from your config
* file instead.
*/
export function createJUnitReport(
results: CypressRunResult | CypressFailedRunResult,
opts?: ConfigOptions,
) {
reporter = new Reporter(
opts || {
filename: 'junit.xml',
},
);

if (isFailedRunResult(results)) {
onAfterRun(results);
return;
}

reporter.setSuiteName(`Cypress Test - ${results.browserName}`);
results.runs.forEach((run) => {
onAfterSpec(run.spec, run);
});

onAfterRun(results);
}
8 changes: 4 additions & 4 deletions src/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
Failure,
Error,
Property,
ConfigOption,
ConfigOptions,
} from './type';
import { create } from 'xmlbuilder2';

Expand Down Expand Up @@ -55,11 +55,11 @@ export class TestCase implements JUnitTestCase {
}
}

export default class Reporter {
export class Reporter {
public rootSuite: JUnitTestSuite;
private opts: ConfigOption;
private opts: ConfigOptions;

constructor(opts: ConfigOption) {
constructor(opts: ConfigOptions) {
this.rootSuite = new TestSuite();
this.opts = opts;
}
Expand Down
2 changes: 1 addition & 1 deletion src/type.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Defines the structure for configuration options.
*/
export interface ConfigOption {
export interface ConfigOptions {
// Name/path of the JUnit file.
filename: string;
}
Expand Down