@0x/contracts-exchange
: Add tests and run prettier.
This commit is contained in:
parent
61fc32b7c0
commit
e01eadaecd
@ -45,5 +45,5 @@ export {
|
|||||||
Token,
|
Token,
|
||||||
TransactionDataParams,
|
TransactionDataParams,
|
||||||
} from './types';
|
} from './types';
|
||||||
export { blockchainTests} from './mocha_blockchain';
|
export { blockchainTests, BlockchainTestsEnvironment } from './mocha_blockchain';
|
||||||
export { chaiSetup, expect } from './chai_setup';
|
export { chaiSetup, expect } from './chai_setup';
|
||||||
|
@ -79,15 +79,9 @@ function defineBlockchainSuite<T>(
|
|||||||
callback: Mocha.BlockchainSuiteCallback,
|
callback: Mocha.BlockchainSuiteCallback,
|
||||||
describeCall: Mocha.ContextDefinitionCallback<T>,
|
describeCall: Mocha.ContextDefinitionCallback<T>,
|
||||||
): T {
|
): T {
|
||||||
return describeCall(
|
return describeCall(description, function(this: Mocha.ISuiteCallbackContext): void {
|
||||||
description,
|
callback.call(this, BlockchainTestsEnvironment.create());
|
||||||
function(this: Mocha.ISuiteCallbackContext): void {
|
});
|
||||||
callback.call(
|
|
||||||
this,
|
|
||||||
BlockchainTestsEnvironment.create(),
|
|
||||||
);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function defineResetsSuite<T>(
|
function defineResetsSuite<T>(
|
||||||
@ -95,22 +89,18 @@ function defineResetsSuite<T>(
|
|||||||
callback: Mocha.SuiteCallback,
|
callback: Mocha.SuiteCallback,
|
||||||
describeCall: Mocha.ContextDefinitionCallback<T>,
|
describeCall: Mocha.ContextDefinitionCallback<T>,
|
||||||
): T {
|
): T {
|
||||||
return describeCall(
|
return describeCall(description, function(this: Mocha.ISuiteCallbackContext): void {
|
||||||
description,
|
const env = BlockchainTestsEnvironment.getInstance();
|
||||||
function(this: Mocha.ISuiteCallbackContext): void {
|
if (env !== undefined) {
|
||||||
const env = BlockchainTestsEnvironment.getInstance();
|
beforeEach(async () => {
|
||||||
if (env !== undefined) {
|
return env.blockchainLifecycle.startAsync();
|
||||||
const _blockchainLifecycle = env.blockchainLifecycle;
|
});
|
||||||
beforeEach(async () => {
|
afterEach(async () => {
|
||||||
return _blockchainLifecycle.startAsync();
|
return env.blockchainLifecycle.revertAsync();
|
||||||
});
|
});
|
||||||
afterEach(async () => {
|
}
|
||||||
return _blockchainLifecycle.revertAsync();
|
callback.call(this);
|
||||||
});
|
});
|
||||||
}
|
|
||||||
callback.call(this);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -128,50 +118,42 @@ export const blockchainTests: Mocha.BlockchainContextDefinition = _.assign(
|
|||||||
return defineBlockchainSuite(description, callback, describe.skip);
|
return defineBlockchainSuite(description, callback, describe.skip);
|
||||||
},
|
},
|
||||||
optional(description: string, callback: Mocha.BlockchainSuiteCallback): Mocha.ISuite | void {
|
optional(description: string, callback: Mocha.BlockchainSuiteCallback): Mocha.ISuite | void {
|
||||||
return defineBlockchainSuite(
|
return defineBlockchainSuite(description, callback, process.env.TEST_ALL ? describe : describe.skip);
|
||||||
description,
|
|
||||||
callback,
|
|
||||||
process.env.TEST_ALL ? describe : describe.skip,
|
|
||||||
);
|
|
||||||
},
|
},
|
||||||
resets: _.assign(
|
resets: _.assign(
|
||||||
function(description: string, callback: Mocha.BlockchainSuiteCallback): Mocha.ISuite {
|
function(description: string, callback: Mocha.BlockchainSuiteCallback): Mocha.ISuite {
|
||||||
return defineBlockchainSuite(
|
return defineBlockchainSuite(description, callback, function(
|
||||||
description,
|
_description: string,
|
||||||
callback,
|
_callback: Mocha.SuiteCallback,
|
||||||
function(_description: string, _callback: Mocha.SuiteCallback): Mocha.ISuite {
|
): Mocha.ISuite {
|
||||||
return defineResetsSuite(_description, _callback, describe);
|
return defineResetsSuite(_description, _callback, describe);
|
||||||
},
|
});
|
||||||
);
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
only(description: string, callback: Mocha.BlockchainSuiteCallback): Mocha.ISuite {
|
only(description: string, callback: Mocha.BlockchainSuiteCallback): Mocha.ISuite {
|
||||||
return defineBlockchainSuite(
|
return defineBlockchainSuite(description, callback, function(
|
||||||
description,
|
_description: string,
|
||||||
callback,
|
_callback: Mocha.SuiteCallback,
|
||||||
function(_description: string, _callback: Mocha.SuiteCallback): Mocha.ISuite {
|
): Mocha.ISuite {
|
||||||
return defineResetsSuite(_description, _callback, describe.only);
|
return defineResetsSuite(_description, _callback, describe.only);
|
||||||
},
|
});
|
||||||
);
|
|
||||||
},
|
},
|
||||||
skip(description: string, callback: Mocha.BlockchainSuiteCallback): void {
|
skip(description: string, callback: Mocha.BlockchainSuiteCallback): void {
|
||||||
return defineBlockchainSuite(
|
return defineBlockchainSuite(description, callback, function(
|
||||||
description,
|
_description: string,
|
||||||
callback,
|
_callback: Mocha.SuiteCallback,
|
||||||
function(_description: string, _callback: Mocha.SuiteCallback): void {
|
): void {
|
||||||
return defineResetsSuite(_description, _callback, describe.skip);
|
return defineResetsSuite(_description, _callback, describe.skip);
|
||||||
},
|
});
|
||||||
);
|
|
||||||
},
|
},
|
||||||
optional(description: string, callback: Mocha.BlockchainSuiteCallback): Mocha.ISuite | void {
|
optional(description: string, callback: Mocha.BlockchainSuiteCallback): Mocha.ISuite | void {
|
||||||
const describeCall = process.env.TEST_ALL ? describe : describe.skip;
|
const describeCall = process.env.TEST_ALL ? describe : describe.skip;
|
||||||
return defineBlockchainSuite(
|
return defineBlockchainSuite(description, callback, function(
|
||||||
description,
|
_description: string,
|
||||||
callback,
|
_callback: Mocha.SuiteCallback,
|
||||||
function(_description: string, _callback: Mocha.SuiteCallback): Mocha.ISuite | void {
|
): Mocha.ISuite | void {
|
||||||
return defineResetsSuite(_description, _callback, describeCall);
|
return defineResetsSuite(_description, _callback, describeCall);
|
||||||
},
|
});
|
||||||
);
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
93
contracts/test-utils/test/mocha_blockchain.ts
Normal file
93
contracts/test-utils/test/mocha_blockchain.ts
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
import * as _ from 'lodash';
|
||||||
|
import * as process from 'process';
|
||||||
|
|
||||||
|
import { blockchainTests, constants, expect } from '../src';
|
||||||
|
|
||||||
|
blockchainTests('mocha blockchain extensions', env => {
|
||||||
|
describe('blockchainTests()', () => {
|
||||||
|
it('passes a valid environment object', () => {
|
||||||
|
expect(env.blockchainLifecycle).to.exist('');
|
||||||
|
expect(env.provider).to.exist('');
|
||||||
|
expect(env.txDefaults).to.exist('');
|
||||||
|
expect(env.web3Wrapper).to.exist('');
|
||||||
|
// HACK(dorothy-zbornak): tslint seems to get confused by these assertions.
|
||||||
|
// tslint:disable: no-unbound-method
|
||||||
|
expect(typeof env.getChainIdAsync).to.eq('function');
|
||||||
|
expect(typeof env.getAccountAddressesAsync).to.eq('function');
|
||||||
|
// tslint:enable: no-unbound-method
|
||||||
|
});
|
||||||
|
|
||||||
|
it('initializes the test environment', async () => {
|
||||||
|
expect(await env.getChainIdAsync()).to.eq(constants.TESTRPC_NETWORK_ID);
|
||||||
|
expect(await env.getAccountAddressesAsync()).to.be.not.empty('');
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('modifiers', () => {
|
||||||
|
blockchainTests.skip('skip', () => {
|
||||||
|
it('does not execute this test', () => {
|
||||||
|
expect.fail();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
blockchainTests(/*.only*/ 'only', () => {
|
||||||
|
it.skip("can't test `only` :-(", () => {
|
||||||
|
// no-op.
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
blockchainTests.optional('optional', () => {
|
||||||
|
it('only runs this with `TEST_ALL` environment flag set', () => {
|
||||||
|
expect(process.env.TEST_ALL).to.be.ok('');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
blockchainTests.resets('resets', () => {
|
||||||
|
const originalBlockhainLifecycle = env.blockchainLifecycle;
|
||||||
|
const blockchainLifecycleCalls = [] as string[];
|
||||||
|
|
||||||
|
before(() => {
|
||||||
|
// Replace `blockchainLifecycle` with a hooked version.
|
||||||
|
env.blockchainLifecycle = createHookedObject(
|
||||||
|
originalBlockhainLifecycle,
|
||||||
|
methodName => blockchainLifecycleCalls.push(methodName),
|
||||||
|
['startAsync', 'revertAsync'],
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
after(() => {
|
||||||
|
// Undo the hook.
|
||||||
|
env.blockchainLifecycle = originalBlockhainLifecycle;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('calls `blockchainLifecycle.startAsync()` before this test', () => {
|
||||||
|
expect(blockchainLifecycleCalls).to.eql(['startAsync']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('calls `blockchainLifecycle.revertAsync()` after the last test', () => {
|
||||||
|
expect(blockchainLifecycleCalls).to.eql(['startAsync', 'revertAsync', 'startAsync']);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
blockchainTests('nested tests', nestedEnv => {
|
||||||
|
it('shares the same environment object', () => {
|
||||||
|
expect(nestedEnv).to.eq(env);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('subtests', () => {
|
||||||
|
require('./subtests/mocha_blockchain_1').define(env);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
function createHookedObject(obj: any, handler: (name: string) => void, methods: string[]): any {
|
||||||
|
const hookedMethods = _.map(methods, methodName => {
|
||||||
|
// tslint:disable: only-arrow-functions
|
||||||
|
return function(this: any, ...args: any[]): any {
|
||||||
|
handler(methodName);
|
||||||
|
return obj[methodName].call(this, ...args);
|
||||||
|
};
|
||||||
|
});
|
||||||
|
return _.assign(_.clone(obj), _.zipObject(methods, hookedMethods));
|
||||||
|
}
|
10
contracts/test-utils/test/subtests/mocha_blockchain_1.ts
Normal file
10
contracts/test-utils/test/subtests/mocha_blockchain_1.ts
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
import { blockchainTests, BlockchainTestsEnvironment, expect } from '../../src';
|
||||||
|
|
||||||
|
// tslint:disable: no-default-export completed-docs
|
||||||
|
export function define(env: BlockchainTestsEnvironment): void {
|
||||||
|
blockchainTests('imported subtests', subtestsEnv => {
|
||||||
|
it('shares the same environment object', () => {
|
||||||
|
expect(subtestsEnv).to.eq(env);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user