-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: ♻️ Simplify unit tests for each of the helper types
- Loading branch information
1 parent
9032eae
commit 0b06d6e
Showing
18 changed files
with
174 additions
and
237 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }>> | ||
// ^? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }>> | ||
// ^? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>> | ||
// ^? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }>> | ||
// ^? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }>> | ||
// ^? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }[]>> | ||
// ^? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>> | ||
// ^? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>> | ||
// ^? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>> | ||
// ^? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }>> | ||
// ^? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }>> | ||
// ^? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }>> | ||
// ^? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>> | ||
// ^? |
Oops, something went wrong.