Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: run flaky test on driver and push to json reports #66

Merged
merged 1 commit into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions examples/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ driver({ browser: 'chrome' }).then(({ service }) => {
operator: 'assertEquals',
except: 'Drowser',
},
{
method: 'getTitle',
operator: 'assertEquals',
except: 'Drowsers',
},
{
method: 'getTitle',
operator: 'assertEquals',
except: 'Drowsers',
},
{
method: 'getTitle',
operator: 'assertEquals',
except: 'Drowsers',
},
]
}).catch((error) => {
console.log(error)
Expand Down
68 changes: 66 additions & 2 deletions src/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,18 +147,82 @@ const driver = async (

methodPromises.push(methodPromise)
} else {
console.error(`Method ${c.method} not found on builder object.`)
console.error(
`Method ${c.method} not found on builder object.`,
)
}
}

if (typeof c === 'function') {}
})

const flakyCases: Array<TDataResult> = []
;((runs: number) => {
for (let i = 0; i < runs; i++) {
service.cases.forEach((c: TDrowserServiceCase) => {
const start = performance.now()

if (typeof c === 'object') {
const method =
(builder as unknown as Record<string, Function>)[c.method]

if (typeof method === 'function') {
const methodPromise = method.call(builder)
let actualValue: unknown = null

methodPromise.then((v: unknown) => {
const assertFunction = assert[c.operator] as TAssertFunction
actualValue = v
assertFunction(actualValue, c.except)

const end = performance.now()

flakyCases.push(
result({
id: nanoid(),
name: c.method,
actual: actualValue,
exceptation: c.except,
status: caseStatus.passed,
timestamp: new Date(),
duration: end - start,
}),
)
})
.catch(() => {
const end = performance.now()

flakyCases.push(
result({
id: nanoid(),
name: c.method,
actual: actualValue,
exceptation: c.except,
status: caseStatus.failed,
timestamp: new Date(),
duration: end - start,
}),
)
})

methodPromises.push(methodPromise)
} else {
console.error(
`Method ${c.method} not found on builder object.`,
)
}
}

if (typeof c === 'function') {}
})
}
})(5)

Promise.all(methodPromises)
.then(() => {
if (exportPdf) exportGeneratedPdf({ results: data.results })
exportGeneratedLog({ results: data.results })
exportJSONReport({ results: data.results })
exportJSONReport({ results: data.results, flakyTests: flakyCases })
})
.catch((error) => {
console.error('An error occurred while processing promises:', error)
Expand Down
29 changes: 28 additions & 1 deletion src/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ const exportGeneratedPdf = (
}

const exportJSONReport = (
{ results }: { results: Array<TDataResult> },
{ results, flakyTests }: {
results: Array<TDataResult>
flakyTests: Array<TDataResult>
},
): void => {
const filePath = join(Deno.cwd(), 'drowser-reports.json')
const hasFile = existsSync(filePath)
Expand All @@ -105,11 +108,35 @@ const exportJSONReport = (
).length
const coveragePercentage = (passedTests / totalTests) * 100

const flakyTestCount = (() => {
const resultMap = new Map<string, any[]>()

flakyTests.forEach((result) => {
const resultsForTest = resultMap.get(result.name) || []
resultsForTest.push(result.status)
resultMap.set(result.name, resultsForTest)
})

let flakyCount = 0

resultMap.forEach((results) => {
if (
results.includes(caseStatus.passed) &&
results.includes(caseStatus.failed)
) {
flakyCount++
}
})

return flakyCount
})()

jsonData.drowser.cases.push({
id: nanoid(),
time: new Date().toISOString(),
avg_duration: averageDuration,
coverage: coveragePercentage,
flaky: flakyTestCount,
cases: results,
})

Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ export type TJSON = {
{
id: string
time: string
avg_duration: string
avg_duration: number
coverage: number
flaky: number
cases: Array<TDataResult>
},
]
Expand Down
Loading