Skip to content

Commit

Permalink
test(ts): add type tests
Browse files Browse the repository at this point in the history
- https://vitest.dev/guide/testing-types.html

Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
  • Loading branch information
unicornware committed Mar 12, 2023
1 parent f0a7eb1 commit 8a2b64f
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 15 deletions.
17 changes: 17 additions & 0 deletions src/__tests__/options-get-iterator-method.spec-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @file Type Tests - GetIteratorMethodOptions
* @module aggregate-error-ponyfill/tests/unit-d/GetIteratorMethodOptions
*/

import type getIteratorMethod from 'es-abstract/helpers/getIteratorMethod'
import type TestSubject from '../options-get-iterator-method'

describe('unit-d:GetIteratorMethodOptions', () => {
it('should equal Parameters<typeof getIteratorMethod>["0"]', () => {
// Arrange
type Expected = Parameters<typeof getIteratorMethod>['0']

// Expect
expectTypeOf<TestSubject>().toEqualTypeOf<Expected>
})
})
19 changes: 19 additions & 0 deletions src/__tests__/options.spec-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @file Type Tests - Options
* @module aggregate-error-ponyfill/tests/unit-d/Options
*/

import type { KeysRequired } from '@flex-development/tutils'
import type TestSubject from '../options'

describe('unit-d:Options', () => {
it('should allow empty object', () => {
expectTypeOf<KeysRequired<TestSubject>>().toBeNever()
})

it('should match [cause?: Cause | undefined]', () => {
expectTypeOf<TestSubject>()
.toHaveProperty('cause')
.toEqualTypeOf<unknown | undefined>()
})
})
24 changes: 24 additions & 0 deletions src/__tests__/ponyfill.spec-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @file Type Tests - AggregateError
* @module aggregate-error-ponyfill/tests/unit-d/AggregateError
*/

import type TestSubject from '../ponyfill'

describe('unit-d:AggregateError', () => {
it('should extend Error', () => {
expectTypeOf<TestSubject>().toMatchTypeOf<Error>()
})

it('should match [cause?: Cause | undefined]', () => {
expectTypeOf<TestSubject<any, string>>()
.toHaveProperty('cause')
.toEqualTypeOf<string | undefined>()
})

it('should match [errors: T[]]', () => {
expectTypeOf<TestSubject<Error | string>>()
.toHaveProperty('errors')
.toEqualTypeOf<(Error | string)[]>()
})
})
6 changes: 3 additions & 3 deletions src/__tests__/ponyfill.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/**
* @file Unit Tests - ponyfill
* @module aggregate-error-ponyfill/tests/unit/ponyfill
* @file Unit Tests - AggregateError
* @module aggregate-error-ponyfill/tests/unit/AggregateError
*/

import TestSubject from '../ponyfill'

describe('unit:ponyfill', () => {
describe('unit:AggregateError', () => {
it('should create spec-compliant AggregateError', () => {
// Arrange
const cause = new Error('The server responded with a 500 status')
Expand Down
2 changes: 1 addition & 1 deletion src/options-get-iterator-method.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @file AggregateError - GetIteratorMethodOptions
* @module aggregate-error-ponyfill/ponyfill/GetIteratorMethodOptions
* @module aggregate-error-ponyfill/GetIteratorMethodOptions
*/

import type getIteratorMethod from 'es-abstract/helpers/getIteratorMethod'
Expand Down
4 changes: 2 additions & 2 deletions src/options.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @file AggregateError - Options
* @module aggregate-error-ponyfill/ponyfill/Options
* @module aggregate-error-ponyfill/Options
*/

/**
Expand All @@ -19,7 +19,7 @@ interface Options<Cause = unknown> {
*
* @see https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error/cause
*/
cause?: Cause
cause?: Cause | undefined
}

export type { Options as default }
18 changes: 9 additions & 9 deletions src/ponyfill.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @file AggregateError - Ponyfill
* @module aggregate-error-ponyfill/ponyfill
* @file AggregateError
* @module aggregate-error-ponyfill/AggregateError
*/

import AdvanceStringIndex from 'es-abstract/2021/AdvanceStringIndex'
Expand All @@ -26,21 +26,21 @@ import type GetIteratorMethodOptions from './options-get-iterator-method'
* @see https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/AggregateError
*
* @template T - Aggregated error type
* @template C - Error cause type
* @template Cause - Error cause type
*
* @class
* @extends {Error}
*/
class AggregateError<T = any, C = unknown> extends Error {
class AggregateError<T = any, Cause = unknown> extends Error {
/**
* Error cause.
*
* @see https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error/cause
*
* @public
* @member {C | undefined} cause
* @member {Cause | undefined} cause
*/
public cause?: C
public cause?: Cause | undefined

/**
* @public
Expand All @@ -67,10 +67,10 @@ class AggregateError<T = any, C = unknown> extends Error {
*
* @param {Iterable<T>} errors - An iterable of errors
* @param {string} [message] - Human-readable description of the error
* @param {Options<C>} [options] - Error options
* @param {C} [options.cause] - The original cause of the error
* @param {Options<Cause>} [options] - Error options
* @param {Cause} [options.cause] - The original cause of the error
*/
constructor(errors: Iterable<T>, message?: string, options?: Options<C>) {
constructor(errors: Iterable<T>, message?: string, options?: Options<Cause>) {
super(message)

/**
Expand Down

0 comments on commit 8a2b64f

Please sign in to comment.