@0x/contracts-test-utils
: Create blockchainTests
mocha fixture directive.
`@0x/contracts-test-utils`: Automatically call `chaiSetup.configure()`. `@0x/contracts-test-utils`: Export `expect` as an alias for `chai.expect`.
This commit is contained in:
@@ -34,7 +34,6 @@
|
||||
},
|
||||
"homepage": "https://github.com/0xProject/0x-monorepo/contracts/test-utils/README.md",
|
||||
"devDependencies": {
|
||||
"mocha": "^4.1.0",
|
||||
"npm-run-all": "^4.1.2",
|
||||
"shx": "^0.2.2",
|
||||
"tslint": "5.11.0",
|
||||
@@ -67,7 +66,8 @@
|
||||
"ethers": "~4.0.4",
|
||||
"js-combinatorics": "^0.5.3",
|
||||
"lodash": "^4.17.11",
|
||||
"make-promises-safe": "^1.1.0"
|
||||
"make-promises-safe": "^1.1.0",
|
||||
"mocha": "^4.1.0"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
|
@@ -1 +1,7 @@
|
||||
import { chaiSetup } from '@0x/dev-utils';
|
||||
export { chaiSetup } from '@0x/dev-utils';
|
||||
import * as chai from 'chai';
|
||||
|
||||
// Set up chai.
|
||||
chaiSetup.configure();
|
||||
export const expect = chai.expect;
|
||||
|
@@ -1,5 +1,4 @@
|
||||
export { AbstractAssetWrapper } from './abstract_asset_wrapper';
|
||||
export { chaiSetup } from './chai_setup';
|
||||
export { constants } from './constants';
|
||||
export {
|
||||
expectContractCallFailedAsync,
|
||||
@@ -46,3 +45,5 @@ export {
|
||||
Token,
|
||||
TransactionDataParams,
|
||||
} from './types';
|
||||
export { blockchainTests} from './mocha_blockchain';
|
||||
export { chaiSetup, expect } from './chai_setup';
|
||||
|
130
contracts/test-utils/src/mocha_blockchain.ts
Normal file
130
contracts/test-utils/src/mocha_blockchain.ts
Normal file
@@ -0,0 +1,130 @@
|
||||
import { BlockchainLifecycle } from '@0x/dev-utils';
|
||||
import { Web3Wrapper } from '@0x/web3-wrapper';
|
||||
import * as _ from 'lodash';
|
||||
import * as mocha from 'mocha';
|
||||
|
||||
import { web3Wrapper } from './web3_wrapper';
|
||||
|
||||
// tslint:disable: no-namespace only-arrow-functions no-unbound-method
|
||||
declare global {
|
||||
export namespace Mocha {
|
||||
export interface BlockchainSuiteState {
|
||||
web3Wrapper: Web3Wrapper;
|
||||
blockchainLifecycle: BlockchainLifecycle;
|
||||
}
|
||||
|
||||
type BlockchainSuiteCallback = (this: ISuiteCallbackContext, state: BlockchainSuiteState) => void;
|
||||
type SuiteCallback = (this: ISuiteCallbackContext) => void;
|
||||
type BlockchainContextDefinitionCallback<T> = (description: string, callback: BlockchainSuiteCallback) => T;
|
||||
type ContextDefinitionCallback<T> = (description: string, callback: SuiteCallback) => T;
|
||||
|
||||
interface BlockchainContextDefinition {
|
||||
reset: {
|
||||
only: BlockchainContextDefinitionCallback<ISuite>;
|
||||
skip: BlockchainContextDefinitionCallback<void>;
|
||||
(description: string, callback: BlockchainSuiteCallback): ISuite;
|
||||
};
|
||||
only: BlockchainContextDefinitionCallback<ISuite>;
|
||||
skip: BlockchainContextDefinitionCallback<void>;
|
||||
(description: string, callback: BlockchainSuiteCallback): ISuite;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Singleton instance.
|
||||
let blockchainLifecycle: BlockchainLifecycle | undefined;
|
||||
|
||||
function defineBlockchainSuite<T>(
|
||||
description: string,
|
||||
callback: Mocha.BlockchainSuiteCallback,
|
||||
describeCall: Mocha.ContextDefinitionCallback<T>,
|
||||
): T {
|
||||
init();
|
||||
return describeCall(
|
||||
description,
|
||||
function(this: mocha.ISuiteCallbackContext): void {
|
||||
callback.call(
|
||||
this,
|
||||
{ web3Wrapper, blockchainLifecycle },
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
function defineResetSuite<T>(
|
||||
description: string,
|
||||
callback: Mocha.SuiteCallback,
|
||||
describeCall: Mocha.ContextDefinitionCallback<T>,
|
||||
): T {
|
||||
return describeCall(
|
||||
description,
|
||||
function(this: mocha.ISuiteCallbackContext): void {
|
||||
if (blockchainLifecycle !== undefined) {
|
||||
const _blockchainLifecycle = blockchainLifecycle;
|
||||
beforeEach(async () => {
|
||||
return _blockchainLifecycle.startAsync();
|
||||
});
|
||||
afterEach(async () => {
|
||||
return _blockchainLifecycle.revertAsync();
|
||||
});
|
||||
}
|
||||
callback.call(this);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
function init(): void {
|
||||
if (blockchainLifecycle !== undefined) {
|
||||
return;
|
||||
}
|
||||
// Create the BlockchainLifecycle instance.
|
||||
blockchainLifecycle = new BlockchainLifecycle(web3Wrapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* Like mocha's `describe()`, but sets up a BlockchainLifecycle and Web3Wrapper.
|
||||
*/
|
||||
export const blockchainTests: Mocha.BlockchainContextDefinition = _.assign(
|
||||
function(description: string, callback: Mocha.BlockchainSuiteCallback): Mocha.ISuite {
|
||||
return defineBlockchainSuite(description, callback, describe);
|
||||
},
|
||||
{
|
||||
only(description: string, callback: Mocha.BlockchainSuiteCallback): Mocha.ISuite {
|
||||
return defineBlockchainSuite(description, callback, describe.only);
|
||||
},
|
||||
skip(description: string, callback: Mocha.BlockchainSuiteCallback): void {
|
||||
return defineBlockchainSuite(description, callback, describe.skip);
|
||||
},
|
||||
reset: _.assign(
|
||||
function(description: string, callback: Mocha.BlockchainSuiteCallback): Mocha.ISuite {
|
||||
return defineBlockchainSuite(
|
||||
description,
|
||||
callback,
|
||||
function(_description: string, _callback: Mocha.SuiteCallback): Mocha.ISuite {
|
||||
return defineResetSuite(_description, _callback, describe);
|
||||
},
|
||||
);
|
||||
},
|
||||
{
|
||||
only(description: string, callback: Mocha.BlockchainSuiteCallback): Mocha.ISuite {
|
||||
return defineBlockchainSuite(
|
||||
description,
|
||||
callback,
|
||||
function(_description: string, _callback: Mocha.SuiteCallback): Mocha.ISuite {
|
||||
return defineResetSuite(_description, _callback, describe.only);
|
||||
},
|
||||
);
|
||||
},
|
||||
skip(description: string, callback: Mocha.BlockchainSuiteCallback): void {
|
||||
return defineBlockchainSuite(
|
||||
description,
|
||||
callback,
|
||||
function(_description: string, _callback: Mocha.SuiteCallback): void {
|
||||
return defineResetSuite(_description, _callback, describe.skip);
|
||||
},
|
||||
);
|
||||
},
|
||||
},
|
||||
),
|
||||
},
|
||||
) as Mocha.BlockchainContextDefinition;
|
@@ -1,10 +1,6 @@
|
||||
import * as chai from 'chai';
|
||||
import * as _ from 'lodash';
|
||||
|
||||
import { chaiSetup } from './chai_setup';
|
||||
|
||||
chaiSetup.configure();
|
||||
const expect = chai.expect;
|
||||
import { expect } from './chai_setup';
|
||||
|
||||
class Value<T> {
|
||||
public value: T;
|
||||
|
Reference in New Issue
Block a user