最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

webdriver io - How to make TypeScript happy when mapping array of WebdriverIO.Element[] to promises and awaiting for them? - Sta

programmeradmin0浏览0评论

I'm writing E2E tests in TypeScript and trying to keep its type system happy.

Initially I had the following code:

const textFromAllNoteParagraphs = $$('.markdown-preview-section p:not(.mod-ui)')
  .map(p => p.getText())
const textContent = (await textFromAllNoteParagraphs).join('\n\n')

While it does the job, TypeScript is not completely happy, because for the second expression it says:

'await' has no effect on the type of this expression. ts(80007)

Also, TypeScript-ESLint complains for the same reason:

Unexpected `await` of a non-Promise (non-"Thenable") value. eslint (@typescript-eslint/await-thenable)

It makes sense to me, because according to types declared textFromAllNoteParagraphs is Promise<string>[] (mapped from WebdriverIO.Element[] array).

Then I try to change the second expression to use Promise.all as follows:

const textContent = (await Promise.all(textFromAllNoteParagraphs)).join('\n\n')

TypeScript is now happy, but if I run my WDIO test, it simply gets stuck forever spinning my CPU (I even have to execute kill -9 to kill it). How to refactor the code to keep it working and make TypeScript happy in this situation?

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论