testing-library / cypress-testing-library

🐅 Simple and complete custom Cypress commands and utilities that encourage good testing practices.
http://npm.im/@testing-library/cypress
MIT License
1.8k stars 152 forks source link

Normalizer access within Cypress #147

Closed dwineknott closed 4 years ago

dwineknott commented 4 years ago

Relevant code or config

 it('Manage Songwriters - Verify elements on page', () => {
        cy.visit('/songwriters/')
        cy.wait('@getWriters').then((xhr) => {
            const { objects } = xhr.responseBody as RootObject;
            }

        cy.get('.manage-writer-view > :nth-child(1) > #writer-list > li')
            .should('have.length', 10)
            .each(($writerList, index) => {
            const writer: Object  = objects[index]

       cy.wrap($writerList).within(() => {
                    cy.findAllByText(RegExp(writer.full_name, "i"),
                    {normalizer: getDefaultNormalizer({collapseWhitespace: true})} );

                    //iterate over each element within each `.songwriter`

                });

What you did: Attempting to call getDefaultNormalizer from within the above script portion.

What happened:

Getting from my IDE:

Cannot find name 'getDefaultNormalizer'.

Reproduction repository: N/A

Problem description: Is this Normalizer call available via the cypress-testing-library? I don't see any documentation regarding this feature specific to Cypress. I have read https://testing-library.com/docs/dom-testing-library/api-queries#normalization . I'm hoping its a simple human error issue with the syntax I am using and this isn't a bug?

Suggested solution: Extend access to this call within Cypress.

kentcdodds commented 4 years ago

Hello @dwineknott,

You'll have to import it directly from @testing-library/dom

import {getDefaultNormalizer} from '@testing-library/dom'

I hope that helps!

dwineknott commented 4 years ago

Hi @kentcdodds . Thank you!

This indeed resolved my initial issue, but I am now getting the following in the console when running my script. In turn the new string is not being returned from here. Any guidance you could provide would be much appreciated:

cy.findAllByText(RegExp(writer.full_name, "i"),
                        {normalizer: str => getDefaultNormalizer ({trim: true})(str).replace(/^\s+|\s+$|\s+(?=\s)/g,"")}

                    )

Error from console:

Screen Shot 2020-08-13 at 5 16 57 PM