Support Async Tests with JavaScripts Promises through async await

Support Async Tests with JavaScripts Promises through async await

Our testing framework works great for our synchronous test. What if we had some asynchronous functions that we wanted to test? We could make our callback functions async, and then use the await keyword to wait for that to resolve, then we can make our assertion on the result and the expected.

Let’s make our testing framework support promises so users can use async/await.

提取碼:36e3

觀看視頻

Code

Transcript

Our testing framework works great for our synchronous test. What if we had some asynchronous functions that we wanted to test? We could make our callback functions async, and then use the await keyword to wait for that to resolve, then we can make our assertion on the result and the expected.

async-await.js

test('sumAsync adds numbers asynchronously', async () => {
  const result = await sumAsync(3, 7)
  const expected = 10
  expect(result).toBe(expected)
})

test('subtractAsync subtracts numbers asynchronously', async () => {
  const result = await subtractAsync(7, 3)
  const expected = 4
  expect(result).toBe(expected)
})

This approach has a little bit of a problem though. If we run our test, we are going to see that they both pass, and then after that, we have an UnhandledPromiseRejectionWarning. That is the actual error coming from our sumAsync function being broken.

image

Because this is an async function, this function will return a promise. When this error is thrown, it's going to reject that promise. Here inside of our test function, this callback is going to return a promise. If we turn this test into an async function, and then await that callback, if that promise is rejected, then we'll land in our catch block.

If no error is thrown, then we'll continue on inside the try block. This will work for both our synchronous and our asynchronous tests.

async function test(title, callback) {
  try {
    await callback()
    console.log(`? ${title}`)
  } catch (error) {
    console.error(`? ${title}`)
    console.error(error)
  }
}

With that, we can run our test again, and things happen exactly as we expect.

image

star此項(xiàng)目

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末攘须,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,185評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件勉抓,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)澎羞,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來敛苇,“玉大人妆绞,你說我怎么就攤上這事。” “怎么了摆碉?”我有些...
    開封第一講書人閱讀 163,524評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵塘匣,是天一觀的道長。 經(jīng)常有香客問我巷帝,道長忌卤,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,339評(píng)論 1 293
  • 正文 為了忘掉前任楞泼,我火速辦了婚禮驰徊,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘堕阔。我一直安慰自己棍厂,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,387評(píng)論 6 391
  • 文/花漫 我一把揭開白布超陆。 她就那樣靜靜地躺著牺弹,像睡著了一般。 火紅的嫁衣襯著肌膚如雪时呀。 梳的紋絲不亂的頭發(fā)上张漂,一...
    開封第一講書人閱讀 51,287評(píng)論 1 301
  • 那天,我揣著相機(jī)與錄音谨娜,去河邊找鬼航攒。 笑死,一個(gè)胖子當(dāng)著我的面吹牛趴梢,可吹牛的內(nèi)容都是我干的漠畜。 我是一名探鬼主播,決...
    沈念sama閱讀 40,130評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼坞靶,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼憔狞!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起滩愁,我...
    開封第一講書人閱讀 38,985評(píng)論 0 275
  • 序言:老撾萬榮一對(duì)情侶失蹤躯喇,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后硝枉,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體廉丽,經(jīng)...
    沈念sama閱讀 45,420評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,617評(píng)論 3 334
  • 正文 我和宋清朗相戀三年妻味,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了正压。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,779評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡责球,死狀恐怖焦履,靈堂內(nèi)的尸體忽然破棺而出拓劝,到底是詐尸還是另有隱情,我是刑警寧澤嘉裤,帶...
    沈念sama閱讀 35,477評(píng)論 5 345
  • 正文 年R本政府宣布郑临,位于F島的核電站,受9級(jí)特大地震影響屑宠,放射性物質(zhì)發(fā)生泄漏厢洞。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,088評(píng)論 3 328
  • 文/蒙蒙 一典奉、第九天 我趴在偏房一處隱蔽的房頂上張望躺翻。 院中可真熱鬧,春花似錦卫玖、人聲如沸公你。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,716評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽陕靠。三九已至,卻和暖如春笨触,著一層夾襖步出監(jiān)牢的瞬間懦傍,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,857評(píng)論 1 269
  • 我被黑心中介騙來泰國打工芦劣, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人说榆。 一個(gè)月前我還...
    沈念sama閱讀 47,876評(píng)論 2 370
  • 正文 我出身青樓虚吟,卻偏偏與公主長得像,于是被迫代替她去往敵國和親签财。 傳聞我的和親對(duì)象是個(gè)殘疾皇子串慰,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,700評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容