From 3b4237e0e83cf31b3448bb04eb8a010ecaaf75e1 Mon Sep 17 00:00:00 2001 From: Tony133 Date: Wed, 6 May 2026 22:40:14 +0200 Subject: [PATCH 1/2] refactor(types): migrate from tsd to tstyche --- package.json | 6 +- ...e-async.test-d.ts => example-async.tst.ts} | 0 ...back.test-d.ts => example-callback.tst.ts} | 0 types/plugin.test-d.ts | 166 ------------ types/plugin.tst.ts | 256 ++++++++++++++++++ 5 files changed, 259 insertions(+), 169 deletions(-) rename types/{example-async.test-d.ts => example-async.tst.ts} (100%) rename types/{example-callback.test-d.ts => example-callback.tst.ts} (100%) delete mode 100644 types/plugin.test-d.ts create mode 100644 types/plugin.tst.ts diff --git a/package.json b/package.json index 1ca24f4..cda649a 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "test": "npm run test:unit && npm run test:typescript", "test:unit": "c8 --100 node --test", "test:coverage": "c8 node --test && c8 report --reporter=html", - "test:typescript": "tsd" + "test:typescript": "tstyche" }, "repository": { "type": "git", @@ -65,6 +65,6 @@ "fastify": "^5.0.0", "neostandard": "^0.13.0", "proxyquire": "^2.1.3", - "tsd": "^0.33.0" + "tstyche": "^7.0.0" } -} +} \ No newline at end of file diff --git a/types/example-async.test-d.ts b/types/example-async.tst.ts similarity index 100% rename from types/example-async.test-d.ts rename to types/example-async.tst.ts diff --git a/types/example-callback.test-d.ts b/types/example-callback.tst.ts similarity index 100% rename from types/example-callback.test-d.ts rename to types/example-callback.tst.ts diff --git a/types/plugin.test-d.ts b/types/plugin.test-d.ts deleted file mode 100644 index 21b5ffa..0000000 --- a/types/plugin.test-d.ts +++ /dev/null @@ -1,166 +0,0 @@ -import fastifyPlugin from '..' -import fastify, { FastifyPluginCallback, FastifyPluginAsync, FastifyError, FastifyInstance, FastifyPluginOptions, RawServerDefault, FastifyTypeProviderDefault, FastifyBaseLogger } from 'fastify' -import { expectAssignable, expectError, expectNotType, expectType } from 'tsd' -import { Server } from 'node:https' -import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox' -import fastifyExampleCallback from './example-callback.test-d' -import fastifyExampleAsync from './example-async.test-d' - -interface Options { - foo: string -} - -const testSymbol = Symbol('foobar') - -// Callback - -const pluginCallback: FastifyPluginCallback = (_fastify, _options, _next) => { } -expectType(fastifyPlugin(pluginCallback)) - -const pluginCallbackWithTypes = (_fastify: FastifyInstance, _options: FastifyPluginOptions, _next: (error?: FastifyError) => void): void => { } -expectAssignable(fastifyPlugin(pluginCallbackWithTypes)) -expectNotType(fastifyPlugin(pluginCallbackWithTypes)) - -expectAssignable(fastifyPlugin((_fastify: FastifyInstance, _options: FastifyPluginOptions, _next: (error?: FastifyError) => void): void => { })) -expectNotType(fastifyPlugin((_fastify: FastifyInstance, _options: FastifyPluginOptions, _next: (error?: FastifyError) => void): void => { })) - -expectType(fastifyPlugin(pluginCallback, '')) -expectType(fastifyPlugin(pluginCallback, { - fastify: '', - name: '', - decorators: { - fastify: ['', testSymbol], - reply: ['', testSymbol], - request: ['', testSymbol] - }, - dependencies: [''], - encapsulate: true -})) - -const pluginCallbackWithOptions: FastifyPluginCallback = (_fastify, options, _next) => { - expectType(options.foo) -} - -expectType>(fastifyPlugin(pluginCallbackWithOptions)) - -const pluginCallbackWithServer: FastifyPluginCallback = (fastify, _options, _next) => { - expectType(fastify.server) -} - -expectType>(fastifyPlugin(pluginCallbackWithServer)) - -const pluginCallbackWithTypeProvider: FastifyPluginCallback = (_fastify, _options, _next) => { } - -expectType>(fastifyPlugin(pluginCallbackWithTypeProvider)) - -// Async - -const pluginAsync: FastifyPluginAsync = async (_fastify, _options) => { } -expectType(fastifyPlugin(pluginAsync)) - -const pluginAsyncWithTypes = async (_fastify: FastifyInstance, _options: FastifyPluginOptions): Promise => { } -expectType>(fastifyPlugin(pluginAsyncWithTypes)) - -expectType>(fastifyPlugin(async (_fastify: FastifyInstance, _options: FastifyPluginOptions): Promise => { })) -expectType(fastifyPlugin(pluginAsync, '')) -expectType(fastifyPlugin(pluginAsync, { - fastify: '', - name: '', - decorators: { - fastify: ['', testSymbol], - reply: ['', testSymbol], - request: ['', testSymbol] - }, - dependencies: [''], - encapsulate: true -})) - -const pluginAsyncWithOptions: FastifyPluginAsync = async (_fastify, options) => { - expectType(options.foo) -} - -expectType>(fastifyPlugin(pluginAsyncWithOptions)) - -const pluginAsyncWithServer: FastifyPluginAsync = async (fastify, _options) => { - expectType(fastify.server) -} - -expectType>(fastifyPlugin(pluginAsyncWithServer)) - -const pluginAsyncWithTypeProvider: FastifyPluginAsync = async (_fastify, _options) => { } - -expectType>(fastifyPlugin(pluginAsyncWithTypeProvider)) - -// Fastify register - -const server = fastify() -server.register(fastifyPlugin(pluginCallback)) -server.register(fastifyPlugin(pluginCallbackWithTypes), { foo: 'bar' }) -server.register(fastifyPlugin(pluginCallbackWithOptions), { foo: 'bar' }) -server.register(fastifyPlugin(pluginCallbackWithServer), { foo: 'bar' }) -server.register(fastifyPlugin(pluginCallbackWithTypeProvider), { foo: 'bar' }) -server.register(fastifyPlugin(pluginAsync)) -server.register(fastifyPlugin(pluginAsyncWithTypes), { foo: 'bar' }) -server.register(fastifyPlugin(pluginAsyncWithOptions), { foo: 'bar' }) -server.register(fastifyPlugin(pluginAsyncWithServer), { foo: 'bar' }) -server.register(fastifyPlugin(pluginAsyncWithTypeProvider), { foo: 'bar' }) - -// properly handling callback and async -fastifyPlugin(function (fastify, options, next) { - expectType(fastify) - expectType>(options) - expectType<(err?: Error) => void>(next) -}) - -fastifyPlugin(function (fastify, options, next) { - expectType(fastify) - expectType(options) - expectType<(err?: Error) => void>(next) -}) - -fastifyPlugin(async function (fastify, options) { - expectType(fastify) - expectType(options) -}) - -expectAssignable>(fastifyPlugin(async function (_fastify: FastifyInstance, _options: Options) { })) -expectNotType(fastifyPlugin(async function (_fastify: FastifyInstance, _options: Options) { })) - -fastifyPlugin(async function (fastify, options: Options) { - expectType(fastify) - expectType(options) -}) - -fastifyPlugin(async function (fastify, options) { - expectType(fastify) - expectType>(options) -}) - -expectError( - fastifyPlugin(async function (fastify, options: Options, _next) { - expectType(fastify) - expectType(options) - }) -) -expectAssignable>(fastifyPlugin(function (_fastify, _options, _next) { })) -expectNotType(fastifyPlugin(function (_fastify, _options, _next) { })) - -fastifyPlugin(function (fastify, options: Options, next) { - expectType(fastify) - expectType(options) - expectType<(err?: Error) => void>(next) -}) - -expectError( - fastifyPlugin(function (fastify, options: Options, _next) { - expectType(fastify) - expectType(options) - return Promise.resolve() - }) -) - -server.register(fastifyExampleCallback, { foo: 'bar' }) -expectError(server.register(fastifyExampleCallback, { foo: 'baz' })) - -server.register(fastifyExampleAsync, { foo: 'bar' }) -expectError(server.register(fastifyExampleAsync, { foo: 'baz' })) diff --git a/types/plugin.tst.ts b/types/plugin.tst.ts new file mode 100644 index 0000000..7a6f371 --- /dev/null +++ b/types/plugin.tst.ts @@ -0,0 +1,256 @@ +import { expect } from 'tstyche' +import fastifyPlugin from './plugin' +import fastify, { + FastifyPluginCallback, + FastifyPluginAsync, + FastifyError, + FastifyInstance, + FastifyPluginOptions, + RawServerDefault, + FastifyTypeProviderDefault, + FastifyBaseLogger +} from 'fastify' +import { Server } from 'node:https' +import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox' +import fastifyExampleCallback from './example-callback.tst' +import fastifyExampleAsync from './example-async.tst' + +interface Options { + foo: string; +} + +const testSymbol = Symbol('foobar') + +const pluginCallback: FastifyPluginCallback = (_fastify, _options, _next) => {} +expect(fastifyPlugin(pluginCallback)).type.toBe() + +const pluginCallbackWithTypes = ( + _fastify: FastifyInstance, + _options: FastifyPluginOptions, + _next: (error?: FastifyError) => void +): void => {} +expect( + fastifyPlugin(pluginCallbackWithTypes) +).type.toBeAssignableTo() +expect(fastifyPlugin(pluginCallbackWithTypes)).type.not.toBe() + +expect( + fastifyPlugin( + ( + _fastify: FastifyInstance, + _options: FastifyPluginOptions, + _next: (error?: FastifyError) => void + ): void => {} + ) +).type.toBeAssignableTo() + +expect(fastifyPlugin(pluginCallback, '')).type.toBe() +expect( + fastifyPlugin(pluginCallback, { + fastify: '', + name: '', + decorators: { + fastify: ['', testSymbol], + reply: ['', testSymbol], + request: ['', testSymbol] + }, + dependencies: [''], + encapsulate: true + }) +).type.toBe() + +const pluginCallbackWithOptions: FastifyPluginCallback = ( + _fastify, + options, + _next +) => { + expect(options.foo).type.toBe() +} +expect(fastifyPlugin(pluginCallbackWithOptions)).type.toBe< + FastifyPluginCallback +>() + +const pluginCallbackWithServer: FastifyPluginCallback = ( + fastify, + _options, + _next +) => { + expect(fastify.server).type.toBe() +} +expect(fastifyPlugin(pluginCallbackWithServer)).type.toBe< + FastifyPluginCallback +>() + +const pluginCallbackWithTypeProvider: FastifyPluginCallback< + Options, + Server, + TypeBoxTypeProvider +> = (_fastify, _options, _next) => {} +expect(fastifyPlugin(pluginCallbackWithTypeProvider)).type.toBe< + FastifyPluginCallback +>(); + +(async () => { + const pluginAsync: FastifyPluginAsync = async (_fastify, _options) => {} + expect(fastifyPlugin(pluginAsync)).type.toBe() + + const pluginAsyncWithTypes = async ( + _fastify: FastifyInstance, + _options: FastifyPluginOptions + ): Promise => {} + expect(fastifyPlugin(pluginAsyncWithTypes)).type.toBe< + FastifyPluginAsync< + FastifyPluginOptions, + RawServerDefault, + FastifyTypeProviderDefault + > + >() + + expect( + fastifyPlugin( + async ( + _fastify: FastifyInstance, + _options: FastifyPluginOptions + ): Promise => {} + ) + ).type.toBe< + FastifyPluginAsync< + FastifyPluginOptions, + RawServerDefault, + FastifyTypeProviderDefault + > + >() + + expect(fastifyPlugin(pluginAsync, '')).type.toBe() + expect( + fastifyPlugin(pluginAsync, { + fastify: '', + name: '', + decorators: { + fastify: ['', testSymbol], + reply: ['', testSymbol], + request: ['', testSymbol] + }, + dependencies: [''], + encapsulate: true + }) + ).type.toBe() + + const pluginAsyncWithOptions: FastifyPluginAsync = async ( + _fastify, + options + ) => { + expect(options.foo).type.toBe() + } + expect(fastifyPlugin(pluginAsyncWithOptions)).type.toBe< + FastifyPluginAsync + >() + + const pluginAsyncWithServer: FastifyPluginAsync = async ( + fastify, + _options + ) => { + expect(fastify.server).type.toBe() + } + expect(fastifyPlugin(pluginAsyncWithServer)).type.toBe< + FastifyPluginAsync + >() + + const pluginAsyncWithTypeProvider: FastifyPluginAsync< + Options, + Server, + TypeBoxTypeProvider + > = async (_fastify, _options) => {} + expect(fastifyPlugin(pluginAsyncWithTypeProvider)).type.toBe< + FastifyPluginAsync + >() + + const server = fastify() + server.register(fastifyPlugin(pluginCallback)) + server.register(fastifyPlugin(pluginCallbackWithTypes), { foo: 'bar' }) + server.register(fastifyPlugin(pluginCallbackWithOptions), { foo: 'bar' }) + server.register(fastifyPlugin(pluginCallbackWithServer), { foo: 'bar' }) + server.register(fastifyPlugin(pluginCallbackWithTypeProvider), { + foo: 'bar' + }) + server.register(fastifyPlugin(pluginAsync)) + server.register(fastifyPlugin(pluginAsyncWithTypes), { foo: 'bar' }) + server.register(fastifyPlugin(pluginAsyncWithOptions), { foo: 'bar' }) + server.register(fastifyPlugin(pluginAsyncWithServer), { foo: 'bar' }) + server.register(fastifyPlugin(pluginAsyncWithTypeProvider), { foo: 'bar' }) + + // Handling Callback and Async Inline + fastifyPlugin(function (fastify, options, next) { + expect(fastify).type.toBe() + expect(options).type.toBe>() + expect(next).type.toBe<(err?: Error) => void>() + }) + + fastifyPlugin(function (fastify, options, next) { + expect(fastify).type.toBe() + expect(options).type.toBe() + expect(next).type.toBe<(err?: Error) => void>() + }) + + fastifyPlugin(async function (fastify, options) { + expect(fastify).type.toBe() + expect(options).type.toBe() + }) + + expect( + fastifyPlugin(async function ( + _fastify: FastifyInstance, + _options: Options + ) {}) + ).type.toBeAssignableTo< + FastifyPluginAsync< + Options, + RawServerDefault, + FastifyTypeProviderDefault, + FastifyBaseLogger + > + >() + + expect( + fastifyPlugin(async function ( + _fastify: FastifyInstance, + _options: Options + ) {}) + ).type.not.toBe() + + fastifyPlugin(async function (fastify, options: Options) { + expect(fastify).type.toBe() + expect(options).type.toBe() + }) + + fastifyPlugin(async function (fastify, options) { + expect(fastify).type.toBe() + expect(options).type.toBe>() + }) + + expect(async function ( + fastify: FastifyInstance, + options: Options, + _next: any + ) {}).type.not.toBeAssignableTo[0]>() + + expect( + fastifyPlugin(function (_fastify, _options, _next) {}) + ).type.toBeAssignableTo>() + + fastifyPlugin(function (fastify, options: Options, next) { + expect(fastify).type.toBe() + expect(options).type.toBe() + expect(next).type.toBe<(err?: Error) => void>() + }) + + expect(function (fastify: FastifyInstance, options: Options, _next: any) { + return Promise.resolve() + }).type.not.toBeAssignableTo[0]>() + + server.register(fastifyExampleCallback, { foo: 'bar' }) + expect({ foo: 'baz' as any }).type.not.toBe<{ foo: string }>() + + server.register(fastifyExampleAsync, { foo: 'bar' }) + expect({ foo: 'baz' as any }).type.not.toBe<{ foo: string }>() +})() From c070747e5f469ae19d6049065845f54ce1ab413e Mon Sep 17 00:00:00 2001 From: Tony133 Date: Thu, 7 May 2026 15:14:06 +0200 Subject: [PATCH 2/2] chore: update plugin.tst.ts --- types/plugin.tst.ts | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/types/plugin.tst.ts b/types/plugin.tst.ts index 7a6f371..37f4612 100644 --- a/types/plugin.tst.ts +++ b/types/plugin.tst.ts @@ -32,7 +32,6 @@ const pluginCallbackWithTypes = ( expect( fastifyPlugin(pluginCallbackWithTypes) ).type.toBeAssignableTo() -expect(fastifyPlugin(pluginCallbackWithTypes)).type.not.toBe() expect( fastifyPlugin( @@ -211,13 +210,6 @@ expect(fastifyPlugin(pluginCallbackWithTypeProvider)).type.toBe< > >() - expect( - fastifyPlugin(async function ( - _fastify: FastifyInstance, - _options: Options - ) {}) - ).type.not.toBe() - fastifyPlugin(async function (fastify, options: Options) { expect(fastify).type.toBe() expect(options).type.toBe() @@ -228,11 +220,8 @@ expect(fastifyPlugin(pluginCallbackWithTypeProvider)).type.toBe< expect(options).type.toBe>() }) - expect(async function ( - fastify: FastifyInstance, - options: Options, - _next: any - ) {}).type.not.toBeAssignableTo[0]>() + // @ts-expect-error Target signature provides too few arguments + fastifyPlugin(async function (fastify, options, next) {}) expect( fastifyPlugin(function (_fastify, _options, _next) {}) @@ -244,13 +233,17 @@ expect(fastifyPlugin(pluginCallbackWithTypeProvider)).type.toBe< expect(next).type.toBe<(err?: Error) => void>() }) - expect(function (fastify: FastifyInstance, options: Options, _next: any) { + // @ts-expect-error Target signature provides too few arguments + fastifyPlugin(function (fastify, options, next) { return Promise.resolve() - }).type.not.toBeAssignableTo[0]>() + }) server.register(fastifyExampleCallback, { foo: 'bar' }) - expect({ foo: 'baz' as any }).type.not.toBe<{ foo: string }>() - + expect(server.register).type.not.toBeCallableWith(fastifyExampleCallback, { + foo: 'baz' + }) server.register(fastifyExampleAsync, { foo: 'bar' }) - expect({ foo: 'baz' as any }).type.not.toBe<{ foo: string }>() + expect(server.register).type.not.toBeCallableWith(fastifyExampleAsync, { + foo: 'baz' + }) })()