Skip to content

Instantly share code, notes, and snippets.

@Oziabr
Created December 8, 2022 01:33
Show Gist options
  • Save Oziabr/9abf3470cf3fe6fd337a73260d5d5496 to your computer and use it in GitHub Desktop.
Save Oziabr/9abf3470cf3fe6fd337a73260d5d5496 to your computer and use it in GitHub Desktop.
async testing with jest without extra squats
describe('sync', () => {
test('value', () => expect(100500).toBe(100500))
test('throw', () => expect(() => { throw 100500 }).toThrow('100500'))
})
describe('async', () => {
describe('.resolves', () => {
test('sleep', () => expect(
new Promise(resolve => setTimeout(() => resolve(100500), 1))
).resolves.toBe(100500))
test('instant', () => expect(
new Promise(resolve => resolve(100500))
).resolves.toBe(100500))
test('dummy', () => expect(
Promise.resolve(100500)
).resolves.toBe(100500))
test.skip('non promise', () => expect(100500).resolves.toBe(100500))
})
describe('.rejects', () => {
test('instant', () => expect(
new Promise((resolve, reject) => reject(100500))
).rejects.toBe(100500))
test('dummy', () => expect(
Promise.reject(100500)
).rejects.toBe(100500))
test('throw', () => expect(
new Promise(() => { throw 100500 })
).rejects.toBe(100500))
})
describe('callback', () => {
var stat, promisify, callbackify
beforeAll(() => ({ promisify, callbackify } = require('util')))
beforeAll(() => ({ stat } = require('fs')))
test('fs.stat as a promise', () => expect(
promisify(stat)('.')
).resolves.toEqual(expect.anything()))
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment