diff --git a/test/build/deferred.spec.ts b/test/build/deferred.spec.ts new file mode 100644 index 0000000..dee0270 --- /dev/null +++ b/test/build/deferred.spec.ts @@ -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"); + }); +}); \ No newline at end of file diff --git a/test/src/deferred.spec.ts b/test/src/deferred.spec.ts new file mode 100644 index 0000000..f537dce --- /dev/null +++ b/test/src/deferred.spec.ts @@ -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() + setTimeout(() => resolve('test'), 10) + + expect(await promise).toBe('test') + }) + + it('should reject the promise after calling reject', async () => { + const { promise, reject } = deferred() + setTimeout(() => reject('test'), 10) + + await expect(promise).rejects.toBe('test') + }) +}) \ No newline at end of file