# Testing

The @ethect/core/testing entry point. A real local node per test, a provider that answers from typed handlers, and a wallet and a signer that a test controls.

`@ethect/core/testing` holds what the library tests itself with. `AnvilNode` starts a real
local node per test, and `MockProvider` is a provider that answers from typed handlers.
`TestWallet` and `TestSigner` stand for the user: a wallet whose answers the test decides, and
a signer that needs no key.

```ts
import { Effect } from "effect";
import { Transport } from "@ethect/core";
import { Eth, Wallet } from "@ethect/core/rpc";
import { TestWallet } from "@ethect/core/testing";

// Run it over a node: `AnvilNode` gives one per test.
const refused = Effect.gen(function* () {
  const [alice, bob] = yield* Eth.accounts();
  // a user who says no to every transaction
  const approve = ({ method }: { readonly method: string }) => method !== "eth_sendTransaction";
  const wallet = yield* TestWallet.make({ accounts: [alice!], approve });
  return yield* Wallet.sendTransaction({ to: bob!, value: 1n }).pipe(
    Effect.as("sent"),
    Effect.catchReason("RpcError", "UserRejectedRequest", () => Effect.succeed("refused")),
    Effect.provide(Transport.eip1193(wallet.provider)),
  );
}); // "refused"
```

| Export | Kind | Description |
| --- | --- | --- |
| [`AnvilNode`](https://ethect.dev/reference/testing/anvil-node/) | module | A scoped anvil process with its own port, IPC socket and chain. |
| [`AnvilNode.reorg(depth)`](https://ethect.dev/reference/testing/reorg/) | function | Replaces the last blocks of the chain by empty ones. |
| [`AnvilNode.placeMulticall3(at?)`](https://ethect.dev/reference/testing/place-multicall3/) | function | Puts Multicall3 where every chain has it. |
| [`MockProvider`](https://ethect.dev/reference/testing/mock-provider/) | module | An EIP-1193 provider that answers from handlers. |
| [`TestWallet`](https://ethect.dev/reference/testing/test-wallet/) | module | A wallet over a node, whose user is the test. |
| [`TestSigner`](https://ethect.dev/reference/testing/test-signer/) | module | A `Signer` over the accounts a node has unlocked. |

## Which one

| You want to test | Use |
| --- | --- |
| reads, transactions, logs, subscriptions, state set up with [`Anvil`](https://ethect.dev/rpc/anvil/) methods | `AnvilNode` |
| what your code does when the last blocks are replaced | `AnvilNode.reorg`, on that node |
| that reads issued together are one `eth_call` | `AnvilNode.placeMulticall3`, first: a fresh anvil has no Multicall3 |
| what an app does when a user approves, refuses, or changes account or chain | `TestWallet`, over an `AnvilNode` |
| a wallet that batches calls, one that asks to upgrade first, one that cannot | `TestWallet`, with its `atomic` option |
| sending, without a wallet and without a key | `TestSigner`, over an `AnvilNode` |
| a method anvil does not serve, or an answer of a precise shape | `MockProvider` |
| the exact request your code sends | `TestWallet` or `MockProvider`, through `requests` |
| provider events: `accountsChanged`, `chainChanged`, `disconnect` | `TestWallet`, through `setAccounts`, `setChain` and `disconnect` |

`TestWallet` answers as a wallet does, and what it signs and sends is real: it happens on the
node. `MockProvider` answers what its handlers say, and nothing happens anywhere.

## Requirements

| | Needs |
| --- | --- |
| `AnvilNode` | [Foundry](https://getfoundry.sh) on the `PATH`, a POSIX shell and `/tmp`, so it does not run on Windows. From Effect: a `ChildProcessSpawner` and a `FileSystem`, which `NodeServices.layer` of `@effect/platform-node` provides. |
| `MockProvider` | nothing. It runs wherever the library does. |
| `TestWallet` | a `Transport` to a node that serves `anvil_setCode` and has the wallet's accounts unlocked: anvil. A `Scope`, which a test has. |
| `TestSigner` | nothing to be built. To send, the same node as `TestWallet`. |

The entry point is separate from `@ethect/core` so that an application bundle never contains
it.

The [Testing guide](https://ethect.dev/guides/testing/) builds a test file with `AnvilNode` and `TestWallet`: a
send the user approves, then refuses, a contract of your own, and a reorg.
