Skip to content

Commit

Permalink
refactor: ♻️ Simplify unit tests for each of the helper types
Browse files Browse the repository at this point in the history
  • Loading branch information
CRBroughton committed Jun 4, 2024
1 parent 9032eae commit 0b06d6e
Show file tree
Hide file tree
Showing 18 changed files with 174 additions and 237 deletions.
5 changes: 5 additions & 0 deletions .changeset/empty-mice-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@crbroughton/ts-test-utils": patch
---

Simplify unit tests for each of the helper types
2 changes: 0 additions & 2 deletions .github/workflows/bun.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ jobs:
bun-version: latest
- name: Install dependencies
run: bun install
- name: Run unit tests
run: bun test
- name: Run typechecker
run: bun run typecheck
- uses: actions/upload-artifact@v3
Expand Down
20 changes: 8 additions & 12 deletions tests/Assignable.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
/* eslint-disable unused-imports/no-unused-vars */
import { describe, it } from 'bun:test'
import type { Assignable, Expect } from '..'

describe('Assignable tests', () => {
it('Passes the assignable test when the first type is assignable', () => {
type Result = Expect<Assignable<{ id: number | string }, { id: number, name: string }>>
// ^?
})
it('Failed the assignable test when the first type is not assignable', () => {
// @ts-expect-error - Fails the exclusion
type Result = Expect<Assignable<{ id: string }, { id: number, name: string }>>
// ^?
})
})
// Passes the assignable test when the first type is assignable
type Result = Expect<Assignable<{ id: number | string }, { id: number, name: string }>>
// ^?

// Failed the assignable test when the first type is not assignable
// @ts-expect-error - Fails the exclusion
type ResultFailure = Expect<Assignable<{ id: string }, { id: number, name: string }>>
// ^?
30 changes: 13 additions & 17 deletions tests/Equals.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
/* eslint-disable unused-imports/no-unused-vars */
import { describe, it } from 'bun:test'
import type { Equals, Expect } from '..'

describe('Equals tests', () => {
it('Passes the equality test when the types are equal', () => {
type ResultBoolean = Expect<Equals<true, true>>
// ^?
type ResultRecord = Expect<Equals<{ id: number }, { id: number }>>
// ^?
})
it('Fails the equality test when the types are not equal', () => {
// @ts-expect-error - Boolean values failing the equality checker
type ResultBoolean = Expect<Equals<true, false>>
// ^?
// @ts-expect-error - Object / Record failing the equality checker
type ResultRecord = Expect<Equals<{ id: number }, { id: string }>>
// ^?
})
})
// Passes the equality test when the types are equal
type ResultBoolean = Expect<Equals<true, true>>
// ^?
type ResultRecord = Expect<Equals<{ id: number }, { id: number }>>
// ^?

// Fails the equality test when the types are not equal
// @ts-expect-error - Boolean values failing the equality checker
type ResultBooleanFailure = Expect<Equals<true, false>>
// ^?
// @ts-expect-error - Object / Record failing the equality checker
type ResultRecordFailure = Expect<Equals<{ id: number }, { id: string }>>
// ^?
32 changes: 13 additions & 19 deletions tests/Excludes.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
/* eslint-disable unused-imports/no-unused-vars */
import { describe, it } from 'bun:test'
import type { Excludes, Expect } from '..'

describe('Excludes tests', () => {
it('Passes the excludes test when the resulting type does not include the sub-type', () => {
type Result = Expect<Excludes<{ id: number, name: string }, { job: string }>>
// ^?
interface FullType { id: number, name: string, location: { city: 'Brighton' } }
interface ExcludedType { hobby: string }
type ResultNestedRecord = Expect<Excludes<FullType, ExcludedType>
// ^?
>
})
it('Failed the excludes test when the resulting type does include the sub-type', () => {
interface FullType { id: number, name: string, location: { city: 'Brighton' } }
interface ExcludedType { location: { city: 'Brighton' } }
// @ts-expect-error - Fails the exclusion
type ResultNestedRecord = Expect<Excludes<FullType, ExcludedType>>
// ^?
})
})
// Passes the excludes test when the resulting type does not include the sub-type
type Result = Expect<Excludes<{ id: number, name: string }, { job: string }>>
// ^?
interface FullType { id: number, name: string, location: { city: 'Brighton' } }
interface ExcludedType { hobby: string }
type ResultNestedRecord = Expect<Excludes<FullType, ExcludedType>>
// ^?

