Skip to content

Commit af12905

Browse files
rzadpsz-piotr
andauthored
Swap arguments for fixture (#215)
Co-authored-by: Piotr Szlachciak <[email protected]>
1 parent 4cb7322 commit af12905

File tree

4 files changed

+17
-23
lines changed

4 files changed

+17
-23
lines changed

docs/source/fixtures.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Example:
1414
import BasicTokenMock from './build/BasicTokenMock';
1515
1616
describe('Fixtures', () => {
17-
async function fixture(provider, [wallet, other]) {
17+
async function fixture([wallet, other], provider) {
1818
const token = await deployContract(wallet, BasicTokenMock, [
1919
wallet.address, 1000
2020
]);
@@ -34,16 +34,16 @@ Example:
3434
});
3535
3636
37-
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.
37+
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.
3838

3939
.. code-block:: ts
4040
4141
import {createFixtureLoader} from 'ethereum-waffle';
4242
43-
const loadFixture = createFixtureLoader(myProvider, myWallets);
43+
const loadFixture = createFixtureLoader(myWallets, myProvider);
4444
4545
// later in tests
46-
await loadFixture((myProvider, myWallets) => {
46+
await loadFixture((myWallets, myProvider) => {
4747
// fixture implementation
4848
});
4949

waffle-cli/test/example/fixtures-example.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {Wallet} from 'ethers';
44
import BasicTokenMock from './build/BasicTokenMock.json';
55

66
describe('INTEGRATION: Fixtures example', () => {
7-
async function fixture(provider: MockProvider, [wallet, other]: Wallet[]) {
7+
async function fixture([wallet, other]: Wallet[], provider: MockProvider) {
88
const token = await deployContract(wallet, BasicTokenMock, [
99
wallet.address, 1000
1010
]);

waffle-provider/src/fixtures.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {providers, Wallet} from 'ethers';
22
import {MockProvider} from './MockProvider';
33

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

1313
export const loadFixture = createFixtureLoader();
1414

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

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

28-
const data = await fixture(provider, wallets);
28+
const data = await fixture(wallets, provider);
2929
const id = await provider.send('evm_snapshot', []);
3030

3131
snapshots.push({fixture, data, id, provider, wallets});

waffle-provider/test/fixtures.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ import {TOKEN_ABI, TOKEN_BYTECODE} from './BasicToken';
55

66
describe('Integration: Fixtures', () => {
77
describe('correctly restores state', () => {
8-
async function tokenFixture(
9-
provider: MockProvider,
10-
[sender, recipient]: Wallet[]
11-
) {
8+
async function tokenFixture([sender, recipient]: Wallet[], provider: MockProvider) {
129
const factory = new ContractFactory(TOKEN_ABI, TOKEN_BYTECODE, sender);
1310
return {
1411
contract: await factory.deploy(1_000),
@@ -49,8 +46,8 @@ describe('Integration: Fixtures', () => {
4946

5047
it('allow for restoring blockchain state', async () => {
5148
const fixture = async (
52-
provider: MockProvider,
53-
[wallet, other]: Wallet[]
49+
[wallet, other]: Wallet[],
50+
provider: MockProvider
5451
) => ({wallet, other, provider});
5552

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

7269
describe('allow for multiple uses of different fixtures', () => {
73-
async function sendAB(provider: MockProvider, [a, b]: Wallet[]) {
70+
async function sendAB([a, b]: Wallet[], provider: MockProvider) {
7471
await send(a, b);
7572
return {a, b};
7673
}
7774

78-
async function sendBA(provider: MockProvider, [a, b]: Wallet[]) {
75+
async function sendBA([a, b]: Wallet[], provider: MockProvider) {
7976
await send(b, a);
8077
return {a, b};
8178
}
@@ -116,8 +113,8 @@ describe('Integration: Fixtures', () => {
116113
});
117114

118115
it('run on isolated chains', async () => {
119-
const fixtureA = async (provider: MockProvider) => provider;
120-
const fixtureB = async (provider: MockProvider) => provider;
116+
const fixtureA = async (_: Wallet[], provider: MockProvider) => (provider);
117+
const fixtureB = async (_: Wallet[], provider: MockProvider) => (provider);
121118

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

139-
const customLoadFixture = createFixtureLoader(
140-
customProvider,
141-
customWallets
142-
);
143-
await customLoadFixture(async (provider, wallets) => {
136+
const customLoadFixture = createFixtureLoader(customWallets, customProvider);
137+
await customLoadFixture(async (wallets, provider) => {
144138
receivedProvider = provider;
145139
receivedWallets = wallets;
146140
});

0 commit comments

Comments
 (0)