From 698f48b82e19cc687a08d025e8607fa649ae6f75 Mon Sep 17 00:00:00 2001 From: David Worms Date: Sat, 28 Sep 2024 17:47:39 +0200 Subject: [PATCH] fix: infer typing in promise fn argument --- samples/before-after-typescript.ts | 2 +- samples/usage-typescript.ts | 2 +- src/index.ts | 23 +++++++++++++---------- test/handler.ts | 23 ++++++++++++++++++++--- tsconfig.json | 3 ++- 5 files changed, 37 insertions(+), 16 deletions(-) diff --git a/samples/before-after-typescript.ts b/samples/before-after-typescript.ts index 9547f33..7e69e6b 100644 --- a/samples/before-after-typescript.ts +++ b/samples/before-after-typescript.ts @@ -54,7 +54,7 @@ const they = configure( ); describe("Test before/after usage", function () { - they("Called 3 times", function (config: ConfigConnected) { + they("Called 3 times", function (config) { if (config.ssh === undefined) { console.info(" ".repeat(6) + "SSH client not connected"); } else { diff --git a/samples/usage-typescript.ts b/samples/usage-typescript.ts index ab185f9..f904391 100755 --- a/samples/usage-typescript.ts +++ b/samples/usage-typescript.ts @@ -23,7 +23,7 @@ const they = configure([ ]); describe("Test mocha-they", function () { - they("Call 2 times", function (conf: Config) { + they("Call 2 times", function (conf) { if (conf.ssh === undefined) { console.info(" ".repeat(6) + "Got null."); } else { diff --git a/src/index.ts b/src/index.ts index b373f40..d510a2f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -23,15 +23,11 @@ export type TheyFunc = ( config: T, done: mocha.Done, ) => void; -export type TheyAsyncFunc = ( - this: mocha.Context, - config: T, -) => PromiseLike; type They = { - only: (msg: string, fn: TheyFunc | TheyAsyncFunc) => mocha.Test[]; - skip: (msg: string, fn: TheyFunc | TheyAsyncFunc) => mocha.Test[]; - (msg: string, fn: TheyFunc | TheyAsyncFunc): mocha.Test[]; + only: (msg: string, fn: TheyFunc) => mocha.Test[]; + skip: (msg: string, fn: TheyFunc) => mocha.Test[]; + (msg: string, fn: TheyFunc): mocha.Test[]; }; function configure(configs: (T | (() => T))[]): They; @@ -59,7 +55,7 @@ function configure( function handle( msg: string, label: string, - fn: TheyFunc | TheyAsyncFunc, + fn: TheyFunc, config: T | (() => T), ): [string, mocha.Func | mocha.AsyncFunc] { if (typeof config === "function") { @@ -71,26 +67,29 @@ function configure( `${msg} (${label})`, fn.length === 0 || fn.length === 1 ? (function (this: mocha.Context) { - // return (fn as TheyAsyncFunc).call(this, configNormalized); + // Promise Style return Promise.resolve() .then(async () => { + // Before hook const configNormalized = // eslint-disable-next-line mocha/no-top-level-hooks before === undefined ? config : await before(config); return configNormalized; }) .then(async (config): Promise<[T | U, unknown, unknown]> => { + // Handler execution try { return [ config, undefined, - await (fn as TheyAsyncFunc).call(this, config), + await (fn as TheyFunc).call(this, config, () => {}), ]; } catch (err: unknown) { return [config, err, undefined]; } }) .then(async ([config, err, prom]) => { + // After hook if (after !== undefined) { // eslint-disable-next-line mocha/no-top-level-hooks await after(config); @@ -103,8 +102,10 @@ function configure( }); } as mocha.AsyncFunc) : (function (this: mocha.Context, next: mocha.Done) { + // Callback Style Promise.resolve() .then(async () => { + // Before hook const configNormalized = // eslint-disable-next-line mocha/no-sibling-hooks,mocha/no-top-level-hooks before === undefined ? config : await before(config); @@ -112,6 +113,7 @@ function configure( }) .then( (config) => + // Handler execution new Promise<[T | U, unknown[]]>((resolve) => { (fn as TheyFunc).call( this, @@ -123,6 +125,7 @@ function configure( }), ) .then(async ([config, args]: [T | U, unknown[]]) => { + // After hook if (after === undefined) return next(...args); // eslint-disable-next-line mocha/no-sibling-hooks,mocha/no-top-level-hooks await after(config); diff --git a/test/handler.ts b/test/handler.ts index 677e701..bd10e7f 100644 --- a/test/handler.ts +++ b/test/handler.ts @@ -1,19 +1,28 @@ +import should from "should"; import { configure } from "../src/index.js"; interface Config { - ssh: { + ssh: null | { host: string; username: string | undefined; }; } -const they = configure([ - null, +const they = configure([ + { ssh: null }, { ssh: { host: "127.0.0.1", username: process.env.USER } }, ]); describe("they.handler", function () { describe("promise", function () { + they("argument is typed", function ({ ssh }) { + // Test this + this.retries(1); + // Test function argument + should(ssh === null || typeof ssh.host === "string").be.true(); + return true; + }); + they("return `true`", function () { return true; }); @@ -30,6 +39,14 @@ describe("they.handler", function () { }); describe("callbak", function () { + they("argument is typed", function ({ ssh }, next) { + // Test this + this.retries(1); + // Test function argument + should(ssh === null || typeof ssh.host === "string").be.true(); + next(); + }); + they("call next synchronously", function (conf, next) { this.timeout(200); next(); diff --git a/tsconfig.json b/tsconfig.json index 5e6d79e..2bf8d12 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,6 +11,7 @@ /* Enable all strict type-checking options. */ "strict": true, "sourceMap": true, - "declaration": true + "declaration": true, + "allowSyntheticDefaultImports": true } }