mocha#AsyncFunc TypeScript Examples
The following examples show how to use
mocha#AsyncFunc.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: test.ts From eosio-contract-api with GNU Affero General Public License v3.0 | 6 votes |
export function createTxIt(client: TestClient): any {
async function runTxTest(fn: () => Promise<void>, self: any): Promise<any> {
await client.query('BEGIN');
try {
await client.init();
return await fn.call(self, client);
} finally {
await client.query('ROLLBACK');
}
}
const result = function txit(title: string, fn: () => Promise<void>): Test {
return it(title, async function () {
return await runTxTest(fn, this);
});
};
result.skip = (title: string, func: () => Promise<void>): Test => it.skip(title, func as unknown as AsyncFunc);
result.only = function (title: string, fn: () => Promise<void>): Test {
return it.only(title, async () => {
return await runTxTest(fn, this);
});
};
return result;
}
Example #2
Source File: setupTests.ts From balancer-v2-monorepo with GNU General Public License v3.0 | 5 votes |
global.sharedBeforeEach = (nameOrFn: string | AsyncFunc, maybeFn?: AsyncFunc): void => {
sharedBeforeEach(nameOrFn, maybeFn);
};
Example #3
Source File: sharedBeforeEach.ts From balancer-v2-monorepo with GNU General Public License v3.0 | 5 votes |
/**
* This Mocha helper acts as a `beforeEach`, but executes the initializer
* just once. It internally uses Hardhat Network and Ganache's snapshots
* and revert instead of re-executing the initializer.
*
* Note that after the last test is run, the state doesn't get reverted.
*
* @param nameOrFn A title that's included in all the hooks that this helper uses.
* @param maybeFn The initializer to be run before the tests.
*/
export function sharedBeforeEach(nameOrFn: string | AsyncFunc, maybeFn?: AsyncFunc): void {
const name = typeof nameOrFn === 'string' ? nameOrFn : undefined;
const fn = typeof nameOrFn === 'function' ? nameOrFn : (maybeFn as AsyncFunc);
let initialized = false;
beforeEach(wrapWithTitle(name, 'Running shared before each or reverting'), async function () {
const provider = await getProvider();
if (!initialized) {
const prevSnapshot = SNAPSHOTS.pop();
if (prevSnapshot !== undefined) {
await revert(provider, prevSnapshot);
SNAPSHOTS.push(await takeSnapshot(provider));
}
await fn.call(this);
SNAPSHOTS.push(await takeSnapshot(provider));
initialized = true;
} else {
const snapshotId = SNAPSHOTS.pop();
if (snapshotId === undefined) throw Error('Missing snapshot ID');
await revert(provider, snapshotId);
SNAPSHOTS.push(await takeSnapshot(provider));
}
});
after(async function () {
if (initialized) {
SNAPSHOTS.pop();
}
});
}