1

Use case

This is a Cypress E2E test coded with JS and I'm trying to compare pre-production and production sitemap URL contents in order to find the differences. I have two data sets (fixture) one for production and the other for test env.

Code snippet:

let compareUrlsBetween = (prodSitemapUrls, testEnvSitemapUrls) => { const pathFirstEnv = new Set(JSON.parse(prodSitemapUrls).map(url => (new URL(url)).pathname)) const pathSecondEnv = new Set(JSON.parse(testEnvSitemapUrls).map(url => (new URL(url)).pathname)) const diff = new Set(pathFirstEnv); for (const path of pathSecondEnv) { diff.delete(path); } return diff } // Check for differences if (compareUrlsBetween.length > 0) { let titi = typeof(compareUrlsBetween(prodSitemapUrls, testEnvSitemapUrls)) console.log(titi) cy.log('text : ' , compareUrlsBetween (prodSitemapUrls, testEnvSitemapUrls)) // Returns null //console.log(compareUrlsBetween(prodSitemapUrls, testEnvSitemapUrls)) //console.log('Production and test env sitemap urls are not ISO, ' + 'Here are the differences : ' , compareUrlsBetween (prodSitemapUrls, testEnvSitemapUrls)) //throw new Error() } else { expect(prodSitemapUrls).to.eq(testEnvSitemapUrls) } 

Test goal & the problem

Test goal is to fail the test in cas of diff between these two fixtures (.xml), throw a new error and show the diff as normal log (cy.log()). I've already tried multiple solutions like JSON.stringify(), data type conversion etc. but none of them solved my case.

Log I observe at this moment : logtext : , {}

PS: the other type of logs like console.log() or console.table() are working perfectly fine

Any help is much appreciated.

4
  • 1
    {} is clearly not null Commented Nov 10, 2022 at 16:20
  • Indeed @KonradLinkowski , it's not but I can't show the data as a normal log (cy.log), This is what I see but as console version : Set(2) {'/flux/sitemaps/sitemap_product_1.xml', '/flux/sitemaps/sitemap_product_2.xml'} Commented Nov 10, 2022 at 16:59
  • What about cy.log('text : ' , [...compareUrlsBetween (prodSitemapUrls, testEnvSitemapUrls)])? Commented Nov 10, 2022 at 17:05
  • 1
    Thank you very much @KonradLinkowski, it works like a charm, already tried this solution but I've forgotten to put ... before my function name :) Commented Nov 10, 2022 at 17:15

2 Answers 2

2

You can also use JSON.stringify. Although the dev console of Cypress will not keep any indentation you could configure, it gives you a full output.

E.g.: cy.log(JSON.stringify(response.body))

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this little trick was really helpful!!
1

Solution

Convert the set into an array

cy.log('text : ' , [...compareUrlsBetween (prodSitemapUrls, testEnvSitemapUrls)]) 

Why?

I'm not 100% sure, but it seems like cy.log uses JSON.stringify underneath which causes sets to be converted to {}

1 Comment

Thank you @KonradLinkowski for the explanation, I think you've said it right, just saw a similaire problematic in another disscution with the same reasoning. In my case I was trying to double JSON.stringify an object and that's why an empty string was returned which is an error, was good to know :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.