// Failed the excludes test when the resulting type does include the sub-type
interface ExcludedTypeFailure { location: { city: 'Brighton' } }
// @ts-expect-error - Fails the exclusion
type ResultNestedRecordFailure = Expect<Excludes<FullType, ExcludedTypeFailure>>
// ^?
24 changes: 10 additions & 14 deletions tests/Extends.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
/* eslint-disable unused-imports/no-unused-vars */
import { describe, it } from 'bun:test'
import type { Expect, Extends } from '..'

describe('Extends tests', () => {
it('Passes the extend test when the resulting type extends another type', () => {
interface BaseType { id: number }
type ExtensionType = BaseType & { name: string }
type Result = Expect<Extends<BaseType, ExtensionType>>
// ^?
})
it('Failed the extends test when the resulting type does not extend the sub-type', () => {
// @ts-expect-error - Object / Record failing the equality checker
type Result = Expect<Extends<{ id: number, name: string }, { hobby: string }>>
// ^?
})
})
// Passes the extend test when the resulting type extends another type
interface BaseType { id: number }
type ExtensionType = BaseType & { name: string }
type Result = Expect<Extends<BaseType, ExtensionType>>
// ^?

// Failed the extends test when the resulting type does not extend the sub-type
// @ts-expect-error - Object / Record failing the equality checker
type ResultFailure = Expect<Extends<{ id: number, name: string }, { hobby: string }>>
// ^?
20 changes: 8 additions & 12 deletions tests/Includes.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
/* eslint-disable unused-imports/no-unused-vars */
import { describe, it } from 'bun:test'
import type { Expect, Includes } from '..'

describe('Includes tests', () => {
it('Passes the includes test when the resulting type includes the sub-type', () => {
type Result = Expect<Includes<{ id: number, name: string }, { name: string }>>
// ^?
})
it('Failed the includes test when the resulting type does not include the sub-type', () => {
// @ts-expect-error - Object / Record failing the equality checker
type Result = Expect<Includes<{ id: number, name: string }, { hobby: string }>>
// ^?
})
})
// Passes the includes test when the resulting type includes the sub-type
type Result = Expect<Includes<{ id: number, name: string }, { name: string }>>
// ^?

// Failed the includes test when the resulting type does not include the sub-type
// @ts-expect-error - Object / Record failing the equality checker
type ResultFailure = Expect<Includes<{ id: number, name: string }, { hobby: string }>>
// ^?
20 changes: 8 additions & 12 deletions tests/IsNonArray.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
/* eslint-disable unused-imports/no-unused-vars */
import { describe, it } from 'bun:test'
import type { Expect, IsNonArray } from '../index'

describe('IsNonArray tests', () => {
it('Passes the IsNonArray test when the type is not an array', () => {
type Result = Expect<IsNonArray<{ id: number }>>
// ^?
})
it('Failed the IsNonArray test when the type is an array', () => {
// @ts-expect-error - Fails the exclusion
type Result = Expect<IsNonArray<{ id: string }[]>>
// ^?
})
})
// Passes the IsNonArray test when the type is not an array
type Result = Expect<IsNonArray<{ id: number }>>
// ^?

// Failed the IsNonArray test when the type is an array
// @ts-expect-error - Fails the exclusion
type ResultFailure = Expect<IsNonArray<{ id: string }[]>>
// ^?
20 changes: 8 additions & 12 deletions tests/IsNonNullable.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
/* eslint-disable unused-imports/no-unused-vars */
import { describe, it } from 'bun:test'
import type { Expect, IsNonNullable } from '../index'

