diff --git a/src/base.ts b/src/base.ts index 2376042..a3bb808 100644 --- a/src/base.ts +++ b/src/base.ts @@ -2,6 +2,7 @@ // tslint:disable no-unused import * as _ from 'lodash' +import * as mocha from 'mocha' import * as Types from './types' @@ -12,18 +13,18 @@ const context: Types.Context = { } const base = (context: I): Types.Base => { - const end = (arg1: any, cb: any) => { + const end = (arg1: any, cb: Types.MochaCallback) => { context = assignWithProps({}, context) if (_.isFunction(arg1)) { cb = arg1 arg1 = undefined } if (!arg1) arg1 = context.expectation || 'test' - async function run(done?: Types.MochaDone) { + async function run(this: mocha.ITestCallbackContext, done?: Types.MochaDone) { if (cb) { context.chain = [...context.chain, { run: async (input: any) => { - await cb(input, done) + await cb.call(this, input, done) } }] } @@ -53,10 +54,11 @@ const base = (context: I): Types.Base => { } if (context.error) throw context.error } - function runWithDone(done: MochaDone) { - run(done).catch(done) - } - return context.test(arg1, (cb && cb.length === 2) ? runWithDone : () => run()) + return context.test(arg1, (cb && cb.length === 2) ? function (done) { + run.call(this, done).catch(done) + } : function () { + return run.call(this) + }) } return { ...Object.entries(context.plugins) diff --git a/src/types.ts b/src/types.ts index d7505ff..c60bb10 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,3 +1,5 @@ +import * as mocha from 'mocha' + export type PluginBuilder = (arg1?: A1, arg2?: A2, arg3?: A3, arg4?: A4) => Plugin export interface Context { @@ -25,9 +27,10 @@ export interface PluginDef { export interface Plugins {[k: string]: PluginDef} +export type MochaCallback = (this: mocha.ITestCallbackContext, context: I, done: MochaDone) => any export interface It { - (expectation: string, cb?: (context: I, done: MochaDone) => any): void - (cb?: (context: I, done: MochaDone) => any): void + (expectation: string, cb?: MochaCallback): void + (cb?: MochaCallback): void } export type Base = {