Skip to content

Commit

Permalink
Swap arguments for fixture (#215)
Browse files Browse the repository at this point in the history
Co-authored-by: Piotr Szlachciak <[email protected]>
  • Loading branch information
rzadp and sz-piotr authored May 28, 2020
1 parent 4cb7322 commit af12905
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 23 deletions.
8 changes: 4 additions & 4 deletions docs/source/fixtures.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Example:
import BasicTokenMock from './build/BasicTokenMock';
describe('Fixtures', () => {
async function fixture(provider, [wallet, other]) {
async function fixture([wallet, other], provider) {
const token = await deployContract(wallet, BasicTokenMock, [
wallet.address, 1000
]);
Expand All @@ -34,16 +34,16 @@ Example:
});
Fixtures receive a provider and an array of wallets as an argument. By default, the provider is obtained by calling `createMockProvider` and the wallets by `getWallets`. You can, however, override those by using a custom fixture loader.
Fixtures receive a provider and an array of wallets as an argument. By default, the wallets are obtained by calling `getWallets` and the provider by `createMockProvider`. You can, however, override those by using a custom fixture loader.

.. code-block:: ts
import {createFixtureLoader} from 'ethereum-waffle';
const loadFixture = createFixtureLoader(myProvider, myWallets);
const loadFixture = createFixtureLoader(myWallets, myProvider);
// later in tests
await loadFixture((myProvider, myWallets) => {
await loadFixture((myWallets, myProvider) => {
// fixture implementation
});
Expand Down
2 changes: 1 addition & 1 deletion waffle-cli/test/example/fixtures-example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {Wallet} from 'ethers';
import BasicTokenMock from './build/BasicTokenMock.json';

describe('INTEGRATION: Fixtures example', () => {
async function fixture(provider: MockProvider, [wallet, other]: Wallet[]) {
async function fixture([wallet, other]: Wallet[], provider: MockProvider) {
const token = await deployContract(wallet, BasicTokenMock, [
wallet.address, 1000
]);
Expand Down
6 changes: 3 additions & 3 deletions waffle-provider/src/fixtures.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {providers, Wallet} from 'ethers';
import {MockProvider} from './MockProvider';

type Fixture<T> = (provider: MockProvider, wallets: Wallet[]) => Promise<T>;
type Fixture<T> = (wallets: Wallet[], provider: MockProvider) => Promise<T>;
interface Snapshot<T> {
fixture: Fixture<T>;
data: T;
Expand All @@ -12,7 +12,7 @@ interface Snapshot<T> {

export const loadFixture = createFixtureLoader();

export function createFixtureLoader(overrideProvider?: MockProvider, overrideWallets?: Wallet[]) {
export function createFixtureLoader(overrideWallets?: Wallet[], overrideProvider?: MockProvider) {
const snapshots: Snapshot<any>[] = [];

return async function load<T>(fixture: Fixture<T>): Promise<T> {
Expand All @@ -25,7 +25,7 @@ export function createFixtureLoader(overrideProvider?: MockProvider, overrideWal
const provider = overrideProvider ?? new MockProvider();
const wallets = overrideWallets ?? provider.getWallets();

const data = await fixture(provider, wallets);
const data = await fixture(wallets, provider);
const id = await provider.send('evm_snapshot', []);

snapshots.push({fixture, data, id, provider, wallets});
Expand Down
24 changes: 9 additions & 15 deletions waffle-provider/test/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import {TOKEN_ABI, TOKEN_BYTECODE} from './BasicToken';

describe('Integration: Fixtures', () => {
describe('correctly restores state', () => {
async function tokenFixture(
provider: MockProvider,
[sender, recipient]: Wallet[]
) {
async function tokenFixture([sender, recipient]: Wallet[], provider: MockProvider) {
const factory = new ContractFactory(TOKEN_ABI, TOKEN_BYTECODE, sender);
return {
contract: await factory.deploy(1_000),
Expand Down Expand Up @@ -49,8 +46,8 @@ describe('Integration: Fixtures', () => {

it('allow for restoring blockchain state', async () => {
const fixture = async (
provider: MockProvider,
[wallet, other]: Wallet[]
[wallet, other]: Wallet[],
provider: MockProvider
) => ({wallet, other, provider});

const {wallet, other, provider} = await loadFixture(fixture);
Expand All @@ -70,12 +67,12 @@ describe('Integration: Fixtures', () => {
});

describe('allow for multiple uses of different fixtures', () => {
async function sendAB(provider: MockProvider, [a, b]: Wallet[]) {
async function sendAB([a, b]: Wallet[], provider: MockProvider) {
await send(a, b);
return {a, b};
}

async function sendBA(provider: MockProvider, [a, b]: Wallet[]) {
async function sendBA([a, b]: Wallet[], provider: MockProvider) {
await send(b, a);
return {a, b};
}
Expand Down Expand Up @@ -116,8 +113,8 @@ describe('Integration: Fixtures', () => {
});

it('run on isolated chains', async () => {
const fixtureA = async (provider: MockProvider) => provider;
const fixtureB = async (provider: MockProvider) => provider;
const fixtureA = async (_: Wallet[], provider: MockProvider) => (provider);
const fixtureB = async (_: Wallet[], provider: MockProvider) => (provider);

const providerA1 = await loadFixture(fixtureA);
const providerA2 = await loadFixture(fixtureA);
Expand All @@ -136,11 +133,8 @@ describe('Integration: Fixtures', () => {
let receivedProvider: any;
let receivedWallets: any;

const customLoadFixture = createFixtureLoader(
customProvider,
customWallets
);
await customLoadFixture(async (provider, wallets) => {
const customLoadFixture = createFixtureLoader(customWallets, customProvider);
await customLoadFixture(async (wallets, provider) => {
receivedProvider = provider;
receivedWallets = wallets;
});
Expand Down

0 comments on commit af12905

Please sign in to comment.