describe('IsNonNulable tests', () => {
it('Passes the IsNonNulable test when the type is not nullable', () => {
type Result = Expect<IsNonNullable<{ id: number }>>
// ^?
})
it('Failed the IsNonNulable test when the type is nullable', () => {
// @ts-expect-error - Fails the IsNonNulable test
type Result = Expect<IsNonNullable<{ id: string } | null>>
// ^?
})
})
// Passes the IsNonNulable test when the type is not nullable
type Result = Expect<IsNonNullable<{ id: number }>>
// ^?

// Failed the IsNonNulable test when the type is nullable
// @ts-expect-error - Fails the IsNonNulable test
type ResultFailure = Expect<IsNonNullable<{ id: string } | null>>
// ^?
40 changes: 18 additions & 22 deletions tests/IsNonNullish.test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
/* eslint-disable unused-imports/no-unused-vars */
import { describe, it } from 'bun:test'
import type { Expect, IsNonNullish } from '../index'

describe('IsNonNullish tests', () => {
it('Passes the IsNonNullish test when the type is not nullish', () => {
type ResultNull = Expect<IsNonNullish<{ id: number }>>
// ^?
type ResultUndefined = Expect<IsNonNullish<{ id: number }>>
// ^?
type ResultBoth = Expect<IsNonNullish<{ id: number }>>
// ^?
})
it('Failed the IsNonNullish test when the type is nullish', () => {
// @ts-expect-error - Fails the exclusion
type ResultNull = Expect<IsNonNullish<{ id: number } | null>>
// ^?
// @ts-expect-error - Fails the exclusion
type ResultUndefined = Expect<IsNonNullish<{ id: number } | undefined>>
// ^?
// @ts-expect-error - Fails the exclusion
type ResultBoth = Expect<IsNonNullish<{ id: number } | null | undefined>>
// ^?
})
})
// Passes the IsNonNullish test when the type is not nullish
type ResultNull = Expect<IsNonNullish<{ id: number }>>
// ^?
type ResultUndefined = Expect<IsNonNullish<{ id: number }>>
// ^?
type ResultBoth = Expect<IsNonNullish<{ id: number }>>
// ^?

// Failed the IsNonNullish test when the type is nullish
// @ts-expect-error - Fails the exclusion
type ResultNullFailure = Expect<IsNonNullish<{ id: number } | null>>
// ^?
// @ts-expect-error - Fails the exclusion
type ResultUndefinedFailure = Expect<IsNonNullish<{ id: number } | undefined>>
// ^?
// @ts-expect-error - Fails the exclusion
type ResultBothFailure = Expect<IsNonNullish<{ id: number } | null | undefined>>
// ^?
20 changes: 8 additions & 12 deletions tests/IsNonUndefined.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
/* eslint-disable unused-imports/no-unused-vars */
import { describe, it } from 'bun:test'
import type { Expect, IsNonUndefined } from '../index'

describe('IsUndefined tests', () => {
it('Passes the IsNonUndefined test when the type is not undefined', () => {
type Result = Expect<IsNonUndefined<{ id: number }>>
// ^?
})
it('Failed the IsNonUndefined test when the type is undefined', () => {
// @ts-expect-error - Fails the exclusion
type Result = Expect<IsNonUndefined<{ id: string } | undefined>>
// ^?
})
})
// Passes the IsNonUndefined test when the type is not undefined
type Result = Expect<IsNonUndefined<{ id: number }>>
// ^?

// Failed the IsNonUndefined test when the type is undefined
// @ts-expect-error - Fails the exclusion
type ResultFailure = Expect<IsNonUndefined<{ id: string } | undefined>>
// ^?
20 changes: 8 additions & 12 deletions tests/IsNullable.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
/* eslint-disable unused-imports/no-unused-vars */
import { describe, it } from 'bun:test'
import type { Expect, IsNullable } from '../index'

describe('IsNullable tests', () => {
it('Passes the IsNullable test when the type is nullable', () => {
type Result = Expect<IsNullable<{ id: number } | null>>
// ^?
})
it('Failed the IsNullable test when the type is not nullable', () => {
// @ts-expect-error - Fails the exclusion
type Result = Expect<IsNullable<{ id: string }>>
// ^?
})
})
// Passes the IsNullable test when the type is nullable
type Result = Expect<IsNullable<{ id: number } | null>>
// ^?

