Skip to content
This repository has been archived by the owner on May 24, 2021. It is now read-only.

Commit

Permalink
feat(ux): show retries info in terminal (#36)
Browse files Browse the repository at this point in the history
* feat(ux): show retries info in terminal
* fix for chalk upgrade
* add env var to disable log
* add test for logging to terminal
  • Loading branch information
kuceb authored Nov 25, 2019
1 parent 6e205f6 commit f1cd179
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 5 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ At the top of **`cypress/support/index.js`**:
require('cypress-plugin-retries')
```

#### Optional Installation
To enable retry logging in the terminal alongside mocha output
Inside **cypress/plugins/index.js**:
```js
module.exports = (on, config) => {
require('cypress-plugin-retries/lib/plugin')(on)
}
```
example output:
![](/docs/terminal_log.png)






### Usage

Expand Down Expand Up @@ -77,6 +92,7 @@ https://github.com/Bkucera/cypress-plugin-retries/issues/32

### Extra Configuration
- Use env var `RETRIES_HIDDEN=1` to hide previous attempts' command log entries (instead of marking them with an orange `x`)
- Use env var `RETRIES_NO_LOG=1` to omit logging to terminal in Cypress run mode (`(retry 1/3) ...`)

### License
[MIT](LICENSE)
1 change: 1 addition & 0 deletions cypress/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// the project's config changing)

module.exports = (on, config) => {
require('../../lib/plugin')(on)
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
}
3 changes: 2 additions & 1 deletion cypress/tests/e2e/after-hook-fail.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

/* EXPECT: {
totalFailed: 1,
totalPassed: 0
totalPassed: 0,
expectedStdout: ['(retry 1/2) expect fail\n (retry 2/2) expect fail'],
} */

describe('should not pass when really failed', () => {
Expand Down
Binary file added docs/terminal_log.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions lib/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const chalk = require('chalk')

module.exports = function (on) {
on('task', {
logRetry ([title, currentRetry, nestedDepth]) {
// eslint-disable-next-line no-console
console.log(`${chalk.yellow(`${' '.repeat(nestedDepth)}(retry ${currentRetry})`)} ${chalk.dim(title)}`)
},
})
}
19 changes: 19 additions & 0 deletions src/index.js → lib/support.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ const debug = function () {
// console.log.apply(this, arguments)
}

function getTestNestedDepth (test) {
let count = 0
let parent = test.parent

while (parent && count < 10) {
count++
parent = parent.parent
}

return count
}

const _top = top

const _clone = Cypress.mocha._mocha.Mocha.Test.prototype.clone
Expand All @@ -34,6 +46,13 @@ Cypress.mocha._mocha.Mocha.Test.prototype.clone = function () {
})
})

if (!Cypress.env('RETRIES_NO_LOG') && Cypress.config('isTextTerminal') && cy.state('runnable')) {
const nestedDepth = getTestNestedDepth(this)

cy.now('task', 'logRetry', [this.title, `${ret._currentRetry + 1}/${ret._retries}`, nestedDepth], { log: false })
.catch((e) => {})
}

setTimeout(() => {

const a = $('.runnable-active', _top.document).find('.runnable-state')
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "cypress-plugin-retries",
"version": "1.2.2",
"description": "Cypress plugin allowing tests to retry a configurable amount of times",
"main": "src",
"main": "lib/support",
"scripts": {
"lint": "eslint --ext json,js,ts .",
"test": "cypress run --spec '**/unit/**'",
Expand All @@ -13,16 +13,19 @@
"pre-commit": "lint-staged"
}
},
"dependencies": {
"chalk": "^3.0.0"
},
"devDependencies": {
"@babel/code-frame": "^7.5.5",
"@cypress/eslint-plugin-dev": "^4.0.0",
"@types/chalk": "^2.2.0",
"@types/fs-extra": "^8.0.0",
"@types/lodash": "^4.14.138",
"@typescript-eslint/eslint-plugin": "^2.2.0",
"@typescript-eslint/parser": "^2.2.0",
"bluebird": "^3.5.5",
"chai": "^4.2.0",
"chalk": "^2.4.2",
"cypress": "^3.4.1",
"debug": "^4.1.1",
"eslint": "^5.16.0",
Expand Down
20 changes: 18 additions & 2 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const path = require('path')
const { expect } = require('chai')
const bluebird = require('bluebird')
const Debug = require('debug')
const chalk = require('chalk').default
const chalk = require('chalk')
const stripAnsi = require('strip-ansi')
const cypress = require('cypress')

Expand Down Expand Up @@ -59,8 +59,13 @@ exports.runTest = async (options = {}) => {
snapshot: false,
spec: '',
expectedResults: {},
expectedStdout: null,
})

_.extend(opts, _.pick(parsedExpectedResults, _.keys(opts)))

parsedExpectedResults = _.omit(parsedExpectedResults, _.keys(opts))

const expectedResults = _.defaults({}, parsedExpectedResults, opts.expectedResults, {
totalFailed: 0,
})
Expand All @@ -72,16 +77,27 @@ exports.runTest = async (options = {}) => {
stdio.passThrough((v) => chalk.magenta(stripAnsi(v.toString())))
// const stdio2 = captureStdio(process.stdout)

let stdout

return cypress.run({
spec: opts.spec,
}).then((res) => {
expect(res).includes(expectedResults)
})
.finally(() => {
stdio.restore()
// console.log(chalk.magenta(stdio.toString()))
stdout = stdio.toString()
stdio.restore()
})
.then(() => {
if (opts.expectedStdout) {
_.forEach(opts.expectedStdout, (v) => {
expect(stdout).include(v)
console.log(`${chalk.bold('run matched stdout:')}\n${v}`)
})
}

// console.log(stdout)
console.log(`${chalk.bold('run matched these results:')} ${JSON.stringify(expectedResults, null, 2)}`)
})
}
Expand Down
52 changes: 52 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,18 @@
lodash "^4.17.4"
read-pkg-up "^6.0.0"

"@types/chalk@^2.2.0":
version "2.2.0"
resolved "https://registry.yarnpkg.com/@types/chalk/-/chalk-2.2.0.tgz#b7f6e446f4511029ee8e3f43075fb5b73fbaa0ba"
integrity sha512-1zzPV9FDe1I/WHhRkf9SNgqtRJWZqrBWgu7JGveuHmmyR9CnAPCie2N/x+iHrgnpYBIcCJWHBoMRv2TRWktsvw==
dependencies:
chalk "*"

"@types/color-name@^1.1.1":
version "1.1.1"
resolved "https://registry.yarnpkg.com/@types/color-name/-/color-name-1.1.1.tgz#1c1261bbeaa10a8055bbc5d8ab84b7b2afc846a0"
integrity sha512-rr+OQyAjxze7GgWrSaJwydHStIhHq2lvY3BOC2Mj7KnzI7XK0Uw1TOOdI9lDoajEbSWLiYgoo4f1R51erQfhPQ==

"@types/eslint-visitor-keys@^1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@types/eslint-visitor-keys/-/eslint-visitor-keys-1.0.0.tgz#1ee30d79544ca84d68d4b3cdb0af4f205663dd2d"
Expand Down Expand Up @@ -419,6 +431,14 @@ ansi-styles@^3.2.0, ansi-styles@^3.2.1:
dependencies:
color-convert "^1.9.0"

ansi-styles@^4.1.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-4.2.0.tgz#5681f0dcf7ae5880a7841d8831c4724ed9cc0172"
integrity sha512-7kFQgnEaMdRtwf6uSfUnVr9gSGC7faurn+J/Mv90/W+iTtN0405/nLdopfMWwchyxhbGYl6TC4Sccn9TUkGAgg==
dependencies:
"@types/color-name" "^1.1.1"
color-convert "^2.0.1"

ansicolors@~0.3.2:
version "0.3.2"
resolved "https://registry.yarnpkg.com/ansicolors/-/ansicolors-0.3.2.tgz#665597de86a9ffe3aa9bfbe6cae5c6ea426b4979"
Expand Down Expand Up @@ -765,6 +785,14 @@ chai@^4.2.0:
pathval "^1.1.0"
type-detect "^4.0.5"

chalk@*, chalk@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-3.0.0.tgz#3f73c2bf526591f574cc492c51e2456349f844e4"
integrity sha512-4D3B6Wf41KOYRFdszmDqMCGq5VV/uMAB273JILmO+3jAlh8X4qDtdtgCR3fxtbLEMzSx22QdhnDcJvu2u1fVwg==
dependencies:
ansi-styles "^4.1.0"
supports-color "^7.1.0"

[email protected], chalk@^2.0.0, chalk@^2.0.1, chalk@^2.1.0, chalk@^2.3.2, chalk@^2.4.1, chalk@^2.4.2:
version "2.4.2"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.4.2.tgz#cd42541677a54333cf541a49108c1432b44c9424"
Expand Down Expand Up @@ -932,11 +960,23 @@ color-convert@^1.9.0:
dependencies:
color-name "1.1.3"

color-convert@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/color-convert/-/color-convert-2.0.1.tgz#72d3a68d598c9bdb3af2ad1e84f21d896abd4de3"
integrity sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==
dependencies:
color-name "~1.1.4"

[email protected]:
version "1.1.3"
resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.3.tgz#a7d0558bd89c42f795dd42328f740831ca53bc25"
integrity sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=

color-name@~1.1.4:
version "1.1.4"
resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.4.tgz#c2a09a87acbde69543de6f63fa3995c826c536a2"
integrity sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==

[email protected]:
version "1.0.3"
resolved "https://registry.yarnpkg.com/colors/-/colors-1.0.3.tgz#0433f44d809680fdeb60ed260f1b0c262e82a40b"
Expand Down Expand Up @@ -2242,6 +2282,11 @@ has-flag@^3.0.0:
resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-3.0.0.tgz#b5d454dc2199ae225699f3467e5a07f3b955bafd"
integrity sha1-tdRU3CGZriJWmfNGfloH87lVuv0=

has-flag@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-4.0.0.tgz#944771fd9c81c81265c4d6941860da06bb59479b"
integrity sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==

has-symbols@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/has-symbols/-/has-symbols-1.0.0.tgz#ba1a8f1af2a0fc39650f5c850367704122063b44"
Expand Down Expand Up @@ -5430,6 +5475,13 @@ supports-color@^2.0.0:
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-2.0.0.tgz#535d045ce6b6363fa40117084629995e9df324c7"
integrity sha1-U10EXOa2Nj+kARcIRimZXp3zJMc=

supports-color@^7.1.0:
version "7.1.0"
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-7.1.0.tgz#68e32591df73e25ad1c4b49108a2ec507962bfd1"
integrity sha512-oRSIpR8pxT1Wr2FquTNnGet79b3BWljqOuoW/h4oBhxJ/HUbX5nX6JSruTkvXDCFMwDPvsaTTbvMLKZWSy0R5g==
dependencies:
has-flag "^4.0.0"

supports-hyperlinks@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/supports-hyperlinks/-/supports-hyperlinks-1.0.1.tgz#71daedf36cc1060ac5100c351bb3da48c29c0ef7"
Expand Down

0 comments on commit f1cd179

Please sign in to comment.