All files / src/util wrap.ts

58.33% Statements 14/24
100% Branches 0/0
71.43% Functions 5/7
59.09% Lines 13/22
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 4012x 5x 5x 4x   5x 5x             5x 5x 5x 5x 5x 4x 4x                                        
// I'm not sure why mocha doesn't provide something like this, you can't
// always use promises
export default (done: (...args) => void, cb: (...args: any[]) => any) => (
  ...args: any[]
) => {
  try {
    return cb(...args);
  } catch (e) {
    done(e);
  }
};
 
export function withWarning(func: Function, regex: RegExp) {
  let message: string = null as never;
  const oldWarn = console.warn;
 
  console.warn = (m: string) => (message = m);
 
  return Promise.resolve(func()).then(val => {
    expect(message).toMatch(regex);
    console.warn = oldWarn;
    return val;
  });
}

export function withError(func: Function, regex: RegExp) {
  let message: string = null as never;
  const oldError = console.error;

  console.error = (m: string) => (message = m);
 
  try {
    const result = func();
    expect(message).toMatch(regex);
    return result;
  } finally {
    console.error = oldError;
  }
}