diff --git a/src/pytest_html/scripts/dom.js b/src/pytest_html/scripts/dom.js
index 029f74b9..667b3747 100644
--- a/src/pytest_html/scripts/dom.js
+++ b/src/pytest_html/scripts/dom.js
@@ -51,7 +51,7 @@ const dom = {
header.querySelector('#results-table-head > tr').appendChild(t.content)
})
- header.querySelector(`.sortable[data-column-type="${sortAttr}"]`).classList.add(sortAsc ? 'desc' : 'asc')
+ header.querySelector(`.sortable[data-column-type="${sortAttr}"]`)?.classList.add(sortAsc ? 'desc' : 'asc')
return header
},
diff --git a/src/pytest_html/scripts/sort.js b/src/pytest_html/scripts/sort.js
index eabc4dad..3eaf4bdd 100644
--- a/src/pytest_html/scripts/sort.js
+++ b/src/pytest_html/scripts/sort.js
@@ -24,6 +24,24 @@ const genericSort = (list, key, ascending, customOrder) => {
return sorted
}
+const durationSort = (list, ascending) => {
+ const parseDuration = (duration) => {
+ if (duration.includes(':')) {
+ // If it's in the format "HH:mm:ss"
+ const [hours, minutes, seconds] = duration.split(':').map(Number)
+ return (hours * 3600 + minutes * 60 + seconds) * 1000
+ } else {
+ // If it's in the format "nnn ms"
+ return parseInt(duration)
+ }
+ }
+ const sorted = list.sort((a, b) => parseDuration(a['duration']) - parseDuration(b['duration']))
+ if (ascending) {
+ sorted.reverse()
+ }
+ return sorted
+}
+
const doInitSort = () => {
const type = storageModule.getSort()
const ascending = storageModule.getSortDirection()
@@ -32,7 +50,18 @@ const doInitSort = () => {
if (type?.toLowerCase() === 'original') {
manager.setRender(list)
} else {
- const sortedList = genericSort(list, type, ascending, initialOrder)
+ let sortedList
+ switch (type) {
+ case 'duration':
+ sortedList = durationSort(list, ascending)
+ break
+ case 'result':
+ sortedList = genericSort(list, type, ascending, initialOrder)
+ break
+ default:
+ sortedList = genericSort(list, type, ascending)
+ break
+ }
manager.setRender(sortedList)
}
}
@@ -45,7 +74,7 @@ const doSort = (type) => {
storageModule.setSortDirection(ascending)
const list = manager.testSubset
- const sortedList = genericSort(list, type, ascending)
+ const sortedList = type === 'duration' ? durationSort(list, ascending) : genericSort(list, type, ascending)
manager.setRender(sortedList)
}
diff --git a/testing/unittest.js b/testing/unittest.js
index b6272082..67f6654b 100644
--- a/testing/unittest.js
+++ b/testing/unittest.js
@@ -94,7 +94,7 @@ describe('Sort tests', () => {
afterEach(() => [sortMock, sortDirectionMock, managerSpy].forEach((fn) => fn.restore()))
it('has no stored sort', () => {
- sortMock = sinon.stub(storageModule, 'getSort').returns(null)
+ sortMock = sinon.stub(storageModule, 'getSort').returns('result')
sortDirectionMock = sinon.stub(storageModule, 'getSortDirection').returns(null)
managerSpy = sinon.spy(dataModule.manager, 'setRender')