diff --git a/package.json b/package.json index bc31dee..6c793dd 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "lint:fix": "eslint --fix", "test": "npm run test:unit && npm run test:typescript", "test:unit": "c8 --100 node --test", - "test:typescript": "tsd" + "test:typescript": "tstyche" }, "repository": { "type": "git", @@ -62,12 +62,7 @@ "c8": "^11.0.0", "eslint": "^9.17.0", "neostandard": "^0.13.0", - "tsd": "^0.33.0" - }, - "tsd": { - "compilerOptions": { - "esModuleInterop": true - } + "tstyche": "^7.1.0" }, "publishConfig": { "access": "public" diff --git a/types/index.test-d.ts b/types/index.test-d.ts deleted file mode 100644 index 1a4398d..0000000 --- a/types/index.test-d.ts +++ /dev/null @@ -1,92 +0,0 @@ -import createError, { FastifyError, FastifyErrorConstructor } from '..' -import { expectType, expectError } from 'tsd' - -const CustomError = createError('ERROR_CODE', 'message') -expectType>(CustomError) -const err = new CustomError() -expectType(err) -expectType<'ERROR_CODE'>(err.code) -expectType(err.message) -expectType(err.statusCode) - -const CustomErrorNoStackTrace = createError('ERROR_CODE', 'message', undefined, undefined, false) -expectType>(CustomErrorNoStackTrace) -const errNoStackTrace = new CustomErrorNoStackTrace() -expectType(errNoStackTrace) -expectType<'ERROR_CODE'>(errNoStackTrace.code) -expectType(errNoStackTrace.message) -expectType(errNoStackTrace.statusCode) - -const CustomTypedError = createError('OTHER_CODE', 'message', 400) -expectType>(CustomTypedError) -const typed = new CustomTypedError() -expectType(typed) -expectType<'OTHER_CODE'>(typed.code) -expectType(typed.message) -expectType<400>(typed.statusCode) - -/* eslint-disable no-new */ -const CustomTypedArgError = createError<[string]>('OTHER_CODE', 'expect %s message', 400) -CustomTypedArgError('a') -expectError(CustomTypedArgError('a', 'b')) -expectError(new CustomTypedArgError('a', 'b')) -expectError(CustomTypedArgError(1)) -expectError(new CustomTypedArgError(1)) - -const CustomTypedArgError2 = createError('OTHER_CODE', 'expect %s message', 400) -CustomTypedArgError2('a') -expectError(CustomTypedArgError2('a', 'b')) -expectError(new CustomTypedArgError2('a', 'b')) -expectError(CustomTypedArgError2(1)) -expectError(new CustomTypedArgError2(1)) - -const CustomTypedArgError3 = createError('OTHER_CODE', 'expect %s message but got %s', 400) -expectError(CustomTypedArgError3('a')) -CustomTypedArgError3('a', 'b') -new CustomTypedArgError3('a', 'b') -expectError(CustomTypedArgError3(1)) -expectError(new CustomTypedArgError3(1)) -expectError(new CustomTypedArgError3(1, 2)) -expectError(new CustomTypedArgError3('1', 2)) -expectError(new CustomTypedArgError3(1, '2')) - -const CustomTypedArgError4 = createError('OTHER_CODE', 'expect %s message but got %s', 400) -expectError(CustomTypedArgError4('a')) -CustomTypedArgError4('a', 'b') -new CustomTypedArgError4('a', 'b') -expectError(CustomTypedArgError4(1)) -expectError(new CustomTypedArgError4(1)) -expectError(new CustomTypedArgError4(1, 2)) -expectError(new CustomTypedArgError4('1', 2)) -expectError(new CustomTypedArgError4(1, '2')) - -const CustomTypedArgError5 = createError<[string, string, string, string]>('OTHER_CODE', 'expect %s message but got %s. Please contact %s by emailing to %s', 400) -expectError(CustomTypedArgError5('a')) -expectError(new CustomTypedArgError5('a', 'b')) -expectError(new CustomTypedArgError5('a', 'b', 'c')) -CustomTypedArgError5('a', 'b', 'c', 'd') -expectError(new CustomTypedArgError5('a', 'b', 'c', 'd', 'e')) - -const CustomTypedArgError6 = createError('OTHER_CODE', 'expect %s message but got %s. Please contact %s by emailing to %s', 400) -expectError(CustomTypedArgError6('a')) -expectError(new CustomTypedArgError6('a', 'b')) -expectError(new CustomTypedArgError6('a', 'b', 'c')) -CustomTypedArgError6('a', 'b', 'c', 'd') -expectError(new CustomTypedArgError6('a', 'b', 'c', 'd', 'e')) - -const CustomErrorWithErrorConstructor = createError('ERROR_CODE', 'message', 500, TypeError) -expectType>(CustomErrorWithErrorConstructor) -CustomErrorWithErrorConstructor({ cause: new Error('Error') }) -const customErrorWithErrorConstructor = CustomErrorWithErrorConstructor() -if (customErrorWithErrorConstructor instanceof FastifyError) { - expectType<'ERROR_CODE'>(customErrorWithErrorConstructor.code) - expectType(customErrorWithErrorConstructor.message) - expectType<500>(customErrorWithErrorConstructor.statusCode) -} - -const error = new FastifyError('ERROR_CODE', 'message', 500) -if (error instanceof FastifyError) { - expectType(error.code) - expectType(error.message) - expectType(error.statusCode) -} diff --git a/types/index.tst.ts b/types/index.tst.ts new file mode 100644 index 0000000..8c15e83 --- /dev/null +++ b/types/index.tst.ts @@ -0,0 +1,153 @@ +import { expect } from 'tstyche' +import createError, { FastifyError, FastifyErrorConstructor } from '..' + +const CustomError = createError('ERROR_CODE', 'message') +expect(CustomError).type.toBe< + FastifyErrorConstructor<{ code: 'ERROR_CODE' }> +>() +const err = new CustomError() +expect(err).type.toBe() +expect(err.code).type.toBe<'ERROR_CODE'>() +expect(err.message).type.toBe() +expect(err.statusCode).type.toBe() + +const CustomErrorNoStackTrace = createError( + 'ERROR_CODE', + 'message', + undefined, + undefined, + false +) +expect(CustomErrorNoStackTrace).type.toBe< + FastifyErrorConstructor<{ code: 'ERROR_CODE' }> +>() +const errNoStackTrace = new CustomErrorNoStackTrace() +expect(errNoStackTrace).type.toBe() +expect(errNoStackTrace.code).type.toBe<'ERROR_CODE'>() +expect(errNoStackTrace.message).type.toBe() +expect(errNoStackTrace.statusCode).type.toBe() + +const CustomTypedError = createError('OTHER_CODE', 'message', 400) +expect(CustomTypedError).type.toBe< + FastifyErrorConstructor<{ code: 'OTHER_CODE'; statusCode: 400 }> +>() +const typed = new CustomTypedError() +expect(typed).type.toBe< + FastifyError & { code: 'OTHER_CODE'; statusCode: 400 } +>() +expect(typed.code).type.toBe<'OTHER_CODE'>() +expect(typed.message).type.toBe() +expect(typed.statusCode).type.toBe<400>() + +/* eslint-disable no-new */ +const CustomTypedArgError = createError<[string]>( + 'OTHER_CODE', + 'expect %s message', + 400 +) +CustomTypedArgError('a') +expect(CustomTypedArgError).type.not.toBeCallableWith('a', 'b') +expect(CustomTypedArgError).type.not.toBeConstructableWith('a', 'b') +expect(CustomTypedArgError).type.not.toBeCallableWith(1) +expect(CustomTypedArgError).type.not.toBeConstructableWith(1) + +const CustomTypedArgError2 = createError( + 'OTHER_CODE', + 'expect %s message', + 400 +) +CustomTypedArgError2('a') +expect(CustomTypedArgError2).type.not.toBeCallableWith('a', 'b') +expect(CustomTypedArgError2).type.not.toBeConstructableWith('a', 'b') +expect(CustomTypedArgError2).type.not.toBeCallableWith(1) +expect(CustomTypedArgError2).type.not.toBeConstructableWith(1) + +const CustomTypedArgError3 = createError( + 'OTHER_CODE', + 'expect %s message but got %s', + 400 +) +expect(CustomTypedArgError3).type.not.toBeCallableWith('a') +CustomTypedArgError3('a', 'b') +new CustomTypedArgError3('a', 'b') +expect(CustomTypedArgError3).type.not.toBeCallableWith(1) +expect(CustomTypedArgError3).type.not.toBeConstructableWith(1) +expect(CustomTypedArgError3).type.not.toBeConstructableWith(1, 2) +expect(CustomTypedArgError3).type.not.toBeConstructableWith('1', 2) +expect(CustomTypedArgError3).type.not.toBeConstructableWith(1, '2') + +const CustomTypedArgError4 = createError( + 'OTHER_CODE', + 'expect %s message but got %s', + 400 +) +expect(CustomTypedArgError4).type.not.toBeCallableWith('a') +CustomTypedArgError4('a', 'b') +new CustomTypedArgError4('a', 'b') +expect(CustomTypedArgError4).type.not.toBeCallableWith(1) +expect(CustomTypedArgError4).type.not.toBeConstructableWith(1) +expect(CustomTypedArgError4).type.not.toBeConstructableWith(1, 2) +expect(CustomTypedArgError4).type.not.toBeConstructableWith('1', 2) +expect(CustomTypedArgError4).type.not.toBeConstructableWith(1, '2') + +const CustomTypedArgError5 = createError<[string, string, string, string]>( + 'OTHER_CODE', + 'expect %s message but got %s. Please contact %s by emailing to %s', + 400 +) +expect(CustomTypedArgError5).type.not.toBeCallableWith('a') +expect(CustomTypedArgError5).type.not.toBeConstructableWith('a', 'b') +expect(CustomTypedArgError5).type.not.toBeConstructableWith('a', 'b', 'c') +CustomTypedArgError5('a', 'b', 'c', 'd') +expect(CustomTypedArgError5).type.not.toBeConstructableWith( + 'a', + 'b', + 'c', + 'd', + 'e' +) + +const CustomTypedArgError6 = createError< + string, + number, + [string, string, string, string] +>( + 'OTHER_CODE', + 'expect %s message but got %s. Please contact %s by emailing to %s', + 400 +) +expect(CustomTypedArgError6).type.not.toBeCallableWith('a') +expect(CustomTypedArgError6).type.not.toBeConstructableWith('a', 'b') +expect(CustomTypedArgError6).type.not.toBeConstructableWith('a', 'b', 'c') +CustomTypedArgError6('a', 'b', 'c', 'd') +expect(CustomTypedArgError6).type.not.toBeConstructableWith( + 'a', + 'b', + 'c', + 'd', + 'e' +) + +const CustomErrorWithErrorConstructor = createError( + 'ERROR_CODE', + 'message', + 500, + TypeError +) +expect(CustomErrorWithErrorConstructor).type.toBe< + FastifyErrorConstructor<{ code: 'ERROR_CODE'; statusCode: 500 }> +>() +CustomErrorWithErrorConstructor({ cause: new Error('Error') }) +const customErrorWithErrorConstructor = CustomErrorWithErrorConstructor() +if (customErrorWithErrorConstructor instanceof FastifyError) { + expect(customErrorWithErrorConstructor.code).type.toBe<'ERROR_CODE'>() + expect(customErrorWithErrorConstructor.message).type.toBe() + expect(customErrorWithErrorConstructor.statusCode).type.toBe<500>() +} + +const error = new FastifyError('ERROR_CODE', 'message', 500) +if (error instanceof FastifyError) { + expect(error.code).type.toBe() + expect(error.message).type.toBe() + expect(error.statusCode).type.toBe() +}