diff --git a/README.md b/README.md index 06f129fa..c045be3f 100644 --- a/README.md +++ b/README.md @@ -101,3 +101,5 @@ If you cloned without `--recursive` you'll need to run `git submodule update --i ### Testing If a snapshot test fails, it's possible it just needs to be updated. Make sure your git status is clean and run `npm test -- -u`. + +Types are tested using [expect-type](https://npmjs.com/package/expect-type). diff --git a/package.json b/package.json index 4ca12951..b75dc213 100644 --- a/package.json +++ b/package.json @@ -81,6 +81,7 @@ "cross-spawn": "^7.0.1", "del-cli": "^3.0.0", "dotenv-extended": "^2.3.0", + "expect-type": "^0.7.4", "jest": "^25.1.0", "lodash": "^4.17.14", "npm-run-all": "^4.1.5", diff --git a/src/index.ts b/src/index.ts index c314099c..2f94039c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -21,7 +21,7 @@ export const createHandyClient: ICreateHandyClient = (...clientArgs: any[]) => { Object.keys(nodeRedis.__proto__).forEach((key: keyof IHandyRedis) => { const func = nodeRedis[key]; - if (useUnderlyingImpl.has(key)) { + if (useUnderlyingImpl.has(key as any)) { handyClient[key] = func.bind(nodeRedis); } else { const wrapped = (...args: any[]) => new Promise((resolve, reject) => { diff --git a/src/overrides.ts b/src/overrides.ts index 60e9a4a6..f8a0bb7f 100644 --- a/src/overrides.ts +++ b/src/overrides.ts @@ -1,9 +1,7 @@ -import { IHandyRedis } from "./generated/interface"; -import { Multi } from "redis"; +import { Multi, RedisClient } from "redis"; -export const useUnderlyingImpl = new Set([ - "multi" -]); +export const useUnderlyingImpl = new Set(['multi', 'end'] as const) +type UnderlyingImplMethods = typeof useUnderlyingImpl extends Set ? X : never export const additionalFunctions = { /** promisified multi execution */ @@ -12,4 +10,6 @@ export const additionalFunctions = { ), }; -export type AdditionalFunctions = typeof additionalFunctions; +export type AdditionalFunctions = typeof additionalFunctions & { + [K in UnderlyingImplMethods]: RedisClient[K]; +}; diff --git a/test/basic-usage.test.ts b/test/basic-usage.test.ts index 48cd906f..30b36f6a 100644 --- a/test/basic-usage.test.ts +++ b/test/basic-usage.test.ts @@ -87,3 +87,9 @@ it("works with redis-mock", async () => { expect(client.redis).toBe(mockClient); }); + +it('has quit and end methods', async () => { + const client = createHandyClient(); + expect(typeof client.quit).toBe('function'); + expect(typeof client.end).toBe('function'); +}) diff --git a/test/types.test.ts b/test/types.test.ts new file mode 100644 index 00000000..8fe283d6 --- /dev/null +++ b/test/types.test.ts @@ -0,0 +1,41 @@ +import { expectTypeOf } from "expect-type"; +import { createHandyClient } from "../src"; +import { RedisClient } from "redis"; + +test("create client with existing client", () => { + expectTypeOf(createHandyClient).toBeCallableWith({} as RedisClient) +}) + +test("client has promisified redis methods", () => { + expectTypeOf(createHandyClient) + .returns.toHaveProperty("get") + .returns.resolves.toEqualTypeOf() + + expectTypeOf(createHandyClient) + .returns.toHaveProperty("set") + .returns.resolves.toEqualTypeOf() + + expectTypeOf(createHandyClient) + .returns.toHaveProperty("geohash") + .parameters.toEqualTypeOf<[string, ...string[]]>() + + expectTypeOf(createHandyClient) + .returns.toHaveProperty("geohash") + .returns.resolves.items.toBeString() + + expectTypeOf(createHandyClient) + .returns.toHaveProperty("zrevrange") + .parameters.toEqualTypeOf<[string, number, number]>() + + expectTypeOf(createHandyClient) + .returns.toHaveProperty("zrevrange") + .returns.resolves.items.toBeString() + + expectTypeOf(createHandyClient) + .returns.toHaveProperty("quit") + .returns.resolves.toBeAny() + + expectTypeOf(createHandyClient) + .returns.toHaveProperty("end") + .returns.toEqualTypeOf() +}) diff --git a/yarn.lock b/yarn.lock index e27dbb62..dcd12ac5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2352,6 +2352,11 @@ expand-brackets@^2.1.4: snapdragon "^0.8.1" to-regex "^3.0.1" +expect-type@^0.7.4: + version "0.7.4" + resolved "https://registry.yarnpkg.com/expect-type/-/expect-type-0.7.4.tgz#588739f5e86e6713df49ae43812570f11225f9d7" + integrity sha512-/ykfDxhYq9vz/EgqH8YTeIehvb9YCP2W7qztpEqm84LaA23nOeGe6+H7huxxfncehig2bV0S0Gyhf+ncUwt2Mg== + expect@^25.4.0: version "25.4.0" resolved "https://registry.yarnpkg.com/expect/-/expect-25.4.0.tgz#0b16c17401906d1679d173e59f0d4580b22f8dc8"