diff --git a/tests/index.test.ts b/src/index.test.ts similarity index 60% rename from tests/index.test.ts rename to src/index.test.ts index 818c804..8efa95e 100644 --- a/tests/index.test.ts +++ b/src/index.test.ts @@ -1,23 +1,25 @@ -import { it } from '../src/index' +import { describe, expect, test } from 'vitest' +import { it } from './index' describe('index.ts', () => { const resolve = { status: true } const reject = new Error('Give me a reason.') - const defaultReject = new Error('Rejection reason unspecified.') describe('result', () => { test('result should contain what promise resolves', async () => { const [res1] = await it(Promise.resolve(resolve)) - const [res2] = await it(Promise.all([Promise.resolve('one'), Promise.resolve('two')])) + const [res2] = await it( + Promise.all([Promise.resolve('one'), Promise.resolve('two')]), + ) expect(res1).toEqual(resolve) expect(res2).toEqual(['one', 'two']) }) - test('result should be `undefined` on rejection', async () => { + test('result should be `null` on rejection', async () => { const [res] = await it(Promise.reject()) - expect(res).toBeUndefined() + expect(res).toBeNull() }) }) @@ -28,13 +30,6 @@ describe('index.ts', () => { expect(err).toBeNull() }) - test('error should be `default` on rejection without a message', async () => { - const [_, err] = await it(Promise.reject()) - - expect(err).toBeInstanceOf(Error) - expect(err!.message).toBe(defaultReject.message) - }) - test('error should be what promise rejects', async () => { const [_, err] = await it(Promise.reject(reject)) diff --git a/src/index.ts b/src/index.ts index 2730cee..50dfea0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,15 +1,9 @@ type Ok = Readonly<[T, null]> -type Err = Readonly<[undefined, T]> +type Err = Readonly<[null, T]> type Result = Promise | Err> -export const it = ( - promise: Promise -): Result => { +export const it = (promise: Promise): Result => { return promise - .then>( - (res: T) => [res, null] - ) - .catch>( - (err?: Error) => [undefined, err || new Error('Rejection reason unspecified.')] - ) + .then>((res: T) => [res, null]) + .catch>((err: F) => [null, err]) }