diff --git a/package.json b/package.json index bec0f258..bb07108b 100644 --- a/package.json +++ b/package.json @@ -5,16 +5,13 @@ "build:jsapp": "browserify ./src/pytest_html/scripts/index.js > ./src/pytest_html/resources/app.js", "build": "npm run unit && npm run build:css && npm run build:jsapp" }, - "dependencies": { - "dayjs": "^1.11.3" - }, "devDependencies": { "browserify": "^17.0.0", "chai": "^4.3.6", + "eslint": "^8.20.0", + "eslint-config-google": "^0.14.0", "mocha": "^10.0.0", "sass": "^1.52.3", - "sinon": "^14.0.0", - "eslint": "^8.20.0", - "eslint-config-google": "^0.14.0" + "sinon": "^14.0.0" } } diff --git a/src/pytest_html/scripts/datamanager.js b/src/pytest_html/scripts/datamanager.js index 58344972..20e3adfb 100644 --- a/src/pytest_html/scripts/datamanager.js +++ b/src/pytest_html/scripts/datamanager.js @@ -47,9 +47,6 @@ class DataManager { get collectedItems() { return this.renderData.collectedItems } - get durationFormat() { - return this.renderData.durationFormat - } get isFinished() { return this.data.runningState === 'Finished' } diff --git a/src/pytest_html/scripts/dom.js b/src/pytest_html/scripts/dom.js index 0dce623a..b8a4a9fa 100644 --- a/src/pytest_html/scripts/dom.js +++ b/src/pytest_html/scripts/dom.js @@ -75,7 +75,7 @@ const dom = { resultBody.querySelector('.col-result').classList.add(`${collapsed ? 'expander' : 'collapser'}`) resultBody.querySelector('.col-result').dataset.id = id resultBody.querySelector('.col-name').innerText = nodeid - resultBody.querySelector('.col-duration').innerText = `${formatDuration(duration)}s` + resultBody.querySelector('.col-duration').innerText = formatDuration(duration) if (longreprtext) { diff --git a/src/pytest_html/scripts/filter.js b/src/pytest_html/scripts/filter.js index 4c2ccac7..c78a4989 100644 --- a/src/pytest_html/scripts/filter.js +++ b/src/pytest_html/scripts/filter.js @@ -27,7 +27,7 @@ const doFilter = (type, show) => { } } -module.exports= { +module.exports = { doFilter, doInitFilter, } diff --git a/src/pytest_html/scripts/utils.js b/src/pytest_html/scripts/utils.js index 29e06112..3dd5e156 100644 --- a/src/pytest_html/scripts/utils.js +++ b/src/pytest_html/scripts/utils.js @@ -1,16 +1,23 @@ -const { manager } = require('./datamanager.js') +const formatedNumber = (number) => + number.toLocaleString('en-US', { + minimumIntegerDigits: 2, + useGrouping: false, + }) -const dayjs = require('dayjs') -const duration = require('dayjs/plugin/duration') -dayjs.extend(duration) -const formatDuration = (dur) => { - const durationFormat = manager.durationFormat - if (durationFormat.length === 0) { - return dur.toFixed(2) - } else { - return dayjs.duration(dur * 1000).format(durationFormat) +const formatDuration = ( ms ) => { + const totalSeconds = ms / 1000 + + if (totalSeconds < 1) { + return `${ms}ms` } + const hours = Math.floor(totalSeconds / 3600) + let remainingSeconds = totalSeconds % 3600 + const minutes = Math.floor(remainingSeconds / 60) + remainingSeconds = remainingSeconds % 60 + const seconds = Math.round(remainingSeconds) + + return `${formatedNumber(hours)}:${formatedNumber(minutes)}:${formatedNumber(seconds)}` } module.exports = { formatDuration } diff --git a/testing/unittest.js b/testing/unittest.js index 667b4fae..f3c46eee 100644 --- a/testing/unittest.js +++ b/testing/unittest.js @@ -2,6 +2,7 @@ const { expect } = require('chai') const sinon = require('sinon') const { doInitFilter, doFilter } = require('../src/pytest_html/scripts/filter.js') const { doInitSort, doSort } = require('../src/pytest_html/scripts/sort.js') +const { formatDuration } = require('../src/pytest_html/scripts/utils.js') const dataModule = require('../src/pytest_html/scripts/datamanager.js') const storageModule = require('../src/pytest_html/scripts/storage.js') @@ -92,7 +93,7 @@ describe('Sort tests', () => { let sortDirectionMock beforeEach(() => dataModule.manager.resetRender()) - afterEach(() => [sortMock,sortDirectionMock, managerSpy].forEach((fn) => fn.restore())) + afterEach(() => [sortMock, sortDirectionMock, managerSpy].forEach((fn) => fn.restore())) it('has no stored sort', () => { sortMock = sinon.stub(storageModule, 'getSort').returns(null) sortDirectionMock = sinon.stub(storageModule, 'getSortDirection').returns(null) @@ -141,3 +142,17 @@ describe('Sort tests', () => { }) }) }) + +describe('utils tests', () => { + describe('formatDuration', () => { + it('handles small durations', () => { + expect(formatDuration(123)).to.eql('123ms') + expect(formatDuration(0)).to.eql('0ms') + expect(formatDuration(999)).to.eql('999ms') + }) + it('handles larger durations', () => { + expect(formatDuration(1234)).to.eql('00:00:01') + expect(formatDuration(12345678)).to.eql('03:25:46') + }) + }) +})