如何在 cy.log() 中打印对象?

use*_*223 3 javascript json object cypress

使用案例

这是一个用 JS 编码的 Cypress E2E 测试,我正在尝试比较预生产和生产站点地图 URL 内容,以找到差异。我有两个数据集(夹具),一个用于生产,另一个用于测试环境。

代码片段:

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)
                                }
Run Code Online (Sandbox Code Playgroud)

测试目标及问题

测试目标是使这两个装置 (.xml) 之间的差异的 cas 测试失败,引发新错误并将差异显示为正常日志 (cy.log())。我已经尝试过多种解决方案JSON.stringify(),例如数据类型转换等,但没有一个解决我的问题。

我此时观察到的日志:logtext : , {}

PS:其他类型的日志,如 console.log() 或 console.table() 工作得很好

任何帮助深表感谢。

Kon*_*owy 5

解决方案

将集合转换为数组

cy.log('text : ' , [...compareUrlsBetween (prodSitemapUrls, testEnvSitemapUrls)])
Run Code Online (Sandbox Code Playgroud)

为什么?

我不是 100% 确定,但似乎下面的cy.log用途JSON.stringify会导致集合转换为{}