Monkey-patching Jasmine's it() function to log errors
Just factor it out into a closure

function patchJasmineForAsyncError(method: string) {
    let oldFn = window[method];
    function create_logger(fn: Function) {
        return (done) =&gt; {
            let p = fn();
            if (p instanceof Promise) {
                p.then(done).catch(e =&gt; {
                    console.error(e);
                    done.fail(e);
                });
            }
        }
    }

    if (method === 'it' || method === 'fit') {
        window[method] = function(desc: string, fn: Function) {
            oldFn(desc, create_logger(fn));
        }
    } else {
        window[method] = function (axn: Function) {
            oldFn(create_logger(axn));
        }
    }
} 


We can clean this up a bit more by getting rid of some magic. I also got rid of the () =&gt; done() because that's the same as done.

function patchJasmineForAsyncError(method: string) {
    let oldFn = window[method];
    function create_logger(fn: Function) {
        return (done) =&gt; {
            let p = fn();
            if (p instanceof Promise) {
                p.then(done).catch(e =&gt; {
                    console.error(e);
                    done.fail(e);
                });
            }
        }
    }

    let allowed_methods = ['it', 'fit'];
    if (allowed_methods.includes(method)) {
        window[method] = function(desc: string, fn: Function) {
            oldFn(desc, create_logger(fn));
        }
    } else {
        window[method] = function (axn: Function) {
            oldFn(create_logger(axn));
        }
    }
} 


We can also reduce some of the duplication in assigning to window[method] if we're okay with adding a slight slow-down to the function, which should be fine.

function patchJasmineForAsyncError(method: string) {
    let oldFn = window[method];
    function create_logger(fn: Function) {
        return (done) =&gt; {
            let p = fn();
            if (p instanceof Promise) {
                p.then(done)
                 .catch(e =&gt; {
                    console.error(e);
                    done.fail(e);
                });
            }
        }
    }
    let allowed_methods = ['it', 'fit'];
    window[method] = function() {
        if (allowed_methods.includes(method)) {
            oldFn(arguments[0], create_logger(arguments[1]));
        } else {
            oldFn(create_logger(arguments[0]));
        }
    }
}