Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swap arguments for fixture #215

Merged
merged 2 commits into from
May 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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