From 9a6a02c6b11c501e1049c7fb1f0a407be6de8da6 Mon Sep 17 00:00:00 2001 From: John Harman Date: Tue, 27 Aug 2019 16:26:28 +0100 Subject: [PATCH 1/6] Creating a test that fails due to no retry-ability It seems to be that if you load a new page, the retry functionality that's baked in at the moment doesn't work. --- cypress/fixtures/test-app/index.html | 4 ++++ cypress/fixtures/test-app/next-page.html | 29 ++++++++++++++++++++++++ cypress/integration/commands.spec.js | 5 ++++ 3 files changed, 38 insertions(+) create mode 100644 cypress/fixtures/test-app/next-page.html diff --git a/cypress/fixtures/test-app/index.html b/cypress/fixtures/test-app/index.html index 867a217..897d82a 100644 --- a/cypress/fixtures/test-app/index.html +++ b/cypress/fixtures/test-app/index.html @@ -60,6 +60,10 @@

getAllByText

+
+

getByText on another page

+ Next Page +
diff --git a/cypress/integration/commands.spec.js b/cypress/integration/commands.spec.js deleted file mode 100644 index fb952ba..0000000 --- a/cypress/integration/commands.spec.js +++ /dev/null @@ -1,99 +0,0 @@ -describe('dom-testing-library commands', () => { - beforeEach(() => { - cy.visit('/') - }) - it('getByPlaceholderText', () => { - cy.getByPlaceholderText('Placeholder Text') - .click() - .type('Hello Placeholder') - }) - - it('getByLabelText', () => { - cy.getByLabelText('Label For Input Labelled By Id') - .click() - .type('Hello Input Labelled By Id') - }) - - it('getByAltText', () => { - cy.getByAltText('Image Alt Text').click() - }) - - it('getByTestId', () => { - cy.getByTestId('image-with-random-alt-tag').click() - }) - - it('getAllByText', () => { - cy.getAllByText(/^Jackie Chan/) - .should('have.length', 2) - .click({multiple: true}) - }) - - it('queryByText', () => { - cy.queryAllByText('Button Text').should('exist') - cy.queryByText('Non-existing Button Text', {timeout: 100}).should( - 'not.exist', - ) - }) - - it('getByText within', () => { - cy.get('#nested').within(() => { - cy.getByText('Button Text').click() - }) - }) - - it('getByText works when another page loads', () => { - cy.getByText('Next Page').click() - cy.getByText('New Page Loaded').should('exist') - }) - - it('getByText in container', () => { - return cy.get('#nested').then(subject => { - cy.getByText('Button Text', {container: subject}).click() - }) - }) - - // query* behaviour tested - it('queryByText can return no results message should not error', () => { - const text = 'Supercalifragilistic' - - cy.queryByText(text, {timeout: 100}).should('have.length', 0) - }) - - it('queryAllByText can return no results message should not error', () => { - const text = 'Supercalifragilistic' - - cy.queryAllByText(text, {timeout: 100}).should('have.length', 0) - }) - - it('queryAllByText with a should(\'exist\') must provide selector error message', () => { - const text = 'Supercalifragilistic' - const errorMessage = `expected 'queryAllByText(\`${text}\`)' to exist in the DOM` - cy.on('fail', err => { - expect(err.message).to.eq(errorMessage) - }) - - cy.queryAllByText(text, {timeout: 100}).should('exist') // NOT POSSIBLE WITH QUERYALL? - }) - - // get* behaviour tested - it('getByText should error if no elements are found', () => { - const regex = /Supercalifragilistic/ - const errorMessage = `Timed out retrying: Expected to find element: 'getByText(${regex})', but never found it.` - cy.on('fail', err => { - expect(err.message).to.eq(errorMessage) - }) - - cy.getByText(regex, {timeout: 100}) // Doesn't explicitly need .should('exist') if it's the last element? - }) - - it('getByText finding multiple items should error', () => { - const errorMessage = `Found multiple elements with the text: /^getByText/i\n\n(If this is intentional, then use the \`*AllBy*\` variant of the query (like \`queryAllByText\`, \`getAllByText\`, or \`findAllByText\`)).` - cy.on('fail', err => { - expect(err.message).to.eq(errorMessage) - }) - - cy.getByText(/^getByText/i) - }) -}) - -/* global cy */ diff --git a/cypress/integration/find.spec.js b/cypress/integration/find.spec.js new file mode 100644 index 0000000..3a43048 --- /dev/null +++ b/cypress/integration/find.spec.js @@ -0,0 +1,130 @@ +describe('find* dom-testing-library commands', () => { + beforeEach(() => { + cy.visit('/') + }) + + // Test each of the types of queries: LabelText, PlaceholderText, Text, DisplayValue, AltText, Title, Role, TestId + + it('findByLabelText', () => { + cy.findByLabelText('Label 1') + .click() + .type('Hello Input Labelled By Id') + }) + + it('findAllByLabelText', () => { + cy.findAllByLabelText(/^Label \d$/).should('have.length', 2) + }) + + it('findByPlaceholderText', () => { + cy.findByPlaceholderText('Input 1') + .click() + .type('Hello Placeholder') + }) + + it('findAllByPlaceholderText', () => { + cy.findAllByPlaceholderText(/^Input \d$/).should('have.length', 2) + }) + + it('findByText', () => { + cy.findByText('Button Text 1') + .click() + .should('contain', 'Button Clicked') + }) + + it('findAllByText', () => { + cy.findAllByText(/^Button Text \d$/) + .should('have.length', 2) + .click({ multiple: true }) + .should('contain', 'Button Clicked') + }) + + it('findByDisplayValue', () => { + cy.findByDisplayValue('Display Value 1') + .click() + .clear() + .type('Some new text') + }) + + it('findAllByDisplayValue', () => { + cy.findAllByDisplayValue(/^Display Value \d$/) + .should('have.length', 2) + }) + + it('findByAltText', () => { + cy.findByAltText('Image Alt Text 1').click() + }) + + it('findAllByAltText', () => { + cy.findAllByAltText(/^Image Alt Text \d$/).should('have.length', 2) + }) + + it('findByTitle', () => { + cy.findByTitle('Title 1').click() + }) + + it('findAllByTitle', () => { + cy.findAllByTitle(/^Title \d$/).should('have.length', 2) + }) + + it('findByRole', () => { + cy.findByRole('dialog').click() + }) + + it('findAllByRole', () => { + cy.findAllByRole(/^dialog/).should('have.length', 2) + }) + + it('findByTestId', () => { + cy.findByTestId('image-with-random-alt-tag-1').click() + }) + + it('findAllByTestId', () => { + cy.findAllByTestId(/^image-with-random-alt-tag-\d$/).should('have.length', 2) + }) + + /* Test the behaviour around these queries */ + + it('findByText with should(\'not.exist\')', () => { + cy.findAllByText(/^Button Text \d$/).should('exist') + cy.findByText('Non-existing Button Text', {timeout: 100}).should('not.exist') + }) + + it('findByText within', () => { + cy.get('#nested').within(() => { + cy.findByText('Button Text 2').click() + }) + }) + + it('findByText in container', () => { + return cy.get('#nested') + .then(subject => { + cy.findByText(/^Button Text/, {container: subject}).click() + }) + }) + + it('findByText works when another page loads', () => { + cy.findByText('Next Page').click() + cy.findByText('New Page Loaded').should('exist') + }) + + it('findByText should error if no elements are found', () => { + const regex = /Supercalifragilistic/ + const errorMessage = `Timed out retrying: Expected to find element: 'findByText(${regex})', but never found it.` + cy.on('fail', err => { + expect(err.message).to.eq(errorMessage) + }) + + cy.findByText(regex, {timeout: 100}) // Doesn't explicitly need .should('exist') if it's the last element? + }) + + it('findByText finding multiple items should error', () => { + const errorMessage = `Found multiple elements with the text: /^Button Text/i\n\n(If this is intentional, then use the \`*AllBy*\` variant of the query (like \`queryAllByText\`, \`getAllByText\`, or \`findAllByText\`)).` + cy.on('fail', err => { + expect(err.message).to.eq(errorMessage) + }) + + cy.findByText(/^Button Text/i) + }) +}) + +/* global cy */ diff --git a/cypress/integration/get.spec.js b/cypress/integration/get.spec.js new file mode 100644 index 0000000..122f76b --- /dev/null +++ b/cypress/integration/get.spec.js @@ -0,0 +1,26 @@ +describe('get* queries should error', () => { + beforeEach(() => { + cy.visit('/') + }) + + const queryPrefixes = ['By', 'AllBy'] + const queryTypes = ['LabelText', 'PlaceholderText', 'Text', 'DisplayValue', 'AltText', 'Title', 'Role', 'TestId'] + + queryPrefixes.forEach(queryPrefix => { + queryTypes.forEach(queryType => { + const obsoleteQueryName = `get${queryPrefix + queryType}`; + const preferredQueryName = `find${queryPrefix + queryType}`; + it(`${obsoleteQueryName} should error and suggest using ${preferredQueryName}`, () => { + + const errorMessage = `You used '${obsoleteQueryName}' which has been removed from Cypress Testing Library because it does not make sense in this context. Please use '${preferredQueryName}' instead.` + cy.on('fail', err => { + expect(err.message).to.eq(errorMessage) + }) + + cy[`${obsoleteQueryName}`]('Irrelevant') + }) + }) + }) +}) + +/* global cy */ \ No newline at end of file diff --git a/cypress/integration/query.spec.js b/cypress/integration/query.spec.js new file mode 100644 index 0000000..87eb41e --- /dev/null +++ b/cypress/integration/query.spec.js @@ -0,0 +1,152 @@ +describe('query* dom-testing-library commands', () => { + beforeEach(() => { + cy.visit('/') + }) + + // Test each of the types of queries: LabelText, PlaceholderText, Text, DisplayValue, AltText, Title, Role, TestId + + it('queryByLabelText', () => { + cy.queryByLabelText('Label 1') + .click() + .type('Hello Input Labelled By Id') + }) + + it('queryAllByLabelText', () => { + cy.queryAllByLabelText(/^Label \d$/).should('have.length', 2) + }) + + it('queryByPlaceholderText', () => { + cy.queryByPlaceholderText('Input 1') + .click() + .type('Hello Placeholder') + }) + + it('queryAllByPlaceholderText', () => { + cy.queryAllByPlaceholderText(/^Input \d$/).should('have.length', 2) + }) + + it('queryByText', () => { + cy.queryByText('Button Text 1') + .click() + .should('contain', 'Button Clicked') + }) + + it('queryAllByText', () => { + cy.queryAllByText(/^Button Text \d$/) + .should('have.length', 2) + .click({ multiple: true }) + .should('contain', 'Button Clicked') + }) + + it('queryByDisplayValue', () => { + cy.queryByDisplayValue('Display Value 1') + .click() + .clear() + .type('Some new text') + }) + + it('queryAllByDisplayValue', () => { + cy.queryAllByDisplayValue(/^Display Value \d$/) + .should('have.length', 2) + }) + + it('queryByAltText', () => { + cy.queryByAltText('Image Alt Text 1').click() + }) + + it('queryAllByAltText', () => { + cy.queryAllByAltText(/^Image Alt Text \d$/).should('have.length', 2) + }) + + it('queryByTitle', () => { + cy.queryByTitle('Title 1').click() + }) + + it('queryAllByTitle', () => { + cy.queryAllByTitle(/^Title \d$/).should('have.length', 2) + }) + + it('queryByRole', () => { + cy.queryByRole('dialog').click() + }) + + it('queryAllByRole', () => { + cy.queryAllByRole(/^dialog/).should('have.length', 2) + }) + + it('queryByTestId', () => { + cy.queryByTestId('image-with-random-alt-tag-1').click() + }) + + it('queryAllByTestId', () => { + cy.queryAllByTestId(/^image-with-random-alt-tag-\d$/).should('have.length', 2) + }) + + /* Test the behaviour around these queries */ + + it('queryByText with .should(\'not.exist\')', () => { + cy.queryAllByText(/^Button Text \d$/).should('exist') + cy.queryByText('Non-existing Button Text', {timeout: 100}).should('not.exist') + }) + + it('queryByText within', () => { + cy.get('#nested').within(() => { + cy.queryByText('Button Text 2').click() + }) + }) + + it('query* will return immediately, and never retry', () => { + cy.queryByText('Next Page').click() + + const errorMessage = `expected 'queryByText(\`New Page Loaded\`)' to exist in the DOM` + cy.on('fail', err => { + expect(err.message).to.eq(errorMessage) + }) + + cy.queryByText('New Page Loaded', { timeout: 300 }).should('exist') + }) + + it('query* in container', () => { + return cy.get('#nested') + .then(subject => { + cy.queryByText(/^Button Text/, {container: subject}).click() + }) + }) + + it('queryByText can return no result, and should not error', () => { + const text = 'Supercalifragilistic' + + cy.queryByText(text, {timeout: 100}) + .should('have.length', 0) + .and('not.exist') + }) + + it('queryAllByText can return no results message should not error', () => { + const text = 'Supercalifragilistic' + + cy.queryAllByText(text, {timeout: 100}) + .should('have.length', 0) + .and('not.exist') + }) + + it('queryAllByText with a should(\'exist\') must provide selector error message', () => { + const text = 'Supercalifragilistic' + const errorMessage = `expected 'queryAllByText(\`${text}\`)' to exist in the DOM` + cy.on('fail', err => { + expect(err.message).to.eq(errorMessage) + }) + + cy.queryAllByText(text, {timeout: 100}).should('exist') // NOT POSSIBLE WITH QUERYALL? + }) + + it('queryByText finding multiple items should error', () => { + const errorMessage = `Found multiple elements with the text: /^queryByText/i\n\n(If this is intentional, then use the \`*AllBy*\` variant of the query (like \`queryAllByText\`, \`getAllByText\`, or \`findAllByText\`)).` + cy.on('fail', err => { + expect(err.message).to.eq(errorMessage) + }) + + cy.queryByText(/^queryByText/i) + }) +}) + +/* global cy */ diff --git a/src/index.js b/src/index.js index 8fd659d..b2047ae 100644 --- a/src/index.js +++ b/src/index.js @@ -7,7 +7,41 @@ const getDefaultCommandOptions = () => { } } -const commands = Object.keys(queries).map(queryName => { +const queryNames = Object.keys(queries); + +const getRegex = /^get/; +const queryRegex = /^query/; +const findRegex = /^find/; + +const getQueryNames = queryNames.filter(q => getRegex.test(q)); +const queryQueryNames = queryNames.filter(q => queryRegex.test(q)); +const findQueryNames = queryNames.filter(q => findRegex.test(q)); + +const getCommands = getQueryNames.map(queryName => { + return { + name: queryName, + command: () => { + Cypress.log({ + name: queryName + }); + + throw new Error(`You used '${queryName}' which has been removed from Cypress Testing Library because it does not make sense in this context. Please use '${queryName.replace(getRegex, 'find')}' instead.`) + } + } +}) + +const queryCommands = queryQueryNames.map(queryName => { + return createCommand(queryName, queryName); +}) + +const findCommands = findQueryNames.map(queryName => { + // dom-testing-library find* queries use a promise to look for an element, but that doesn't work well with Cypress retryability + // Use the query* commands so that we can lean on Cypress to do the retry for us + // When it does return a null or empty array, Cypress will retry until the assertions are satisfied or the command times out + return createCommand(queryName, queryName.replace(findRegex, 'query')); +}) + +function createCommand(queryName, implementationName) { return { name: queryName, command: (...args) => { @@ -16,7 +50,7 @@ const commands = Object.keys(queries).map(queryName => { const waitOptions = typeof lastArg === 'object' ? {...defaults, ...lastArg} : defaults - const queryImpl = queries[queryName] + const queryImpl = queries[implementationName] const baseCommandImpl = doc => { const container = getContainer(waitOptions.container || doc) return queryImpl(container, ...args) @@ -41,21 +75,8 @@ const commands = Object.keys(queries).map(queryName => { .window({log: false}) .then((thenArgs) => { const getValue = () => { - let result; - try { - const value = commandImpl(thenArgs.document); - result = Cypress.$(value); - } - catch (err) { - // Catch exceptions where it can't find the element, so we can retry - if (/Unable to find an element with the text/.test(err.message)) - { - consoleProps.error = err; - result = Cypress.$(); - } else { - throw err; - } - } + const value = commandImpl(thenArgs.document); + const result = Cypress.$(value); // Overriding the selector of the jquery object because it's displayed in the long message of .should('exist') failure message // Hopefully it makes it clearer, because I find the normal response of "Expected to find element '', but never found it" confusing @@ -77,7 +98,8 @@ const commands = Object.keys(queries).map(queryName => { }) } - if (/queryBy|queryAllBy/.test(queryName)) { + if (queryRegex.test(queryName)) { + // For get* queries, do not retry return getValue(); } @@ -94,7 +116,7 @@ const commands = Object.keys(queries).map(queryName => { }) }, } -}) +} function filterInputs(value) { if (Array.isArray(value) && value.length === 0) { @@ -125,7 +147,13 @@ function queryArgument(args) { return input; } -export {commands} +const commands = [ + ...getCommands, + ...findCommands, + ...queryCommands +]; + +export { commands } /* eslint no-new-func:0, complexity:0 */ /* globals Cypress, cy */ From 4518d6f827592f6fc1b4086ed855a3ab8e66d886 Mon Sep 17 00:00:00 2001 From: "Kent C. Dodds" Date: Tue, 3 Sep 2019 22:48:40 -0600 Subject: [PATCH 6/6] Update index.js --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index bc3e420..3fcfb8f 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,4 @@ -import {configure, queries, waitForElement} from '@testing-library/dom' +import {configure, queries} from '@testing-library/dom' import {getContainer} from './utils' const getDefaultCommandOptions = () => {