Skip to content

Commit

Permalink
fix: do not save log file unless it is the last attempt (#290)
Browse files Browse the repository at this point in the history
* [FIX-#219] add conditions to avoid log file is still generated after succe… (#288)

* feat: add conditions to avoid log file is still generated after successful retry

* feat: add conditions to avoid log file is still generated after successful retry

* refactoring

* more refactoring

Co-authored-by: Cliff Su <[email protected]>
  • Loading branch information
bahmutov and stu01509 authored Mar 10, 2022
1 parent 06f070c commit f55eb22
Showing 1 changed file with 34 additions and 18 deletions.
52 changes: 34 additions & 18 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@ const truncateFilename = s => Cypress._.truncate(s, {
})
const getCleanFilename = s => truncateFilename(cleanupFilename(s))
const getFilepath = filename => path.join('cypress', 'logs', filename)
const retriesTimes = getRetriesTimes()

function getRetriesTimes () {
const retries = Cypress.config('retries')
if (Cypress._.isNumber(retries)) {
return retries
}

if (Cypress._.isObject(retries) && Cypress._.isNumber(retries.runMode)) {
return retries.runMode
}

return 0
}

const failedCaseTable = {}

function writeFailedTestInfo ({
specName,
Expand Down Expand Up @@ -89,7 +105,7 @@ function startLogging () {
// getting an event some time after the command finishes.
// Still better to have approximate value than nothing
options.wallClockStoppedAt = Date.now()
options.duration = +options.wallClockStoppedAt - (+ new Date(options.wallClockStartedAt))
options.duration = +options.wallClockStoppedAt - (+new Date(options.wallClockStartedAt))
options.consoleProps.Duration = options.duration
}
})
Expand All @@ -104,14 +120,14 @@ function onFailed () {
if (this.currentTest.state === 'passed') {
return
}

const testName = this.currentTest.fullTitle()
// prevents processing failed test twice - from our "afterEach" callback
// and from wrapping user "afterEach"
if (hasSeen(testName)) {
return

// remember the test case retry times
if (failedCaseTable[testName]) {
failedCaseTable[testName] += 1
} else {
failedCaseTable[testName] = 1
}
doneWithTest(testName)

const title = this.currentTest.title

Expand Down Expand Up @@ -150,8 +166,17 @@ function onFailed () {
testError,
testCommands
}
const filepath = writeFailedTestInfo(info)
info.filepath = filepath

// If finally retry still failed or we didn't set the retry value in cypress.json
// directly to write the failed log
const lastAttempt = failedCaseTable[testName] - 1 === retriesTimes
const noRetries = retriesTimes === 0
debug('no retries %o last attempt %o', noRetries, lastAttempt)
if (noRetries || lastAttempt) {
const filepath = writeFailedTestInfo(info)
debug('saving the log file %s', filepath)
info.filepath = filepath
}

cy.task('failed', info, { log: false })
}
Expand All @@ -164,15 +189,6 @@ function onFailed () {
// "afterEach" function with our callback "onFailed". This ensures we run
// first.

// remember which tests we have processed already
const seenTests = {}
function hasSeen (testName) {
return seenTests[testName]
}
function doneWithTest (testName) {
seenTests[testName] = true
}

const _afterEach = afterEach
/* eslint-disable-next-line no-global-assign */
afterEach = (name, fn) => {
Expand Down

0 comments on commit f55eb22

Please sign in to comment.