From 0b06d6e7cfe2af980c102d65e818175a859e2614 Mon Sep 17 00:00:00 2001 From: CRBroughton Date: Tue, 4 Jun 2024 19:21:14 +0100 Subject: [PATCH] refactor: :recycle: Simplify unit tests for each of the helper types --- .changeset/empty-mice-cry.md | 5 +++++ .github/workflows/bun.yml | 2 -- tests/Assignable.test.ts | 20 ++++++++---------- tests/Equals.test.ts | 30 ++++++++++++--------------- tests/Excludes.test.ts | 32 ++++++++++++----------------- tests/Extends.test.ts | 24 +++++++++------------- tests/Includes.test.ts | 20 ++++++++---------- tests/IsNonArray.test.ts | 20 ++++++++---------- tests/IsNonNullable.test.ts | 20 ++++++++---------- tests/IsNonNullish.test.ts | 40 ++++++++++++++++-------------------- tests/IsNonUndefined.test.ts | 20 ++++++++---------- tests/IsNullable.test.ts | 20 ++++++++---------- tests/IsNullish.test.ts | 28 +++++++++++-------------- tests/IsUndefined.test.ts | 20 ++++++++---------- tests/Length.test.ts | 30 ++++++++++++--------------- tests/NotEquals.test.ts | 30 ++++++++++++--------------- tests/Position.test.ts | 30 ++++++++++++--------------- tests/isArray.test.ts | 20 ++++++++---------- 18 files changed, 174 insertions(+), 237 deletions(-) create mode 100644 .changeset/empty-mice-cry.md diff --git a/.changeset/empty-mice-cry.md b/.changeset/empty-mice-cry.md new file mode 100644 index 0000000..d54b6a3 --- /dev/null +++ b/.changeset/empty-mice-cry.md @@ -0,0 +1,5 @@ +--- +"@crbroughton/ts-test-utils": patch +--- + +Simplify unit tests for each of the helper types diff --git a/.github/workflows/bun.yml b/.github/workflows/bun.yml index 883ddac..dda6a36 100644 --- a/.github/workflows/bun.yml +++ b/.github/workflows/bun.yml @@ -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 diff --git a/tests/Assignable.test.ts b/tests/Assignable.test.ts index 3b0fccd..4625f39 100644 --- a/tests/Assignable.test.ts +++ b/tests/Assignable.test.ts @@ -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> - // ^? - }) - it('Failed the assignable test when the first type is not assignable', () => { - // @ts-expect-error - Fails the exclusion - type Result = Expect> - // ^? - }) -}) +// Passes the assignable test when the first type is assignable +type Result = Expect> +// ^? + +// Failed the assignable test when the first type is not assignable +// @ts-expect-error - Fails the exclusion +type ResultFailure = Expect> +// ^? diff --git a/tests/Equals.test.ts b/tests/Equals.test.ts index a699056..46c4f44 100644 --- a/tests/Equals.test.ts +++ b/tests/Equals.test.ts @@ -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> - // ^? - type ResultRecord = Expect> - // ^? - }) - it('Fails the equality test when the types are not equal', () => { - // @ts-expect-error - Boolean values failing the equality checker - type ResultBoolean = Expect> - // ^? - // @ts-expect-error - Object / Record failing the equality checker - type ResultRecord = Expect> - // ^? - }) -}) +// Passes the equality test when the types are equal +type ResultBoolean = Expect> +// ^? +type ResultRecord = Expect> +// ^? + +// Fails the equality test when the types are not equal +// @ts-expect-error - Boolean values failing the equality checker +type ResultBooleanFailure = Expect> +// ^? +// @ts-expect-error - Object / Record failing the equality checker +type ResultRecordFailure = Expect> +// ^? diff --git a/tests/Excludes.test.ts b/tests/Excludes.test.ts index 7f3b5b5..f9edd18 100644 --- a/tests/Excludes.test.ts +++ b/tests/Excludes.test.ts @@ -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> - // ^? - interface FullType { id: number, name: string, location: { city: 'Brighton' } } - interface ExcludedType { hobby: string } - type ResultNestedRecord = Expect - // ^? -> - }) - 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> - // ^? - }) -}) +// Passes the excludes test when the resulting type does not include the sub-type +type Result = Expect> +// ^? +interface FullType { id: number, name: string, location: { city: 'Brighton' } } +interface ExcludedType { hobby: string } +type ResultNestedRecord = Expect> +// ^? + +// 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> +// ^? diff --git a/tests/Extends.test.ts b/tests/Extends.test.ts index 3163106..7533c30 100644 --- a/tests/Extends.test.ts +++ b/tests/Extends.test.ts @@ -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> - // ^? - }) - 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> - // ^? - }) -}) +// Passes the extend test when the resulting type extends another type +interface BaseType { id: number } +type ExtensionType = BaseType & { name: string } +type Result = Expect> +// ^? + +// 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> +// ^? diff --git a/tests/Includes.test.ts b/tests/Includes.test.ts index d57d37c..f3ea319 100644 --- a/tests/Includes.test.ts +++ b/tests/Includes.test.ts @@ -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> - // ^? - }) - 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> - // ^? - }) -}) +// Passes the includes test when the resulting type includes the sub-type +type Result = Expect> +// ^? + +// 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> +// ^? diff --git a/tests/IsNonArray.test.ts b/tests/IsNonArray.test.ts index c81a522..dd02e8b 100644 --- a/tests/IsNonArray.test.ts +++ b/tests/IsNonArray.test.ts @@ -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> - // ^? - }) - it('Failed the IsNonArray test when the type is an array', () => { - // @ts-expect-error - Fails the exclusion - type Result = Expect> - // ^? - }) -}) +// Passes the IsNonArray test when the type is not an array +type Result = Expect> +// ^? + +// Failed the IsNonArray test when the type is an array +// @ts-expect-error - Fails the exclusion +type ResultFailure = Expect> +// ^? diff --git a/tests/IsNonNullable.test.ts b/tests/IsNonNullable.test.ts index 5e31bf1..60a47f3 100644 --- a/tests/IsNonNullable.test.ts +++ b/tests/IsNonNullable.test.ts @@ -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> - // ^? - }) - it('Failed the IsNonNulable test when the type is nullable', () => { - // @ts-expect-error - Fails the IsNonNulable test - type Result = Expect> - // ^? - }) -}) +// Passes the IsNonNulable test when the type is not nullable +type Result = Expect> +// ^? + +// Failed the IsNonNulable test when the type is nullable +// @ts-expect-error - Fails the IsNonNulable test +type ResultFailure = Expect> +// ^? diff --git a/tests/IsNonNullish.test.ts b/tests/IsNonNullish.test.ts index 4505e78..3fd8b17 100644 --- a/tests/IsNonNullish.test.ts +++ b/tests/IsNonNullish.test.ts @@ -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> - // ^? - type ResultUndefined = Expect> - // ^? - type ResultBoth = Expect> - // ^? - }) - it('Failed the IsNonNullish test when the type is nullish', () => { - // @ts-expect-error - Fails the exclusion - type ResultNull = Expect> - // ^? - // @ts-expect-error - Fails the exclusion - type ResultUndefined = Expect> - // ^? - // @ts-expect-error - Fails the exclusion - type ResultBoth = Expect> - // ^? - }) -}) +// Passes the IsNonNullish test when the type is not nullish +type ResultNull = Expect> +// ^? +type ResultUndefined = Expect> +// ^? +type ResultBoth = Expect> +// ^? + +// Failed the IsNonNullish test when the type is nullish +// @ts-expect-error - Fails the exclusion +type ResultNullFailure = Expect> +// ^? +// @ts-expect-error - Fails the exclusion +type ResultUndefinedFailure = Expect> +// ^? +// @ts-expect-error - Fails the exclusion +type ResultBothFailure = Expect> +// ^? diff --git a/tests/IsNonUndefined.test.ts b/tests/IsNonUndefined.test.ts index e1c2ac9..fc03e3f 100644 --- a/tests/IsNonUndefined.test.ts +++ b/tests/IsNonUndefined.test.ts @@ -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> - // ^? - }) - it('Failed the IsNonUndefined test when the type is undefined', () => { - // @ts-expect-error - Fails the exclusion - type Result = Expect> - // ^? - }) -}) +// Passes the IsNonUndefined test when the type is not undefined +type Result = Expect> +// ^? + +// Failed the IsNonUndefined test when the type is undefined +// @ts-expect-error - Fails the exclusion +type ResultFailure = Expect> +// ^? diff --git a/tests/IsNullable.test.ts b/tests/IsNullable.test.ts index 0ff42ae..236832a 100644 --- a/tests/IsNullable.test.ts +++ b/tests/IsNullable.test.ts @@ -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> - // ^? - }) - it('Failed the IsNullable test when the type is not nullable', () => { - // @ts-expect-error - Fails the exclusion - type Result = Expect> - // ^? - }) -}) +// Passes the IsNullable test when the type is nullable +type Result = Expect> +// ^? + +// Failed the IsNullable test when the type is not nullable +// @ts-expect-error - Fails the exclusion +type ResultFailure = Expect> +// ^? diff --git a/tests/IsNullish.test.ts b/tests/IsNullish.test.ts index 9927df4..2d6762b 100644 --- a/tests/IsNullish.test.ts +++ b/tests/IsNullish.test.ts @@ -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> - // ^? - type ResultUndefined = Expect> - // ^? - type ResultBoth = Expect> - // ^? - }) - it('Failed the IsNullish test when the type is not nullish', () => { - // @ts-expect-error - Fails the exclusion - type Result = Expect> - // ^? - }) -}) +// Passes the IsNullish test when the type is nullish +type ResultNull = Expect> +// ^? +type ResultUndefined = Expect> +// ^? +type ResultBoth = Expect> +// ^? + +// Failed the IsNullish test when the type is not nullish +// @ts-expect-error - Fails the exclusion +type ResultFailure = Expect> +// ^? diff --git a/tests/IsUndefined.test.ts b/tests/IsUndefined.test.ts index 3621c7e..2459a5b 100644 --- a/tests/IsUndefined.test.ts +++ b/tests/IsUndefined.test.ts @@ -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> - // ^? - }) - it('Failed the IsUndefined test when the type is not undefined', () => { - // @ts-expect-error - Fails the exclusion - type Result = Expect> - // ^? - }) -}) +// Passes the IsUndefined test when the type is undefined +type Result = Expect> +// ^? + +// Failed the IsUndefined test when the type is not undefined +// @ts-expect-error - Fails the exclusion +type ResultFailure = Expect> +// ^? diff --git a/tests/Length.test.ts b/tests/Length.test.ts index d998df5..b400df3 100644 --- a/tests/Length.test.ts +++ b/tests/Length.test.ts @@ -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, 3>> - // ^? - type ResultRecord = Expect, 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, 10>> - // ^? - // @ts-expect-error - Object / Record failing the length checker - type ResultRecord = Expect, 10>> - // ^? - }) -}) +// Passes the length test when the lengths dont match +type Result = Expect, 3>> +// ^? +type ResultRecord = Expect, 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, 10>> +// ^? +// @ts-expect-error - Object / Record failing the length checker +type ResultRecordFailure = Expect, 10>> +// ^? diff --git a/tests/NotEquals.test.ts b/tests/NotEquals.test.ts index 69a8835..ccfb732 100644 --- a/tests/NotEquals.test.ts +++ b/tests/NotEquals.test.ts @@ -1,20 +1,16 @@ /* eslint-disable unused-imports/no-unused-vars */ -import { describe, it } from 'bun:test' import type { Expect, NotEquals } from '../index' -describe('NotEquals tests', () => { - it('Passes the equality test when the types are not equal', () => { - type ResultBoolean = Expect> - // ^? - type ResultRecord = Expect> - // ^? - }) - it('Fails the equality test when the types are equal', () => { - // @ts-expect-error - Boolean values failing the equality checker - type ResultBoolean = Expect> - // ^? - // @ts-expect-error - Object / Record failing the equality checker - type ResultRecord = Expect> - // ^? - }) -}) +// Passes the equality test when the types are not equal +type ResultBoolean = Expect> +// ^? +type ResultRecord = Expect> +// ^? + +// Fails the equality test when the types are equal +// @ts-expect-error - Boolean values failing the equality checker +type ResultBooleanFailure = Expect> +// ^? +// @ts-expect-error - Object / Record failing the equality checker +type ResultRecordFailure = Expect> +// ^? diff --git a/tests/Position.test.ts b/tests/Position.test.ts index 092aa59..6015eb0 100644 --- a/tests/Position.test.ts +++ b/tests/Position.test.ts @@ -1,20 +1,16 @@ /* eslint-disable unused-imports/no-unused-vars */ -import { describe, it } from 'bun:test' import type { Equals, Expect, Position } from '../index' -describe('Position tests', () => { - it('Passes the position test when the given position matches', () => { - type Result = Expect, 1>> - // ^? - type ResultRecord = Expect, { id: string }>> - // ^? - }) - it('Fails the position test when the given position does not match', () => { - // @ts-expect-error - Fails when the given position doesn't match - type Results = Expect, 10>> - // ^? - // @ts-expect-error - Fails when the given position doesn't match - type ResultRecord = Expect, { id: number }>> - // ^? - }) -}) +// Passes the position test when the given position matches +type Result = Expect, 1>> +// ^? +type ResultRecord = Expect, { id: string }>> +// ^? + +// Fails the position test when the given position does not match +// @ts-expect-error - Fails when the given position doesn't match +type ResultFailure = Expect, 10>> +// ^? +// @ts-expect-error - Fails when the given position doesn't match +type ResultRecordFailure = Expect, { id: number }>> +// ^? diff --git a/tests/isArray.test.ts b/tests/isArray.test.ts index 4aca38e..0282f80 100644 --- a/tests/isArray.test.ts +++ b/tests/isArray.test.ts @@ -1,15 +1,11 @@ /* eslint-disable unused-imports/no-unused-vars */ -import { describe, it } from 'bun:test' import type { Expect, IsArray } from '../index' -describe('IsArray tests', () => { - it('Passes the IsArray test when the type is an array', () => { - type Result = Expect> - // ^? - }) - it('Failed the IsArray test when the type is not an array', () => { - // @ts-expect-error - Fails the exclusion - type Result = Expect> - // ^? - }) -}) +// Passes the IsArray test when the type is an array +type Result = Expect> +// ^? + +// Failed the IsArray test when the type is not an array +// @ts-expect-error - Fails the exclusion +type ResultFailure = Expect> +// ^?