Skip to content

Commit

Permalink
fix: set timestamp on test end for sqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
rmdm committed Apr 7, 2020
1 parent 1cd6214 commit 51098d2
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 8 deletions.
9 changes: 8 additions & 1 deletion lib/report-builder/report-builder-sqlite.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ module.exports = class ReportBuilderSqlite extends ReportBuilder {
await this._sqliteAdapter.init();
}

format(result, status) {
result.timestamp = Date.now();
return super.format(result, status);
}

async saveStaticFiles() {
const destPath = this._pluginConfig.path;

Expand All @@ -46,7 +51,9 @@ module.exports = class ReportBuilderSqlite extends ReportBuilder {
_addTestResult(formattedResult, props) {
super._addTestResult(formattedResult, props);

const testResult = this._createTestResult(formattedResult, _.extend(props, {attempt: formattedResult.attempt}));
const testResult = this._createTestResult(formattedResult, _.extend(props, {
timestamp: formattedResult.timestamp
}));
if (!ignoredStatuses.includes(testResult.status)) {
this._writeTestResultToDb(testResult, formattedResult);
}
Expand Down
5 changes: 3 additions & 2 deletions lib/sqlite-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ module.exports = class SqliteAdapter {
imagesInfo,
screenshot,
multipleTabs,
status
status,
timestamp = Date.now()
} = testResult;

return {
Expand All @@ -85,7 +86,7 @@ module.exports = class SqliteAdapter {
screenshot,
multipleTabs,
status,
timestamp: Date.now()
timestamp
};
}

Expand Down
11 changes: 11 additions & 0 deletions lib/test-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ module.exports = class TestAdapter {
this._imagesSaver = this._hermione.htmlReporter.imagesSaver;
this._testId = `${this._testResult.fullTitle()}.${this._testResult.browserId}`;
this._errorDetails = undefined;
this._timestamp = this._testResult.timestamp;

if (utils.shouldUpdateAttempt(status)) {
testsAttempts.set(this._testId, _.isUndefined(testsAttempts.get(this._testId)) ? 0 : testsAttempts.get(this._testId) + 1);
Expand Down Expand Up @@ -285,6 +286,16 @@ module.exports = class TestAdapter {
return true;
}

get timestamp() {
return this._timestamp;
}

set timestamp(timestamp) {
if (!_.isNumber(this._timestamp)) {
this._timestamp = timestamp;
}
}

async saveErrorDetails(reportPath) {
if (!this.errorDetails) {
return;
Expand Down
28 changes: 23 additions & 5 deletions test/unit/lib/report-builder-sqlite.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,6 @@ describe('ReportBuilderSqlite', () => {
const stubTest_ = (opts = {}) => {
const {imagesInfo = []} = opts;

let attempt = 0;
if (opts.attempt === undefined) {
Object.defineProperty(opts, 'attempt', {get: () => attempt++});
}

return _.defaultsDeep(opts, {
state: {name: 'name-default'},
suite: {
Expand Down Expand Up @@ -138,6 +133,29 @@ describe('ReportBuilderSqlite', () => {

assert.equal(status, ERROR);
});

it('should use timestamp from test result when it is present', async () => {
reportBuilderSqlite.format.returns(_.defaults(formattedSuite_(), {
timestamp: 100500
}));
await reportBuilderSqlite.addSuccess(stubTest_());
const db = new Database(TEST_DB_PATH);

const [{timestamp}] = db.prepare('SELECT * from suites').all();
db.close();

assert.equal(timestamp, 100500);
});

it('should use some current timestamp when test result misses one', async () => {
await reportBuilderSqlite.addSuccess(stubTest_());
const db = new Database(TEST_DB_PATH);

const [{timestamp}] = db.prepare('SELECT * from suites').all();
db.close();

assert.isNumber(timestamp);
});
});

describe('finalization', () => {
Expand Down
11 changes: 11 additions & 0 deletions test/unit/lib/test-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -522,4 +522,15 @@ describe('hermione test adapter', () => {
assert.calledWith(imagesSaver.saveImg, sinon.match('dest/path'), {destPath: 'dest/path', reportDir: 'report/path'});
});
});

describe('timestamp', () => {
it('should return corresponding timestamp of the test result', () => {
const testResult = mkTestResult_({
timestamp: 100500
});
const hermioneTestAdapter = mkHermioneTestResultAdapter(testResult);

assert.strictEqual(hermioneTestAdapter.timestamp, 100500);
});
});
});

0 comments on commit 51098d2

Please sign in to comment.