cypress-xray-plugin
Version:
A Cypress plugin for uploading test results to Xray (test management for Jira)
143 lines (136 loc) • 5.59 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CypressTaskListener = exports.PluginTask = void 0;
exports.enqueueTask = enqueueTask;
const util_1 = require("../hooks/after/util");
const base64_1 = require("../util/base64");
const dedent_1 = require("../util/dedent");
const errors_1 = require("../util/errors");
/**
* All tasks which are available within the plugin.
*/
// eslint-disable-next-line @typescript-eslint/naming-convention
exports.PluginTask = {
/**
* The task which handles incoming responses from requests dispatched through `cy.request`
* within a test.
*/
["INCOMING_RESPONSE"]: "cypress-xray-plugin:task:response",
/**
* The task that provides Xray iteration parameters for a test run. These can be used to
* distinguish between iterations in the execution results view.
*/
["ITERATION_DEFINITION"]: "cypress-xray-plugin:task:iteration:definition",
/**
* The task which handles outgoing requests dispatched through `cy.request` within a test.
*/
["OUTGOING_REQUEST"]: "cypress-xray-plugin:task:request",
};
function enqueueTask(task, ...args) {
switch (task) {
case "cypress-xray-plugin:task:request": {
// Cast valid because of overload.
const [filename, request] = args;
const taskParameters = {
filename: filename,
request: request,
test: Cypress.currentTest.titlePath.join(" "),
};
return cy.task(task, taskParameters);
}
case "cypress-xray-plugin:task:response": {
// Cast valid because of overload.
const [filename, response] = args;
const taskParameters = {
filename: filename,
response: response,
test: Cypress.currentTest.titlePath.join(" "),
};
return cy.task(task, taskParameters);
}
case "cypress-xray-plugin:task:iteration:definition": {
// Cast valid because of overload.
const [parameters] = args;
const taskParameters = {
parameters: parameters,
test: Cypress.currentTest.titlePath.join(" "),
};
return cy.task(task, taskParameters);
}
}
}
class CypressTaskListener {
constructor(projectKey, evidenceCollection, iterationParameterCollection, logger) {
this.ignoredTests = new Set();
this.projectKey = projectKey;
this.evidenceCollection = evidenceCollection;
this.iterationParameterCollection = iterationParameterCollection;
this.logger = logger;
}
["cypress-xray-plugin:task:request"](args) {
try {
const issueKeys = (0, util_1.getTestIssueKeys)(args.test, this.projectKey);
for (const issueKey of issueKeys) {
this.evidenceCollection.addEvidence(issueKey, {
contentType: "application/json",
data: (0, base64_1.encode)(JSON.stringify(args.request, null, 2)),
filename: args.filename,
});
}
}
catch (error) {
if (!this.ignoredTests.has(args.test)) {
this.logger.message("warning", (0, dedent_1.dedent)(`
Test: ${args.test}
Encountered a cy.request call which will not be included as evidence.
Caused by: ${(0, errors_1.errorMessage)(error)}
`));
this.ignoredTests.add(args.test);
}
}
return args.request;
}
["cypress-xray-plugin:task:response"](args) {
try {
const issueKeys = (0, util_1.getTestIssueKeys)(args.test, this.projectKey);
for (const issueKey of issueKeys) {
this.evidenceCollection.addEvidence(issueKey, {
contentType: "application/json",
data: (0, base64_1.encode)(JSON.stringify(args.response, null, 2)),
filename: args.filename,
});
}
}
catch (error) {
if (!this.ignoredTests.has(args.test)) {
this.logger.message("warning", (0, dedent_1.dedent)(`
Test: ${args.test}
Encountered a cy.request call which will not be included as evidence.
Caused by: ${(0, errors_1.errorMessage)(error)}
`));
this.ignoredTests.add(args.test);
}
}
return args.response;
}
["cypress-xray-plugin:task:iteration:definition"](args) {
try {
const issueKeys = (0, util_1.getTestIssueKeys)(args.test, this.projectKey);
for (const issueKey of issueKeys) {
this.iterationParameterCollection.setIterationParameters(issueKey, args.test, args.parameters);
}
}
catch (error) {
if (!this.ignoredTests.has(args.test)) {
this.logger.message("warning", (0, dedent_1.dedent)(`
Test: ${args.test}
Encountered an iteration definition task call which cannot be mapped to a test.
Caused by: ${(0, errors_1.errorMessage)(error)}
`));
this.ignoredTests.add(args.test);
}
}
return args.parameters;
}
}
exports.CypressTaskListener = CypressTaskListener;