Skip to content

Commit

Permalink
feat: add REPORT_SAVED event
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolai Markov committed Sep 15, 2022
1 parent 24d3b34 commit cd16aa6
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 10 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,8 @@ Property name | Description
Event | Description
------------------------- | -------------
`DATABASE_CREATED` | Will be triggered after sqlite database is created. The handler accepts a database instance. The event is synchronous.
`TEST_SCREENSHOTS_SAVED` | Will be triggered after test screenshots were saved. The handler accepts test id and screenshots info. The event is synchronous.
`TEST_SCREENSHOTS_SAVED` | Will be triggered after test screenshots were saved. The handler accepts test id and screenshots info. The event is asynchronous, so your handler can return a promise.
`REPORT_SAVED` | Will be triggered after all test files were saved. The event is asynchronous, so your handler can return a promise.
### events
Expand All @@ -558,7 +559,7 @@ module.exports = (hermione, opts) => {
Example of a subscription to an event `TEST_SCREENSHOTS_SAVED`:
```js
hermione.htmlReporter.on(hermione.htmlReporter.events.TEST_SCREENSHOTS_SAVED, ({testId, attempt, imagesInfo}) => {
hermione.htmlReporter.on(hermione.htmlReporter.events.TEST_SCREENSHOTS_SAVED, async ({testId, attempt, imagesInfo}) => {
console.log(`Screenshots for test "${testId}" (attempt #${attempt}) were saved:`, imagesInfo);
/* Expected output:
Screenshots for test "Feature Test.chrome-desktop" (attempt #0) were saved:
Expand All @@ -578,6 +579,13 @@ hermione.htmlReporter.on(hermione.htmlReporter.events.TEST_SCREENSHOTS_SAVED, ({
});
```
Example of a subscription to an event `REPORT_SAVED`:
```js
hermione.htmlReporter.on(hermione.htmlReporter.events.REPORT_SAVED, async ({reportPath}) => {
await uploadDirToS3(reportPath);
});
```
### addExtraItem
Adds item to html report as link:
Expand Down
3 changes: 2 additions & 1 deletion lib/constants/plugin-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

const getSyncEvents = () => ({
DATABASE_CREATED: 'databaseCreated',
TEST_SCREENSHOTS_SAVED: 'testScreenshotsSaved'
TEST_SCREENSHOTS_SAVED: 'testScreenshotsSaved',
REPORT_SAVED: 'reportSaved'
});

const events = getSyncEvents();
Expand Down
2 changes: 2 additions & 0 deletions lib/merge-reports/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ module.exports = async (pluginConfig, hermione, srcPaths, {destination: destPath
serverUtils.saveStaticFilesToReportDir(hermione, pluginConfig, destPath),
serverUtils.writeDatabaseUrlsFile(destPath, srcPaths)
]);

await hermione.htmlReporter.emitAsync(hermione.htmlReporter.events.REPORT_SAVED, {reportPath: destPath});
};

function validateOpts(srcPaths, destPath) {
Expand Down
7 changes: 6 additions & 1 deletion lib/plugin-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ module.exports = class PluginAdapter {
staticReportBuilder.saveStaticFiles(),
prepareData(this._hermione, staticReportBuilder, this._config)
])
.then(() => staticReportBuilder.finalize());
.then(() => staticReportBuilder.finalize())
.then(async () => {
const htmlReporter = this._hermione.htmlReporter;

await htmlReporter.emitAsync(htmlReporter.events.REPORT_SAVED, {reportPath: this._config.path});
});
}

async _run(prepareData) {
Expand Down
4 changes: 2 additions & 2 deletions lib/plugin-api.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';

const EventsEmitter = require('events');
const EventsEmitter2 = require('eventemitter2');
const pluginEvents = require('./constants/plugin-events');
const {downloadDatabases, mergeDatabases, getTestsTreeFromDatabase} = require('./db-utils/server');

module.exports = class HtmlReporter extends EventsEmitter {
module.exports = class HtmlReporter extends EventsEmitter2 {
static create(config) {
return new this(config);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/test-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ module.exports = class TestAdapter {
}

const htmlReporter = this._hermione.htmlReporter;
htmlReporter.emit(htmlReporter.events.TEST_SCREENSHOTS_SAVED, {
await htmlReporter.emitAsync(htmlReporter.events.TEST_SCREENSHOTS_SAVED, {
testId: this._testId,
attempt: this.attempt,
imagesInfo: this.getImagesInfo()
Expand Down
16 changes: 15 additions & 1 deletion test/unit/lib/merge-reports/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const serverUtils = require('lib/server-utils');

describe('lib/merge-reports', () => {
const sandbox = sinon.sandbox.create();
let htmlReporter;

const execMergeReports_ = async ({pluginConfig = stubConfig(), hermione = stubTool(stubConfig()), paths = [], opts = {}}) => {
opts = _.defaults(opts, {destination: 'default-dest-report/path'});
Expand All @@ -17,6 +18,10 @@ describe('lib/merge-reports', () => {
beforeEach(() => {
sandbox.stub(serverUtils, 'saveStaticFilesToReportDir').resolves();
sandbox.stub(serverUtils, 'writeDatabaseUrlsFile').resolves();

htmlReporter = sinon.stub();
htmlReporter.events = {REPORT_SAVED: 'reportSaved'};
htmlReporter.emitAsync = sinon.stub();
});

afterEach(() => sandbox.restore());
Expand All @@ -39,7 +44,7 @@ describe('lib/merge-reports', () => {

it('should merge reports', async () => {
const pluginConfig = stubConfig();
const hermione = stubTool(pluginConfig);
const hermione = stubTool(pluginConfig, {}, {}, htmlReporter);
const paths = ['src-report/path-1', 'src-report/path-2'];
const destination = 'dest-report/path';

Expand All @@ -48,4 +53,13 @@ describe('lib/merge-reports', () => {
assert.calledOnceWith(serverUtils.saveStaticFilesToReportDir, hermione, pluginConfig, destination);
assert.calledOnceWith(serverUtils.writeDatabaseUrlsFile, destination, paths);
});

it('should emit REPORT_SAVED event', async () => {
const hermione = stubTool({}, {}, {}, htmlReporter);
const destination = 'dest-report/path';

await execMergeReports_({pluginConfig: {}, hermione, paths: [''], opts: {destination}});

assert.calledOnceWith(hermione.htmlReporter.emitAsync, 'reportSaved', {reportPath: destination});
});
});
9 changes: 9 additions & 0 deletions test/unit/lib/plugin-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,15 @@ describe('lib/plugin-adapter', () => {
});
});

it('should emit REPORT_SAVED event', async () => {
await initCliReporter_({path: '/some/report/path'}, {});

tool.emit(tool.events.END);
await tool.emitAsync(tool.events.RUNNER_END);

assert.calledOnceWith(tool.htmlReporter.emitAsync, 'reportSaved', {reportPath: '/some/report/path'});
});

it('should log correct path to html report', () => {
return initCliReporter_({path: 'some/path'}, {})
.then(() => {
Expand Down
4 changes: 2 additions & 2 deletions test/unit/lib/test-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('hermione test adapter', () => {
Object.assign({
imagesSaver: {saveImg: sandbox.stub()},
events: {TEST_SCREENSHOTS_SAVED: 'testScreenshotsSaved'},
emit: sinon.stub()
emitAsync: sinon.stub()
}, htmlReporter)
);

Expand Down Expand Up @@ -308,7 +308,7 @@ describe('hermione test adapter', () => {
const htmlReporterEmitStub = sinon.stub();
const hermioneTestAdapter = mkHermioneTestResultAdapter(testResult, {
htmlReporter: {
emit: htmlReporterEmitStub
emitAsync: htmlReporterEmitStub
}
});
sinon.stub(hermioneTestAdapter, 'getImagesInfo').returns([{test: 123}]);
Expand Down
4 changes: 4 additions & 0 deletions test/unit/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ function stubTool(config = stubConfig(), events = {}, errors = {}, htmlReporter)
tool.run = sinon.stub().resolves(false);
tool.readTests = sinon.stub().resolves(stubTestCollection());
tool.htmlReporter = htmlReporter || sinon.stub();
_.defaultsDeep(tool.htmlReporter, {
emitAsync: sinon.stub(),
events: {REPORT_SAVED: 'reportSaved'}
});
tool.isWorker = () => {
return false;
};
Expand Down

0 comments on commit cd16aa6

Please sign in to comment.