Skip to content

Commit

Permalink
refactor 7
Browse files Browse the repository at this point in the history
  • Loading branch information
Artem-Babich committed Mar 17, 2023
1 parent 23f265e commit f1ad440
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 73 deletions.
50 changes: 41 additions & 9 deletions src/reporter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ interface TaskInfo {
}

interface TestInfo {
reportData: Dictionary<any[]>;
fixture: Fixture;
test: Test;
testRunIds: string[];
Expand Down Expand Up @@ -86,7 +87,7 @@ interface BrowserRunInfo extends Browser {
interface TestRunInfo {
errs: TestRunErrorFormattableAdapter[];
warnings: string[];
reportData: any[];
reportData: Dictionary<any[]>;
durationMs: number;
unstable: boolean;
screenshotPath: string;
Expand Down Expand Up @@ -307,6 +308,7 @@ export default class Reporter {
pendingTestRunDonePromise: Reporter._createPendingPromise(),
pendingTestRunStartPromise: Reporter._createPendingPromise(),
browsers: [],
reportData: {},
};
}

Expand All @@ -316,11 +318,11 @@ export default class Reporter {
return task.tests.map(test => Reporter._createTestItem(test, runsPerTest));
}

private static _createTestRunInfo (reportItem: TestInfo, testRun: TestRun): TestRunInfo {
private static _createTestRunInfo (reportItem: TestInfo): TestRunInfo {
return {
errs: sortBy(reportItem.errs, ['userAgent', 'code']),
warnings: reportItem.warnings,
reportData: testRun.reportDataLog ? testRun.reportDataLog.data : [],
reportData: reportItem.reportData,
durationMs: +new Date() - (reportItem.startTime as number), //eslint-disable-line @typescript-eslint/no-extra-parens
unstable: reportItem.unstable,
screenshotPath: reportItem.screenshotPath as string,
Expand Down Expand Up @@ -412,7 +414,7 @@ export default class Reporter {
}

if (!testItem.testRunInfo) {
testItem.testRunInfo = Reporter._createTestRunInfo(testItem, testRun);
testItem.testRunInfo = Reporter._createTestRunInfo(testItem);

if (testItem.test.skip)
taskInfo.skipped++;
Expand Down Expand Up @@ -553,10 +555,12 @@ export default class Reporter {

reportItem.browsers.push(browser);

reportItem.pendingRuns = isTestRunStoppedTaskExecution ? 0 : reportItem.pendingRuns - 1;
reportItem.unstable = reportItem.unstable || testRun.unstable;
reportItem.errs = reportItem.errs.concat(testRun.errs);
reportItem.warnings = testRun.warningLog ? union(reportItem.warnings, testRun.warningLog.messages) : [];
reportItem.pendingRuns = isTestRunStoppedTaskExecution ? 0 : reportItem.pendingRuns - 1;
reportItem.unstable = reportItem.unstable || testRun.unstable;
reportItem.errs = reportItem.errs.concat(testRun.errs);
reportItem.warnings = testRun.warningLog ? union(reportItem.warnings, testRun.warningLog.messages) : [];
reportItem.reportData = reportItem.reportData || {};
reportItem.reportData[testRun.id] = testRun.reportDataLog.data;

if (testRun.quarantine) {
reportItem.quarantine = reportItem.quarantine || {};
Expand Down Expand Up @@ -645,15 +649,43 @@ export default class Reporter {
(this.taskInfo.pendingTaskDonePromise.resolve as Function)();
}

private _prepareReportDataEventArgs (testRun: TestRun): any {
const { test, reportDataLog, browser, id } = testRun;
const fixture = test.fixture;

const testInfo = {
name: test.name,
id: test.id,
meta: test.meta,
};

const fixtureInfo = {
name: fixture?.name,
id: fixture?.id,
meta: fixture?.meta,
path: fixture?.path,
};

return {
reportData: reportDataLog.data,
test: testInfo,
fixture: fixtureInfo,
testRunId: id,
browser,
};
}

private async _onReportDataHandler ({ testRun, data }: ReportDataEventArgs): Promise<void> {
if (!this.taskInfo)
return;

const testRunInfo = this._prepareReportDataEventArgs(testRun);

await this.dispatchToPlugin({
method: ReporterPluginMethod.reportData as string,
initialObject: this.taskInfo.task,
args: [
testRun,
testRunInfo,
...data,
],
});
Expand Down
1 change: 0 additions & 1 deletion src/runner/test-run-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ export default class TestRunController extends AsyncEventEmitter {
private _assignTestRunEvents (testRun: TestRun | LegacyTestRun, connection: BrowserConnection): void {
testRun.on('action-start', async (args: ActionEventArg) => this._emitActionStart(Object.assign(args, { testRun })));
testRun.on('action-done', async (args: ActionEventArg) => this._emitActionDone(Object.assign(args, { testRun })));
testRun.on('report-data', async (data: any) => this._messageBus.emit('report-data', { testRun, data }));

testRun.once('start', async () => this._emitTestRunStart());
testRun.once('ready', async () => {
Expand Down
79 changes: 27 additions & 52 deletions test/functional/fixtures/reporter/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -889,83 +889,58 @@ const experimentalDebug = !!process.env.EXPERIMENTAL_DEBUG;
});

describe('Report Data', () => {
let dataResult = null;
let reporter = null;
let reportDataInfos = {};
let testDoneInfos = {};
let reporter = null;

const createReportDataReporter = () => {
dataResult = {
reportDataInfos: {},
testDoneList: {},
};

return createReporter({
reportData: (testRun, ...data) => {
const testName = testRun.test.name;
reportData: ({ browser }, ...data) => {
const alias = browser.alias;

if (!dataResult.reportDataInfos[testName])
dataResult.reportDataInfos[testName] = [];
if (!reportDataInfos[alias])
reportDataInfos[alias] = [];

dataResult.reportDataInfos[testName].push(...data);
reportDataInfos[alias].push(data);
},
reportTestDone: (name, { reportData }) => {
dataResult.testDoneList[name] = reportData;
reportTestDone: (name, { reportData, browsers }) => {
browsers.forEach(({ testRunId, alias }) => {
testDoneInfos[alias] = reportData[testRunId];
});
},
});
};

const expectedReports = {
'Run t.report action twice': [['Report 1', 'Report 2'], 'Report 3'],
'Run t.report action with multiple args': ['Report 1', 'Report2', { 'reportResult': 'test' }, 'Report 3', 'Report 4'],
'Run t.report action with object val': ['Report 1', { 'reportResult': 'test' }],
};


beforeEach(() => {
reporter = createReportDataReporter();
});
reportDataInfos = {};
testDoneInfos = {};

it('Should raise reportData twice', async () => {
const testName = 'Run t.report action twice';

await runTests('testcafe-fixtures/report-data-test.js', testName, {
reporter,
});

expect(dataResult.testDoneList).eql({ [testName]: expectedReports[testName] });
expect(dataResult.reportDataInfos).eql({ [testName]: expectedReports[testName] });
reporter = createReportDataReporter();
});

it('Should raise reportData with object value', async () => {
const testName = 'Run t.report action with object val';
it('Should raise reportData twice with different argument count and types', async () => {
const expectedReportData = [1, true, 'string', { 'reportResult': 'test' }];

await runTests('testcafe-fixtures/report-data-test.js', testName, {
await runTests('testcafe-fixtures/report-data-test.js', 'Run t.report action with object val', {
reporter,
});

expect(dataResult.testDoneList).eql({ [testName]: expectedReports[testName] });
expect(dataResult.reportDataInfos).eql({ [testName]: expectedReports[testName] });
});
const reportDataBrowserInfos = Object.entries(reportDataInfos);
const testDoneBrowserInfos = Object.entries(testDoneInfos);

it('Should raise reportData with multiple args', async () => {
const testName = 'Run t.report action with multiple args';
expect(reportDataBrowserInfos.length).eql(config.browsers.length);
expect(testDoneBrowserInfos.length).eql(config.browsers.length);

await runTests('testcafe-fixtures/report-data-test.js', testName, {
reporter,
reportDataBrowserInfos.forEach(([alias, reportData]) => {
expect(reportData.flat()).eql(testDoneInfos[alias]);
});

expect(dataResult.testDoneList).eql({ [testName]: expectedReports[testName] });
expect(dataResult.reportDataInfos).eql({ [testName]: expectedReports[testName] });
});
testDoneBrowserInfos.forEach(([, reportData]) => {
const [, ...rest] = reportData;

it('Should collect info from different tests', async () => {
await runTests('testcafe-fixtures/report-data-test.js', null, {
reporter,
expect(rest).eql(expectedReportData);
});

expect(dataResult.testDoneList).eql(expectedReports);
expect(dataResult.reportDataInfos).eql(expectedReports);
});

});

describe('Warnings', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
fixture`Report Data API`
.page('../pages/index.html');

test('Run t.report action twice', async t => {
await t.report(['Report 1', 'Report 2']);
await t.report('Report 3');
});

test('Run t.report action with object val', async t => {
await t.report('Report 1').report({ 'reportResult': 'test' });
});

test('Run t.report action with multiple args', async t => {
await t.report('Report 1', 'Report2', { 'reportResult': 'test' });
await t.report('Report 3', 'Report 4');
await t
.report(t.browser.alias)
.report(1, true, 'string', { 'reportResult': 'test' });
});

0 comments on commit f1ad440

Please sign in to comment.