// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. import { clearTimeout, setTimeout } from "timers"; export function asyncit(expectation: string, assertion?: () => Promise | void, timeout?: number): void { let testFunction: (done: DoneFn) => void; if (assertion) { testFunction = done => { let promise = assertion(); if (promise) { promise.then(() => done()) .catch((err) => { fail(err); done(); }); } else { done(); } }; } it(expectation, testFunction, timeout); } export async function captureException(fn: () => Promise): Promise { try { await fn(); return null; } catch (e) { return e; } } export function delay(durationInMilliseconds: number): Promise { let source = new PromiseSource(); setTimeout(() => source.resolve(), durationInMilliseconds); return source.promise; } export class PromiseSource { public promise: Promise private resolver: (value?: T | PromiseLike) => void; private rejecter: (reason?: any) => void; constructor() { this.promise = new Promise((resolve, reject) => { this.resolver = resolve; this.rejecter = reject; }); } resolve(value?: T | PromiseLike) { this.resolver(value); } reject(reason?: any) { this.rejecter(reason); } }