Skip to content

Commit

Permalink
feat: test client resetFork decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm committed Aug 18, 2023
1 parent 31ea734 commit 4015600
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
2 changes: 2 additions & 0 deletions packages/react/src/hooks/useBlockNumber.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { expect, test } from 'vitest'
import { useBlockNumber } from './useBlockNumber.js'

test('mounts', async () => {
await testClient.mainnet.resetFork()

const { result } = renderHook(() => useBlockNumber())

await waitFor(() => expect(result.current.isSuccess).toBeTruthy())
Expand Down
2 changes: 1 addition & 1 deletion packages/test/src/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { getRpcUrls } from './utils.js'

type Fork = { blockNumber: bigint; url: string }

type Chain = Evaluate<
export type Chain = Evaluate<
viem_Chain & {
fork: Fork
port: number
Expand Down
37 changes: 32 additions & 5 deletions packages/test/src/clients.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,57 @@
import { createTestClient, http } from 'viem'
import {
type Account,
type Client,
type TestActions,
type TestRpcSchema,
type Transport,
createTestClient,
http,
} from 'viem'

import { mainnet, mainnet2, optimism } from './chains.js'
import { type Chain, mainnet, mainnet2, optimism } from './chains.js'

export const mainnetTestClient = createTestClient({
mode: 'anvil',
cacheTime: 0,
chain: mainnet,
transport: http(),
})
}).extend(decorator)

export const mainnet2TestClient = createTestClient({
mode: 'anvil',
cacheTime: 0,
chain: mainnet2,
transport: http(),
})
}).extend(decorator)

export const optimismTestClient = createTestClient({
mode: 'anvil',
cacheTime: 0,
chain: optimism,
transport: http(),
})
}).extend(decorator)

export const testClient = {
mainnet: mainnetTestClient,
mainnet2: mainnet2TestClient,
optimism: optimismTestClient,
}

function decorator(
client: Client<
Transport,
Chain,
Account | undefined,
TestRpcSchema<'anvil'>,
TestActions
>,
) {
return {
resetFork() {
return client.reset({
jsonRpcUrl: client.chain.fork.url,
blockNumber: client.chain.fork.blockNumber,
})
},
}
}
7 changes: 1 addition & 6 deletions packages/test/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@ import { pool } from './constants.js'
afterAll(async () => {
// If you are using a fork, you can reset your anvil instance to the initial fork block.
await Promise.all(
Object.values(testClient).map((client) =>
client.reset({
jsonRpcUrl: client.chain.fork.url,
blockNumber: client.chain.fork.blockNumber,
}),
),
Object.values(testClient).map((client) => client.resetFork()),
)
})

Expand Down

1 comment on commit 4015600

@vercel
Copy link

@vercel vercel bot commented on 4015600 Aug 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

wagmi-v2 – ./docs

wagmi-v2.vercel.app
wagmi-v2-wagmi-dev.vercel.app
alpha.wagmi.sh
wagmi-v2-git-alpha-wagmi-dev.vercel.app

Please sign in to comment.