Skip to content

Commit

Permalink
test: tests for deferred util
Browse files Browse the repository at this point in the history
Changelog: test
  • Loading branch information
mrspartak committed Jun 22, 2024
1 parent 28273ca commit d50875f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
8 changes: 8 additions & 0 deletions test/build/deferred.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { describe, expect, it } from "vitest";
import { deferred } from '../../dist/index.js'

describe("deferred", () => {
it("must be a function", () => {
expect(typeof deferred).toBe("function");
});
});
29 changes: 29 additions & 0 deletions test/src/deferred.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { describe, expect, it } from "vitest";
import { deferred } from '../../src/deferred.js'

describe('deferred', () => {
it('should be a function', () => {
expect(typeof deferred).toBe('function')
})

it('should return an object', () => {
const deferredObject = deferred()
expect(deferredObject.promise instanceof Promise).toBe(true)
expect(deferredObject.resolve instanceof Function).toBe(true)
expect(deferredObject.reject instanceof Function).toBe(true)
})

it('should resolve the promise after calling resolve', async () => {
const { promise, resolve } = deferred<string>()
setTimeout(() => resolve('test'), 10)

expect(await promise).toBe('test')
})

it('should reject the promise after calling reject', async () => {
const { promise, reject } = deferred<string>()
setTimeout(() => reject('test'), 10)

await expect(promise).rejects.toBe('test')
})
})

0 comments on commit d50875f

Please sign in to comment.