Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

Commit

Permalink
fix: set this on mocha callback
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Jan 27, 2018
1 parent 47d762b commit 5968028
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
16 changes: 9 additions & 7 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// tslint:disable no-unused

import * as _ from 'lodash'
import * as mocha from 'mocha'

import * as Types from './types'

Expand All @@ -12,18 +13,18 @@ const context: Types.Context = {
}

const base = <I extends Types.Context>(context: I): Types.Base<I, {}> => {
const end = (arg1: any, cb: any) => {
const end = (arg1: any, cb: Types.MochaCallback<I>) => {
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)
}
}]
}
Expand Down Expand Up @@ -53,10 +54,11 @@ const base = <I extends Types.Context>(context: I): Types.Base<I, {}> => {
}
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)
Expand Down
7 changes: 5 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as mocha from 'mocha'

export type PluginBuilder<I, A1 = undefined, A2 = undefined, A3 = undefined, A4 = undefined> = (arg1?: A1, arg2?: A2, arg3?: A3, arg4?: A4) => Plugin<I>

export interface Context {
Expand Down Expand Up @@ -25,9 +27,10 @@ export interface PluginDef {

export interface Plugins {[k: string]: PluginDef}

export type MochaCallback<I> = (this: mocha.ITestCallbackContext, context: I, done: MochaDone) => any
export interface It<I> {
(expectation: string, cb?: (context: I, done: MochaDone) => any): void
(cb?: (context: I, done: MochaDone) => any): void
(expectation: string, cb?: MochaCallback<I>): void
(cb?: MochaCallback<I>): void
}

export type Base<I extends Context, T extends Plugins> = {
Expand Down

0 comments on commit 5968028

Please sign in to comment.