[DEPRECATED] Common base for my other typescript projects
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

40 lines
910 B

// Testing tools
export {
it,
expect,
} from "https://raw.githubusercontent.com/allain/expect/9effa6c6da3bcf4b66114b44e6b1662e85c91337/mod.ts";
import {
mock,
} from "https://raw.githubusercontent.com/allain/expect/9effa6c6da3bcf4b66114b44e6b1662e85c91337/mod.ts";
export type PatchResult = {
exec(body: Function): void;
fn: Function;
};
export const test = Deno.test;
export function patch<
T extends Object,
K extends keyof T,
F extends T[K] & Function,
>(
obj: T,
method: K,
...stubs: F[]
): PatchResult {
const result = {
fn: () => {},
exec(body: Function) {
const orig = obj[method] as unknown as Function;
result.fn = mock.fn(...(stubs.length ? stubs : [orig.bind(obj)]));
Object.assign(obj, { [method]: result.fn });
try {
body();
} finally {
Object.assign(obj, { [method]: orig });
}
},
};
return result;
}