pause-poll
Version:
Promisify setTimeout & setInterval
37 lines (33 loc) • 1.19 kB
text/typescript
import { pause } from './main.js'
beforeEach(jest.useFakeTimers as any)
describe('pause', () => {
it('pauses for 1s and returns 1', async () => {
const mock = jest.spyOn(global, 'setTimeout')
const promise = pause(1000, () => 1)
jest.runAllTimers()
expect(await promise).toBe(1)
expect(mock).toBeCalledTimes(1)
expect(mock).lastCalledWith(expect.any(Function), 1000)
})
it('pauses for 1s and returns [1, 2, 3]', async () => {
const mock = jest.spyOn(global, 'setTimeout')
const promise = pause(1000, (a, b, c) => [a, b, c], 1, 2, 3)
jest.runAllTimers()
expect(await promise).toEqual([1, 2, 3])
expect(mock).toBeCalledTimes(1)
expect(mock).lastCalledWith(expect.any(Function), 1000)
})
// it('pauses for 1s and returns [1, 2, 3]', async () => {
// const data = await pause(1000, (a, b, c) => [a, b, c], 1, 2, 3)
// expect(data).toEqual([1, 2, 3])
// })
// it('pauses for 1s and throws Error', async () => {
// try {
// await pause(1000, () => {
// throw Error()
// })
// } catch (error) {
// expect(error).toEqual(Error())
// }
// })
})