Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Commit

Permalink
bring init back under assignReporterMethods for exception suppressing (
Browse files Browse the repository at this point in the history
  • Loading branch information
VasilyStrelyaev committed Nov 29, 2022
1 parent 37c2dc0 commit 2a8355e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 78 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "testcafe-reporter-dashboard-devextreme",
"version": "1.3.1",
"version": "1.3.2",
"description": "Dashboard TestCafe reporter plugin fork, tailored to the DevExtreme team.",
"author": {
"name": "Developer Express Inc.",
Expand Down
49 changes: 24 additions & 25 deletions src/reporter-object-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,29 @@ export function reporterObjectFactory (
...BLANK_REPORTER,
createErrorDecorator: errorDecorator,

getReportUrl (): string {
return createReportUrl(buildId || id, dashboardUrl, authenticationToken);
}
};

async function uploadWarnings (): Promise<string | undefined> {
const warningsRunIds = Object.keys(testRunToWarningsMap);

if (!warningsRunIds.length && !runWarnings.length)
return void 0;

const warningsInfo: WarningsInfo[] = [];

for (const testRunId of warningsRunIds)
warningsInfo.push({ testRunId, warnings: testRunToWarningsMap[testRunId] });

if (runWarnings.length)
warningsInfo.push({ warnings: runWarnings });

return await uploader.uploadRunWarning(id, warningsInfo);
}

assignReporterMethods(reporterPluginObject, {
async init (): Promise<void> {
const validationResponse = await transport.fetchFromDashboard(
'api/validateReporter',
Expand All @@ -109,41 +132,17 @@ export function reporterObjectFactory (
const responseJson = await validationResponse.json() as DashboardInfo;

if (!responseJson)
throw new Error('Expected json DashboardInfo response');
logger.error('Expected json DashboardInfo response');

if (!validationResponse.ok) {
const errorMessage = responseJson.message ? responseJson.message : AUTHENTICATION_TOKEN_REJECTED;

logger.error(errorMessage);
throw new Error(errorMessage);
}

processDashboardWarnings(responseJson);
},

getReportUrl (): string {
return createReportUrl(buildId || id, dashboardUrl, authenticationToken);
}
};

async function uploadWarnings (): Promise<string | undefined> {
const warningsRunIds = Object.keys(testRunToWarningsMap);

if (!warningsRunIds.length && !runWarnings.length)
return void 0;

const warningsInfo: WarningsInfo[] = [];

for (const testRunId of warningsRunIds)
warningsInfo.push({ testRunId, warnings: testRunToWarningsMap[testRunId] });

if (runWarnings.length)
warningsInfo.push({ warnings: runWarnings });

return await uploader.uploadRunWarning(id, warningsInfo);
}

assignReporterMethods(reporterPluginObject, {
async reportTaskStart (startTime, userAgents, testCount, taskStructure: ReportedTestStructureItem[], taskProperties: TaskProperties): Promise<void> {
if (rejectReport) return;

Expand Down
52 changes: 0 additions & 52 deletions test/reporter-methods/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { reporterObjectFactory } from '../../src/reporter-object-factory';
import { DashboardSettings } from '../../src/types/internal';
import { TC_OLDEST_COMPATIBLE_VERSION } from '../../src/validate-settings';
import { mockFileExists, mockReadFile } from '../mocks';
import { AUTHENTICATION_TOKEN_REJECTED } from '../../src/texts';
import { testDoneInfo } from '../data';
import { DashboardValidationResult, RUNS_LIMIT_EXCEEDED_ERROR_MESSAGE } from '../../src/types/common';
import { DashboardInfo } from '../../src/types';
Expand Down Expand Up @@ -37,8 +36,6 @@ async function runReporterLifecycleMethods (reporter: any, requests: { url: stri
}

describe('initReporter', () => {
const errorText = 'not good';

let requests: { url: string; method: string; body: string }[] = [];

let logs: string[] = [];
Expand All @@ -63,44 +60,12 @@ describe('initReporter', () => {
json: () => Promise.resolve('okMock') } as Response);
};

function fetchFailMock (url, { method, body }) {
requests.push({ url, method, body });

const error = { message: errorText } as DashboardInfo;

return Promise.resolve({ ok: false, status: 401, statusText: 'Unauthorized',
json: () => Promise.resolve(error)
});
}

function fetchFailSilentMock () {
const silentError = { message: '' } as DashboardInfo;

return Promise.resolve({ ok: false, status: 401, statusText: 'Unauthorized',
json: () => Promise.resolve(silentError)
});
}

function getReporter (fetchMock) {
return buildReporterPlugin(() => reporterObjectFactory(
mockReadFile, mockFileExists, fetchMock, SETTINGS, loggerMock, TC_OLDEST_COMPATIBLE_VERSION
), process.stdout);
}

async function assertInitError (reporter, expectedError) {
let errorMessage;

try {
await reporter.init();
}
catch (e) {
errorMessage = e.message;
}
finally {
assert.strictEqual(errorMessage, expectedError);
}
}

function fetchOutOfLimits (url, { method, body }) {
const outOfLimitResponseJson =
{ type: DashboardValidationResult.warning, message: RUNS_LIMIT_EXCEEDED_ERROR_MESSAGE } as DashboardInfo;
Expand Down Expand Up @@ -152,21 +117,4 @@ describe('initReporter', () => {
assert.strictEqual(logs.length, 1);
assert.strictEqual(logs[0], 'Task execution report: http://localhost/runs/project_1/buildId');
});

it('Should throw on error', async () => {
const reporter = getReporter(fetchFailMock) as any;

await assertInitError(reporter, errorText);
assert.strictEqual(errors.length, 1);
assert.strictEqual(errors[0], errorText);
assert.strictEqual(requests.length, 1);
});

it('Should use default error text', async () => {
const reporter = getReporter(fetchFailSilentMock);

await assertInitError(reporter, AUTHENTICATION_TOKEN_REJECTED);
assert.strictEqual(errors.length, 1);
assert.strictEqual(errors[0], AUTHENTICATION_TOKEN_REJECTED);
});
});

0 comments on commit 2a8355e

Please sign in to comment.