From 1d7abce555d024bbae697133ff9329156461cfc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?La=C3=ADs=20Tomaz?= Date: Wed, 26 Aug 2020 08:16:16 +0200 Subject: [PATCH] fix: Throw error when assertion contains only one language chainer (#8105) Co-authored-by: Jennifer Shehane --- .../integration/commands/assertions_spec.js | 18 ++++++++++++++++++ packages/driver/src/cy/commands/asserting.js | 8 ++++++++ packages/driver/src/cypress/error_messages.js | 4 ++++ 3 files changed, 30 insertions(+) diff --git a/packages/driver/cypress/integration/commands/assertions_spec.js b/packages/driver/cypress/integration/commands/assertions_spec.js index a58fe9538134..fa2879c15250 100644 --- a/packages/driver/cypress/integration/commands/assertions_spec.js +++ b/packages/driver/cypress/integration/commands/assertions_spec.js @@ -431,6 +431,24 @@ describe('src/cy/commands/assertions', () => { cy.noop({}).should('dee.eq', {}) }) + describe('language chainers err', () => { + // https://github.com/cypress-io/cypress/issues/883 + const langChainers = ['to', 'be', 'been', 'is', 'that', 'which', 'and', 'has', 'have', 'with', 'at', 'of', 'same', 'but', 'does', 'still'] + + langChainers.forEach((langChainer) => { + it(`throws err when assertion contains only one language chainer: ${langChainer}`, (done) => { + cy.on('fail', (err) => { + expect(err.message).to.eq(`The chainer \`${langChainer}\` is a language chainer provided to improve the readability of your assertions, not an actual assertion. Please provide a valid assertion.`) + expect(err.docsUrl).to.eq('https://on.cypress.io/assertions') + + done() + }) + + cy.noop(true).should(langChainer, true) + }) + }) + }) + it('throws err when ends with a non available chainable', (done) => { cy.on('fail', (err) => { expect(err.message).to.eq('The chainer `eq2` was not found. Could not build assertion.') diff --git a/packages/driver/src/cy/commands/asserting.js b/packages/driver/src/cy/commands/asserting.js index 2db2de5e2ac1..9d2c9915adb4 100644 --- a/packages/driver/src/cy/commands/asserting.js +++ b/packages/driver/src/cy/commands/asserting.js @@ -110,6 +110,14 @@ module.exports = function (Commands, Cypress, cy, state) { throwAndLogErr(err) } + // https://github.com/cypress-io/cypress/issues/883 + // A single chainer used that is not an actual assertion, like '.should('be', 'true')' + if (chainers.length < 2 && !_.isFunction(memo[value]) && !isCheckingExistence) { + err = $errUtils.cypressErrByPath('should.language_chainer', { args: { originalChainers } }) + err.retry = false + throwAndLogErr(err) + } + return applyChainer(memo, value) }, exp) diff --git a/packages/driver/src/cypress/error_messages.js b/packages/driver/src/cypress/error_messages.js index 8671a5521cf0..15bf4cabf091 100644 --- a/packages/driver/src/cypress/error_messages.js +++ b/packages/driver/src/cypress/error_messages.js @@ -1361,6 +1361,10 @@ module.exports = { should: { chainer_not_found: 'The chainer `{{chainer}}` was not found. Could not build assertion.', + language_chainer: { + message: 'The chainer `{{originalChainers}}` is a language chainer provided to improve the readability of your assertions, not an actual assertion. Please provide a valid assertion.', + docsUrl: 'https://on.cypress.io/assertions', + }, eventually_deprecated: 'The `eventually` assertion chainer has been deprecated. This is now the default behavior so you can safely remove this word and everything should work as before.', },