// Failed the IsNullable test when the type is not nullable
// @ts-expect-error - Fails the exclusion
type ResultFailure = Expect<IsNullable<{ id: string }>>
// ^?
28 changes: 12 additions & 16 deletions tests/IsNullish.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
/* eslint-disable unused-imports/no-unused-vars */
import { describe, it } from 'bun:test'
import type { Expect, IsNullish } from '../index'

describe('IsNullish tests', () => {
it('Passes the IsNullish test when the type is nullish', () => {
type ResultNull = Expect<IsNullish<{ id: number } | null>>
// ^?
type ResultUndefined = Expect<IsNullish<{ id: number } | undefined>>
// ^?
type ResultBoth = Expect<IsNullish<{ id: number } | null | undefined>>
// ^?
})
it('Failed the IsNullish test when the type is not nullish', () => {
// @ts-expect-error - Fails the exclusion
type Result = Expect<IsNullish<{ id: string }>>
// ^?
})
})
// Passes the IsNullish test when the type is nullish
type ResultNull = Expect<IsNullish<{ id: number } | null>>
// ^?
type ResultUndefined = Expect<IsNullish<{ id: number } | undefined>>
// ^?
type ResultBoth = Expect<IsNullish<{ id: number } | null | undefined>>
// ^?

// Failed the IsNullish test when the type is not nullish
// @ts-expect-error - Fails the exclusion
type ResultFailure = Expect<IsNullish<{ id: string }>>
// ^?
20 changes: 8 additions & 12 deletions tests/IsUndefined.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
/* eslint-disable unused-imports/no-unused-vars */
import { describe, it } from 'bun:test'
import type { Expect, IsUndefined } from '../index'

describe('IsUndefined tests', () => {
it('Passes the IsUndefined test when the type is undefined', () => {
type Result = Expect<IsUndefined<{ id: number } | undefined>>
// ^?
})
it('Failed the IsUndefined test when the type is not undefined', () => {
// @ts-expect-error - Fails the exclusion
type Result = Expect<IsUndefined<{ id: string }>>
// ^?
})
})
// Passes the IsUndefined test when the type is undefined
type Result = Expect<IsUndefined<{ id: number } | undefined>>
// ^?

// Failed the IsUndefined test when the type is not undefined
// @ts-expect-error - Fails the exclusion
type ResultFailure = Expect<IsUndefined<{ id: string }>>
// ^?
30 changes: 13 additions & 17 deletions tests/Length.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
/* eslint-disable unused-imports/no-unused-vars */
import { describe, it } from 'bun:test'
import type { Equals, Expect, Length } from '../index'

describe('Length tests', () => {
it('Passes the length test when the lengths dont match', () => {
type Result = Expect<Equals<Length<[0, 1, 2]>, 3>>
// ^?
type ResultRecord = Expect<Equals<Length<[{ id: string }, { name: string }]>, 2>>
// ^?
})
it('Fails the length test when the types are not of the same length', () => {
// @ts-expect-error - number values failing the length checker
type Results = Expect<Equals<Length<[0, 1, 2]>, 10>>
// ^?
// @ts-expect-error - Object / Record failing the length checker
type ResultRecord = Expect<Equals<Length<[{ id: string }, { name: string }]>, 10>>
// ^?
})
})
// Passes the length test when the lengths dont match
type Result = Expect<Equals<Length<[0, 1, 2]>, 3>>
// ^?
type ResultRecord = Expect<Equals<Length<[{ id: string }, { name: string }]>, 2>>
// ^?

// Fails the length test when the types are not of the same length
// @ts-expect-error - number values failing the length checker
type Results = Expect<Equals<Length<[0, 1, 2]>, 10>>
// ^?
// @ts-expect-error - Object / Record failing the length checker
type ResultRecordFailure = Expect<Equals<Length<[{ id: string }, { name: string }]>, 10>>
// ^?
Loading

0 comments on commit 0b06d6e

Please sign in to comment.