Skip to content

Instantly share code, notes, and snippets.

View arslantx's full-sized avatar

Arslan Haytiyev arslantx

  • Austin, TX
View GitHub Profile
@arslantx
arslantx / test.js
Created July 6, 2021 06:53 — forked from apieceofbart/test.js
Async testing with jest fake timers and promises
// Let's say you have a function that does some async operation inside setTimeout (think of polling for data)
function runInterval(callback, interval = 1000) {
setInterval(async () => {
const results = await Promise.resolve(42) // this might fetch some data from server
callback(results)
}, interval)
}
// Goal: We want to test that function - make sure our callback was called