Skip to content

Commit

Permalink
Add healthcheck function to avoid breaking tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan-Carvalheiro committed Sep 30, 2022
1 parent 0e6369e commit 8c6ac9c
Showing 1 changed file with 47 additions and 26 deletions.
73 changes: 47 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const {
getAgentInfo, getCodeRef, getFullTestName, getFullStepName,
} = require('./utils/objectUtils');

const testItemStatuses = { PASSED: 'passed', FAILED: 'failed', SKIPPED: 'pending' };
const testItemStatuses = {PASSED: 'passed', FAILED: 'failed', SKIPPED: 'pending'};
const logLevels = {
ERROR: 'error',
TRACE: 'trace',
Expand All @@ -37,21 +37,39 @@ const promiseErrorHandler = (promise) => {
});
};

async function healthCheck(rpClient) {
await rpClient.checkConnect().then((response) => {
console.log('You have successfully connected to the server.');
console.log(`You are using an account: ${response.fullName}`);
}, (error) => {
throw error;
console.log('Error connection to server');
console.dir(error);
});
}

class JestReportPortal {
constructor(globalConfig, options) {
const agentInfo = getAgentInfo();
this.reportOptions = getClientInitObject(getOptions.options(options));
if (this.reportOptions.enabled === 'false') return {invalid: true};
this.client = new RPClient(this.reportOptions, agentInfo);
this.tempSuiteIds = new Map();
this.tempTestIds = new Map();
this.tempStepId = null;
this.promises = [];
this.stop = false;
}

// eslint-disable-next-line no-unused-vars
onRunStart() {
async onRunStart() {
try {
await healthCheck(this.client);
} catch (e) {
this.stop = true;
return {invalid: true}
}
const startLaunchObj = getStartLaunchObject(this.reportOptions);
const { tempId, promise } = this.client.startLaunch(startLaunchObj);
const {tempId, promise} = this.client.startLaunch(startLaunchObj);

this.tempLaunchId = tempId;
promiseErrorHandler(promise);
Expand All @@ -60,6 +78,7 @@ class JestReportPortal {

// eslint-disable-next-line no-unused-vars
onTestResult(test, testResult) {
if (this.stop) return {invalid: true};
let suiteDuration = 0;
let testDuration = 0;
for (let result = 0; result < testResult.testResults.length; result++) {
Expand Down Expand Up @@ -101,9 +120,11 @@ class JestReportPortal {

// eslint-disable-next-line no-unused-vars
async onRunComplete() {
if (this.stop) return {invalid: true};
;
await Promise.all(this.promises);
if (this.reportOptions.launchId) return;
const { promise } = this.client.finishLaunch(this.tempLaunchId);
const {promise} = this.client.finishLaunch(this.tempLaunchId);

if (this.reportOptions.logLaunchLink === true) {
promise.then((response) => {
Expand All @@ -120,7 +141,7 @@ class JestReportPortal {
return;
}
const codeRef = getCodeRef(path, suiteName);
const { tempId, promise } = this.client.startTestItem(getSuiteStartObject(suiteName, codeRef, suiteDuration),
const {tempId, promise} = this.client.startTestItem(getSuiteStartObject(suiteName, codeRef, suiteDuration),
this.tempLaunchId);

this.tempSuiteIds.set(suiteName, tempId);
Expand All @@ -140,7 +161,7 @@ class JestReportPortal {
test.ancestorTitles[test.ancestorTitles.length - 1], codeRef, testDuration,
);
const parentId = this.tempTestIds.get(test.ancestorTitles.slice(0, -1).join('/')) || tempSuiteId;
const { tempId, promise } = this.client.startTestItem(testStartObj, this.tempLaunchId, parentId);
const {tempId, promise} = this.client.startTestItem(testStartObj, this.tempLaunchId, parentId);

this.tempTestIds.set(fullTestName, tempId);
promiseErrorHandler(promise);
Expand All @@ -154,7 +175,7 @@ class JestReportPortal {
const stepDuration = test.duration;
const stepStartObj = getStepStartObject(test.title, isRetried, codeRef, stepDuration);
const parentId = this.tempTestIds.get(test.ancestorTitles.join('/')) || tempSuiteId;
const { tempId, promise } = this.client.startTestItem(stepStartObj, this.tempLaunchId, parentId);
const {tempId, promise} = this.client.startTestItem(stepStartObj, this.tempLaunchId, parentId);

this.tempStepId = tempId;
promiseErrorHandler(promise);
Expand All @@ -165,33 +186,33 @@ class JestReportPortal {
const errorMsg = test.failureMessages[0];

switch (test.status) {
case testItemStatuses.PASSED:
this._finishPassedStep(isRetried);
break;
case testItemStatuses.FAILED:
this._finishFailedStep(errorMsg, isRetried);
break;
default:
this._finishSkippedStep(isRetried);
case testItemStatuses.PASSED:
this._finishPassedStep(isRetried);
break;
case testItemStatuses.FAILED:
this._finishFailedStep(errorMsg, isRetried);
break;
default:
this._finishSkippedStep(isRetried);
}
}

_finishPassedStep(isRetried) {
const status = testItemStatuses.PASSED;
const finishTestObj = { status, retry: isRetried };
const { promise } = this.client.finishTestItem(this.tempStepId, finishTestObj);
const finishTestObj = {status, retry: isRetried};
const {promise} = this.client.finishTestItem(this.tempStepId, finishTestObj);

promiseErrorHandler(promise);
this.promises.push(promise);
}

_finishFailedStep(failureMessage, isRetried) {
const status = testItemStatuses.FAILED;
const finishTestObj = { status, retry: isRetried };
const finishTestObj = {status, retry: isRetried};

this._sendLog(failureMessage);

const { promise } = this.client.finishTestItem(this.tempStepId, finishTestObj);
const {promise} = this.client.finishTestItem(this.tempStepId, finishTestObj);

promiseErrorHandler(promise);
this.promises.push(promise);
Expand All @@ -202,21 +223,21 @@ class JestReportPortal {
message,
level: logLevels.ERROR,
};
const { promise } = this.client.sendLog(this.tempStepId, logObject);
const {promise} = this.client.sendLog(this.tempStepId, logObject);

promiseErrorHandler(promise);
this.promises.push(promise);
}

_finishSkippedStep(isRetried) {
const status = 'skipped';
const issue = this.reportOptions.skippedIssue === false ? { issueType: 'NOT_ISSUE' } : null;
const issue = this.reportOptions.skippedIssue === false ? {issueType: 'NOT_ISSUE'} : null;
const finishTestObj = {
status,
retry: isRetried,
...(issue && { issue }),
...(issue && {issue}),
};
const { promise } = this.client.finishTestItem(this.tempStepId, finishTestObj);
const {promise} = this.client.finishTestItem(this.tempStepId, finishTestObj);

promiseErrorHandler(promise);
this.promises.push(promise);
Expand All @@ -225,7 +246,7 @@ class JestReportPortal {
_finishTest(tempTestId, key) {
if (!tempTestId) return;

const { promise } = this.client.finishTestItem(tempTestId, {});
const {promise} = this.client.finishTestItem(tempTestId, {});

this.tempTestIds.delete(key);
promiseErrorHandler(promise);
Expand All @@ -235,7 +256,7 @@ class JestReportPortal {
_finishSuite(tempSuiteId, key) {
if (!tempSuiteId) return;

const { promise } = this.client.finishTestItem(tempSuiteId, {});
const {promise} = this.client.finishTestItem(tempSuiteId, {});

this.tempSuiteIds.delete(key);
promiseErrorHandler(promise);
Expand Down

0 comments on commit 8c6ac9c

Please sign in to comment.