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

[FIX-#219] add conditions to avoid log file is still generated after succe… #288

Merged
merged 2 commits into from
Mar 10, 2022
Merged
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
43 changes: 26 additions & 17 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ 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 () {
if (typeof Cypress.config('retries') === 'number') {
return Cypress.config('retries')
} else if (typeof Cypress.config('retries')['runMode'] === 'number') {
return Cypress.config('retries')['runMode'];
}
return 0;
}

const failedCaseTable = {}

function writeFailedTestInfo ({
specName,
Expand Down Expand Up @@ -104,14 +116,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 test case retry times
if (failedCaseTable[testName]) {
failedCaseTable[testName]++
} else {
failedCaseTable[testName] = 1
}
doneWithTest(testName)

const title = this.currentTest.title

Expand Down Expand Up @@ -150,8 +162,13 @@ 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
if (failedCaseTable[testName] - 1 === retriesTimes || retriesTimes === 0) {
const filepath = writeFailedTestInfo(info)
info.filepath = filepath
}

cy.task('failed', info, { log: false })
}
Expand All @@ -164,14 +181,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 */
Expand All @@ -192,4 +201,4 @@ afterEach = (name, fn) => {
startLogging()
beforeEach(initLog)
// register our callback to process failed tests without wrapping
_afterEach(onFailed)
_afterEach(onFailed)