Promise.allSettled 中多个选择器的 TestCafe 断言似乎具有误导性

Posted

技术标签:

【中文标题】Promise.allSettled 中多个选择器的 TestCafe 断言似乎具有误导性【英文标题】:TestCafe assertions over multiple selectors in Promise.allSettled seems misleading 【发布时间】:2022-01-09 18:14:50 【问题描述】:

考虑以下测试:

import  Selector, t  from 'testcafe'


const validSelector = Selector('html')
const invalidSelector = Selector('non-existent')

//the following test passes and establishes a baseline for the assertions below
test
  ('baseline', async t => 
    await t
      .expect(validSelector.exists).ok()
      .expect(invalidSelector.exists).notOk()
)

//here the first set of assertions pass, while the second fail
test
  ('Promise.allSettled is misleading', async t => 
    let selectors = [validSelector, validSelector, invalidSelector]
    let expected = ['fulfilled', 'fulfilled', 'rejected']
    await Promise.allSettled(selectors.map(async selector => await t.expect(selector.exists).ok()))
      .then(async o => await t.expect(JSON.stringify(o.map(result => result.status)) === JSON.stringify(expected)).ok())

    selectors = [validSelector, invalidSelector, validSelector]
    expected = ['fulfilled', 'rejected', 'fulfilled']
    await Promise.allSettled(selectors.map(async selector => await t.expect(selector.exists).ok()))
      .then(async o => await t.expect(JSON.stringify(o.map(result => result.status)) === JSON.stringify(expected)).ok(JSON.stringify(o.map(result => result.status))))
)


const handleExceptions = async (selector) =>
  await t.expect(selector.exists).ok()
    .then(f => true)
    .catch(r => false)

//here all of the promises are fulfilled due to the exception handling,
//but as one may expect the results are the same as in the previous test -- the assertion at the end fails
test
  ('Promise.allSettled with exception handling is still misleading', async t => 
    const selectors = [validSelector, invalidSelector, validSelector]
    const expected = [true, false, true]
    await Promise.allSettled(selectors.map(async selector => await handleExceptions(selector)))
      .then(async o => await t.expect(JSON.stringify(o) === JSON.stringify(expected)).ok(JSON.stringify(o)))
)

//in this test the assertion actually passes
test
  ('Snapshots seem like the only viable option', async t => 
    const tags = ['html', 'non-existent', 'html']
    const expected = [true, false, true]
    await Promise.allSettled(tags.map(async tag => await Selector(tag)))
      .then(async selectors => await Promise.allSettled(selectors.map(async s => await s.value())))
      .then(snapshots => snapshots.map(snapshot => snapshot.value != null))
      .then(async o => await t.expect(JSON.stringify(o) === JSON.stringify(expected)).ok(JSON.stringify(o)))
)

问题是——由于 TestCafe 的一些好处或上面的用法有任何错误,在上述情况下所描述的 Promise.allSettled 行为是预期的吗?

如果是前者,那么用户应该按照上次测试的建议采用快照路线还是有其他可行的选择(可能更安全、更快等)?

【问题讨论】:

【参考方案1】:

这个问题似乎与以下开放的 TestCafe 问题有关:https://github.com/DevExpress/testcafe/issues/5623。请投票并订阅通知以了解我们的进展。

【讨论】:

以上是关于Promise.allSettled 中多个选择器的 TestCafe 断言似乎具有误导性的主要内容,如果未能解决你的问题,请参考以下文章

等到内心的承诺完成

[Javascript] Wait for Multiple JavaScript Promises to Settle with Promise.allSettled()

什么是 Promise.allSettled() !新手老手都要会?

Promise.all的异常处理,前端开发者需了解

Promise.all的异常处理,前端开发者需了解

带你快速入门Promise对象