diff --git a/README.md b/README.md index 1c88ddd..ccf879a 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,18 @@ If the dialog was invisible, the visibility assertion fails, and the rest of the ![Dialog was closed](./img/dialog-closed.png) +## Debugging + +This module uses [debug](https://github.com/debug-js/debug#readme) module to output verbose browser console messages when needed. To turn the logging on, open the browser's DevTools console and set the local storage entry: + +```js +localStorage.debug = 'cypress-if' +``` + +If you re-run the tests, you should see the messages appear in the console + +![Debug messages in the console](./img/debug.png) + ## Small print Author: Gleb Bahmutov <gleb.bahmutov@gmail.com> © 2022 diff --git a/img/debug.png b/img/debug.png new file mode 100644 index 0000000..fa096b3 Binary files /dev/null and b/img/debug.png differ diff --git a/package-lock.json b/package-lock.json index 79a7398..6a838ee 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,6 +8,9 @@ "name": "cypress-if", "version": "0.0.0-development", "license": "MIT", + "dependencies": { + "debug": "^4.3.4" + }, "devDependencies": { "cypress": "^10.6.0", "prettier": "^2.7.1", @@ -1375,7 +1378,6 @@ "version": "4.3.4", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", "integrity": "sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==", - "dev": true, "dependencies": { "ms": "2.1.2" }, @@ -3035,8 +3037,7 @@ "node_modules/ms": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", - "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", - "dev": true + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" }, "node_modules/neo-async": { "version": "2.6.2", @@ -8346,7 +8347,6 @@ "version": "4.3.4", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", "integrity": "sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==", - "dev": true, "requires": { "ms": "2.1.2" } @@ -9572,8 +9572,7 @@ "ms": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", - "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", - "dev": true + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" }, "neo-async": { "version": "2.6.2", diff --git a/package.json b/package.json index 7939355..c9f3c4f 100644 --- a/package.json +++ b/package.json @@ -24,5 +24,8 @@ "repository": { "type": "git", "url": "https://github.com/bahmutov/cypress-if.git" + }, + "dependencies": { + "debug": "^4.3.4" } } diff --git a/src/index.js b/src/index.js index fed0981..798b7eb 100644 --- a/src/index.js +++ b/src/index.js @@ -1,10 +1,12 @@ +const debug = require('debug')('cypress-if') + Cypress.Commands.add( 'if', { prevSubject: true }, function (subject, assertion) { const cmd = cy.state('current') - console.log('if', cmd.attributes, 'subject', subject) - console.log('next command', cmd.next) + debug('if', cmd.attributes, 'subject', subject) + debug('next command', cmd.next) const hasSubject = Boolean(subject) let assertionsPassed = true @@ -20,19 +22,19 @@ Cypress.Commands.add( if (!hasSubject || !assertionsPassed) { let nextCommand = cmd.attributes.next while (nextCommand) { - console.log( + debug( 'skipping the next "%s" command "%s"', nextCommand.attributes.type, nextCommand.attributes.name, ) // cy.log(`**skipping ${cmd.attributes.next.attributes.name}**`) - console.log('skipping "%s"', nextCommand.attributes.name) - console.log(nextCommand.attributes) + debug('skipping "%s"', nextCommand.attributes.name) + debug(nextCommand.attributes) nextCommand.attributes.skip = true nextCommand = nextCommand.attributes.next if (nextCommand && nextCommand.attributes.type === 'parent') { - console.log( + debug( 'stop skipping commands, see a parent command "%s"', nextCommand.attributes.name, ) @@ -52,27 +54,21 @@ Cypress.Commands.add( Cypress.Commands.overwrite('get', function (get, selector) { // can we see the next command already? const cmd = cy.state('current') - console.log(cmd) + debug(cmd) const next = cmd.attributes.next if (next && next.attributes.name === 'if') { // disable the built-in assertion return get(selector).then( (getResult) => { - console.log('internal get result', getResult) + debug('internal get result', getResult) return getResult }, (noResult) => { - console.log('no get result', noResult) - debugger + debug('no get result', noResult) }, ) } return get(selector) }) - -// Cypress.Commands.overwrite('click', function (click, subject) { -// console.log('my click', subject) -// return click(subject) -// })