diff --git a/src/export.ts b/src/export.ts index 5eec6b1..a23b845 100644 --- a/src/export.ts +++ b/src/export.ts @@ -7,7 +7,7 @@ import { writeJson, writeJsonSync, } from '@deps' -import { generateFileName } from '@pkg/utils.ts' +import { generateFileName, updateOrCreate } from '@pkg/utils.ts' import { TDataResult, TJSON } from '@pkg/types.ts' import { getAverageDuration, getCoverage, getFlaky } from '@pkg/utils.ts' @@ -135,33 +135,78 @@ const exportJSONReport = ( total_tests: [ { id: 'years', - data: [{ - x: month, - y: totalTests.length, - }], + data: jsonData?.drowser?.metrics?.graphs?.total_tests[0] + .data as any ?? [], }, ], - passing_tests: [ - { name: month, count: groupByStatus?.passed?.length ?? 0 }, - ], - failed_tests: [ - { name: month, count: groupByStatus?.failed?.length ?? 0 }, - ], - test_coverage: [ - { name: month, count: getCoverage({ results: totalTests }) ?? 0 }, - ], - avg_test_duration: [ - { - name: month, - count: getAverageDuration({ results: totalTests }) ?? 0, - }, - ], - flaky_tests: [ - { id: month, value: flakyTestsCount ?? 0 }, - ], + passing_tests: + jsonData?.drowser?.metrics?.graphs?.passing_tests as any ?? [], + failed_tests: jsonData?.drowser?.metrics?.graphs?.failed_tests as any ?? + [], + test_coverage: + jsonData?.drowser?.metrics?.graphs?.test_coverage as any ?? [], + avg_test_duration: jsonData?.drowser?.metrics?.graphs + ?.avg_test_duration as any ?? [], + flaky_tests: jsonData?.drowser?.metrics?.graphs + ?.flaky_tests as any ?? [], }, } + updateOrCreate( + jsonData?.drowser?.metrics?.graphs?.total_tests[0]?.data as Array< + Record + >, + 'x', + { x: month, y: totalTests.length }, + month, + ) + + updateOrCreate( + jsonData?.drowser?.metrics?.graphs?.passing_tests as Array< + Record + >, + 'name', + { name: month, count: groupByStatus?.passed?.length }, + month, + ) + + updateOrCreate( + jsonData?.drowser?.metrics?.graphs?.failed_tests as Array< + Record + >, + 'name', + { name: month, count: groupByStatus?.failed?.length }, + month, + ) + + updateOrCreate( + jsonData?.drowser?.metrics?.graphs?.test_coverage as Array< + Record + >, + 'name', + { name: month, count: getCoverage({ results: totalTests }) }, + month, + ) + + updateOrCreate( + jsonData?.drowser?.metrics?.graphs?.avg_test_duration as Array< + Record + >, + 'name', + { name: month, count: getAverageDuration({ results: totalTests }) }, + month, + ) + + updateOrCreate( + jsonData?.drowser?.metrics?.graphs + ?.flaky_tests as Array< + Record + >, + 'id', + { id: month, value: flakyTestsCount }, + month, + ) + jsonData.drowser.cases.push({ id: nanoid(), time: new Date().toISOString(), diff --git a/src/utils.ts b/src/utils.ts index 4ebe095..a65a746 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -89,6 +89,24 @@ const getFlaky = ({ flakyTests }: { flakyTests: Array }) => { return flakyTestCount } +const updateOrCreate = ( + arr: any[], + key: string, + newObj: any, + month: string, +) => { + if (Array.isArray(arr) && arr.length > 0) { + const index = arr.findIndex((item) => item[key] === month) + if (index !== -1) { + Object.assign(arr[index], newObj) + } else { + arr.push(newObj) + } + } else { + arr.push(newObj) + } +} + export { generateFileName, getAverageDuration, @@ -97,4 +115,5 @@ export { getTimestamp, humanizeDuration, isValidHttpUrl, + updateOrCreate, }