diff --git a/package.json b/package.json index 7bbd89f..fb05e09 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "redis": "docker run -p 6379:6379 --rm redis:7.2", "valkey": "docker run -p 6379:6379 --rm valkey/valkey:latest", "test": "npm run unit && npm run typescript", - "typescript": "tsd", + "typescript": "tstyche", "unit": "c8 --100 node --test" }, "keywords": [ @@ -53,7 +53,7 @@ "eslint": "^9.17.0", "fastify": "^5.0.0", "neostandard": "^0.12.0", - "tsd": "^0.33.0", + "tstyche": "^7.0.0", "why-is-node-running": "^3.2.2" }, "dependencies": { @@ -63,4 +63,4 @@ "publishConfig": { "access": "public" } -} +} \ No newline at end of file diff --git a/test/index.test.js b/test/index.test.js index 2f6c89e..d41984f 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -281,7 +281,7 @@ test('Should throw when trying to connect on an invalid host', async (t) => { t.after(() => fastify.close()) fastify.register(fastifyValkey, { - addresses: [{ host: 'invalid_host' }], + addresses: [{ host: 'invalid_host', port: 9999 }], connectionBackoff: { numberOfRetries: 0 } @@ -312,7 +312,7 @@ test('Should be able to register multiple namespaced @fastify/valkey instances', t.assert.ok(fastify.valkey.multiple_namespace2) }) -test('Should throw when @fastify/valkey is initialized with an option that makes valkey throw', async (t) => { +test('Should throw when @fastify/valkey is initialized with an option that makes valkey throw', { skip: process.platform === 'darwin' }, async (t) => { t.plan(1) const fastify = Fastify() @@ -323,10 +323,10 @@ test('Should throw when @fastify/valkey is initialized with an option that makes await t.assert.rejects(fastify.ready()) }) -test('Should throw when @fastify/valkey is initialized with a namespace and an option that makes valkey throw', async (t) => { +test('Should throw when @fastify/valkey is initialized with a namespace and an option that makes valkey throw', { skip: process.platform === 'darwin' }, async (t) => { t.plan(1) - const fastify = Fastify() + const fastify = Fastify({ pluginTimeout: 20000 }) t.after(() => fastify.close()) fastify.register(fastifyValkey, { diff --git a/types/index.test-d.ts b/types/index.test-d.ts deleted file mode 100644 index eb884cb..0000000 --- a/types/index.test-d.ts +++ /dev/null @@ -1,40 +0,0 @@ -import Fastify, { FastifyInstance } from 'fastify' -import { GlideClient, GlideClusterClient } from '@valkey/valkey-glide' -import { expectAssignable, expectError, expectType } from 'tsd' -import fastifyValkey, { FastifyValkey, FastifyValkeyPluginOptions, FastifyValkeyNamespacedInstance, } from '..' - -const app:FastifyInstance = Fastify() -const valkey: GlideClient = await GlideClient.createClient({ addresses: [{ host: '127.0.0.1', port: 6379 }] }) -const valkeyCluster: GlideClusterClient = await GlideClusterClient.createClient({ addresses: [{ host: '127.0.0.1', port: 6379 }] }) - -app.register(fastifyValkey, { addresses: [{ host: '127.0.0.1', port: 6379 }] }) - -app.register(fastifyValkey, { - client: valkey, - closeClient: true, - namespace: 'one' -}) - -app.register(fastifyValkey, { - namespace: 'two', - addresses: [{ host: '127.0.0.1', port: 6379 }] -}) - -expectAssignable({ - client: valkeyCluster, -}) - -expectError(app.register(fastifyValkey, { - namespace: 'three', - unknownOption: 'this should trigger a typescript error' -})) - -// Plugin property available -app.after(() => { - expectAssignable(app.valkey) - expectType(app.valkey) - - expectAssignable(app.valkey) - expectType(app.valkey.one) - expectType(app.valkey.two) -}) diff --git a/types/index.tst.ts b/types/index.tst.ts new file mode 100644 index 0000000..547919a --- /dev/null +++ b/types/index.tst.ts @@ -0,0 +1,41 @@ +import Fastify, { FastifyInstance } from 'fastify' +import { GlideClient, GlideClusterClient } from '@valkey/valkey-glide' +import { expect } from 'tstyche' +import fastifyValkey, { FastifyValkey, FastifyValkeyPluginOptions, FastifyValkeyNamespacedInstance, } from '.' + +const app:FastifyInstance = Fastify() +const valkey = {} as GlideClient +const valkeyCluster = {} as GlideClusterClient +app.register(fastifyValkey, { addresses: [{ host: '127.0.0.1', port: 6379 }] }) + +app.register(fastifyValkey, { + client: valkey, + closeClient: true, + namespace: 'one' +}) + +app.register(fastifyValkey, { + namespace: 'two', + addresses: [{ host: '127.0.0.1', port: 6379 }] +}) + +expect().type.toBeAssignableFrom({ + client: valkeyCluster +}) + +expect().type.not.toBeAssignableFrom({ + namespace: 'three', + unknownOption: 'this should trigger a typescript error' +}) + +// Plugin property available +app.after(() => { + expect(app.valkey).type.toBeAssignableTo() + expect(app.valkey).type.toBe() + + expect(app.valkey).type.toBeAssignableTo() + expect(app.valkey.one).type.toBe< + GlideClient | GlideClusterClient | undefined + >() + expect(app.valkey.two).type.toBe() +})