Skip to content

Commit

Permalink
EPMRPP-95590 || Time with microseconds support
Browse files Browse the repository at this point in the history
  • Loading branch information
AmsterGet committed Oct 3, 2024
1 parent 88df82f commit 385259d
Show file tree
Hide file tree
Showing 9 changed files with 343 additions and 187 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
- The agent now supports reporting the time for launches, test items and logs with microsecond precision in the ISO string format.
For logs, microsecond precision is available on the UI from ReportPortal [version 24.2](https://reportportal.io/docs/releases/Version24.2/#:~:text=import%20plugin.-,Microseconds,-added%20to%20timestamps).
- `@reportportal/client-javascript` bumped to version `5.3.0`.
### Security
- Updated versions of vulnerable packages (micromatch).

## [5.1.0] - 2024-07-16
### Added
Expand Down
56 changes: 56 additions & 0 deletions __mocks__/@reportportal/client-javascript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2024 EPAM Systems
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

class RPClientMock {
config;

constructor(config) {
this.config = config;
}

startLaunch = jest.fn().mockReturnValue({
promise: Promise.resolve('ok'),
tempId: 'tempLaunchId',
});

finishLaunch = jest.fn().mockReturnValue({
promise: Promise.resolve('ok'),
});

startTestItem = jest.fn().mockReturnValue({
promise: Promise.resolve('ok'),
tempId: 'tempTestItemId',
});

finishTestItem = jest.fn().mockReturnValue({
promise: Promise.resolve('ok'),
});

getPromiseFinishAllItems = jest.fn().mockReturnValue({
promise: Promise.resolve('ok'),
});

sendLog = jest.fn().mockReturnValue({
promise: Promise.resolve('ok'),
});

checkConnect = jest.fn().mockReturnValue({
promise: Promise.resolve('ok'),
});
}

module.exports = RPClientMock;
43 changes: 22 additions & 21 deletions __tests__/jasmine-reportportal-reporter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,27 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const helpers = require('@reportportal/client-javascript/lib/helpers');
const Reporter = require('../lib/jasmine-reportportal-reporter');
const SpecificUtils = require('../lib/specificUtils');

describe('jasmine Report Portal reporter', () => {
const Reporter = require('../lib/jasmine-reportportal-reporter');
const SpecificUtils = require('../lib/specificUtils');
const mockedDate = '2024-09-23T12:20:59.392987Z';

describe('jasmine Report Portal reporter', () => {
let reporter;
const tempLaunchId = 'ewrf35432r';
let promise;
let baseTime;

beforeEach(() => {
jest.useFakeTimers();
jest.spyOn(helpers, 'now').mockReturnValue(mockedDate);
// TODO: should be replaced with unregistering specific listeners only
process.removeAllListeners();
const client = {
startTestItem() {},
finishTestItem() {},
sendLog() {},
};
const onSetLaunchStatus = function () {};

Check warning on line 36 in __tests__/jasmine-reportportal-reporter.spec.js

View workflow job for this annotation

GitHub Actions / test

Unexpected unnamed function

Check warning on line 36 in __tests__/jasmine-reportportal-reporter.spec.js

View workflow job for this annotation

GitHub Actions / test

Unexpected unnamed function
baseTime = new Date(2020, 4, 8);
jest.setSystemTime(baseTime);
reporter = new Reporter(
{
client,
Expand Down Expand Up @@ -363,7 +364,7 @@ describe('jasmine Report Portal reporter', () => {
level: 'level',
file: null,
message: 'message',
time: baseTime.valueOf(),
time: mockedDate,
},
],
};
Expand All @@ -381,13 +382,13 @@ describe('jasmine Report Portal reporter', () => {
level: 'level',
file: null,
message: 'message',
time: baseTime.valueOf(),
time: mockedDate,
},
{
level: 'level1',
file: null,
message: 'message1',
time: baseTime.valueOf(),
time: mockedDate,
},
],
};
Expand Down Expand Up @@ -448,7 +449,7 @@ describe('jasmine Report Portal reporter', () => {
{
message: 'message',
level: 'level',
time: baseTime.valueOf(),
time: mockedDate,
},
null
);
Expand All @@ -465,7 +466,7 @@ describe('jasmine Report Portal reporter', () => {
{
message: '',
level: 'level',
time: baseTime.valueOf(),
time: mockedDate,
},
undefined
);
Expand Down Expand Up @@ -604,7 +605,7 @@ describe('jasmine Report Portal reporter', () => {
description: 'text description',
testCaseId: 'testCaseId',
codeRef: 'codeRef',
startTime: baseTime.valueOf(),
startTime: mockedDate,
},
tempLaunchId,
null
Expand Down Expand Up @@ -653,7 +654,7 @@ describe('jasmine Report Portal reporter', () => {
description: 'test description',
name: 'test description',
codeRef: 'codeRef',
startTime: baseTime.valueOf(),
startTime: mockedDate,
},
tempLaunchId,
null
Expand All @@ -674,7 +675,7 @@ describe('jasmine Report Portal reporter', () => {
promise.then(() => {
expect(reporter.setParentInfo).toHaveBeenCalledWith({
tempId: '3452',
startTime: baseTime.valueOf(),
startTime: mockedDate,
});

done();
Expand Down Expand Up @@ -759,14 +760,14 @@ describe('jasmine Report Portal reporter', () => {
tempId: '3452',
promise: Promise.resolve(),
});
jest.spyOn(reporter, 'getHookStartTime').mockReturnValue(baseTime.valueOf());
jest.spyOn(reporter, 'getHookStartTime').mockReturnValue(mockedDate);

reporter.hookStarted('beforeAll');

expect(reporter.client.startTestItem).toHaveBeenCalledWith(
{
type: 'BEFORE_SUITE',
startTime: baseTime.valueOf(),
startTime: mockedDate,
name: 'beforeAll',
},
tempLaunchId,
Expand All @@ -787,7 +788,7 @@ describe('jasmine Report Portal reporter', () => {

expect(reporter.client.finishTestItem).toHaveBeenCalledWith('3452', {
status: 'passed',
endTime: baseTime.valueOf(),
endTime: mockedDate,
});
expect(reporter.itemStartTime).toEqual(null);
});
Expand All @@ -802,7 +803,7 @@ describe('jasmine Report Portal reporter', () => {

expect(reporter.client.finishTestItem).toHaveBeenCalledWith('3452', {
status: 'failed',
endTime: baseTime.valueOf(),
endTime: mockedDate,
});
});

Expand Down Expand Up @@ -879,7 +880,7 @@ describe('jasmine Report Portal reporter', () => {
{
message: 'message: error\nstackTrace: stack',
level: 'ERROR',
time: baseTime.valueOf(),
time: mockedDate,
},
null
);
Expand Down Expand Up @@ -978,7 +979,7 @@ describe('jasmine Report Portal reporter', () => {
{
message: 'message: error\nstackTrace: stack',
level: 'ERROR',
time: baseTime.valueOf(),
time: mockedDate,
},
null
);
Expand Down
7 changes: 5 additions & 2 deletions __tests__/reportportal-agent.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const SpecificUtils = require('../lib/specificUtils');

const reporterOptions = {
apiKey: 'reportportalApiKey',
endpoint: 'endpoint',
endpoint: 'http://reportportal.example.com/api/v1',
project: 'projectName',
launch: 'launcherName',
description: 'description',
Expand All @@ -44,10 +44,13 @@ describe('Report Portal agent', () => {
let agent;

beforeAll(() => {
jest.clearAllMocks(); // Clear mocks before initialization
agent = new ReportportalAgent(options);
});

beforeEach(() => {
jest.clearAllMocks(); // Clear mocks before initialization
})

Check failure on line 52 in __tests__/reportportal-agent.spec.js

View workflow job for this annotation

GitHub Actions / test

Insert `;`

Check failure on line 52 in __tests__/reportportal-agent.spec.js

View workflow job for this annotation

GitHub Actions / test

Insert `;`

it('should be properly initialized', () => {
expect(agent.tempLaunchId).toBeDefined();
expect(agent.client).toBeDefined();
Expand Down
28 changes: 28 additions & 0 deletions __tests__/specificUtils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,5 +348,33 @@ describe('Specific Utils', () => {
expect(escapeString).toBe('\\_test\\*');
});
});

describe('convertIsoStringToMicroseconds', () => {
it('converts ISO string with microseconds correctly', () => {
const isoString = '2024-09-20T14:32:35.304456Z';
const expectedMicroseconds = 1726842755304456;
expect(SpecificUtils.convertIsoStringToMicroseconds(isoString)).toBe(expectedMicroseconds);
});

it('handles microseconds accurately', () => {
const isoString = '2021-03-15T12:00:00.000001Z';
const expectedMicroseconds = 1615809600000001;
expect(SpecificUtils.convertIsoStringToMicroseconds(isoString)).toBe(expectedMicroseconds);
});

it('returns correct microseconds at epoch start', () => {
const isoString = '1970-01-01T00:00:00.000001Z';
const expectedMicroseconds = 1;
expect(SpecificUtils.convertIsoStringToMicroseconds(isoString)).toBe(expectedMicroseconds);
});
});

describe('getBeforeHookStartTime', () => {
it('should return the start time for the hook as reduced time from test item start by 1 millisecond', () => {
const itemStartTime = '2024-09-20T14:32:35.304456Z';
const expectedHookStartTime = '2024-09-20T14:32:35.303456Z';
expect(SpecificUtils.getBeforeHookStartTime(itemStartTime)).toBe(expectedHookStartTime);
});
});
});
});
4 changes: 3 additions & 1 deletion lib/jasmine-reportportal-reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

const { EVENTS } = require('@reportportal/client-javascript/lib/constants/events');
const { RP_STATUSES } = require('@reportportal/client-javascript/lib/constants/statuses');
const clientHelpers = require('@reportportal/client-javascript/lib/helpers');
const SpecificUtils = require('./specificUtils');
const { entityType, hookTypes, hookTypesMap } = require('./constants/itemTypes');
const LOG_LEVELS = require('./constants/logLevels');
Expand Down Expand Up @@ -84,7 +85,7 @@ class ReportportalReporter {
}

getTime() {
return new Date().valueOf();
return clientHelpers.now();
}

addAttributes(attr) {
Expand Down Expand Up @@ -282,6 +283,7 @@ class ReportportalReporter {
});
}

// TODO: update
getHookStartTime(hookType, parent) {
if (hookType === entityType.BEFORE_METHOD || hookType === entityType.BEFORE_SUITE) {
return Math.max(parent && parent.startTime, this.itemStartTime - 1);
Expand Down
Loading

0 comments on commit 385259d

Please sign in to comment.