Testing Web3 Using Synpress

profile
Mike Sell
crashnaut.com

Introduction link

You're comfortable with Playwright or Cypress. You can write E2E tests in your sleep. Then someone asks: "Can you test our dApp?"

You set up Playwright, write a test, click the "Connect Wallet" button... and hit a wall. How do you interact with MetaMask? How do you confirm transactions? How do you test against a blockchain?

Synpress solves this.

What Makes Web3 Testing Different? link

Traditional web apps interact with APIs and databases. Web3 apps interact with:

This creates unique testing challenges:

Enter Synpress link

Synpress is a testing framework built on Playwright that handles all the Web3-specific challenges:

If you know Playwright, you already know 90% of Synpress. It's just Playwright with Web3 superpowers.

format_quote

Note: This guide focuses on Playwright. Synpress also has experimental Cypress support.

Prerequisites: Web3 Concepts You Need link

If you're new to Web3, here are the key concepts you'll encounter:

Wallets link

Blockchains & Networks link

Transactions link

Smart Contracts link

Common DeFi Patterns link

Don't worry if this feels overwhelming - Synpress handles most of the complexity for you!

What Can Synpress Do? link

Here's what Synpress brings to your testing toolkit:

Wallet Automation

Blockchain Testing

Developer Experience

Installation link

If you don't have Playwright set up yet:

Then add Synpress:

Project Structure link

Here's a basic structure for your Synpress tests:

Configuration link

First, let's configure Playwright to work with Synpress. Create or update your playwright.config.ts:

Setting Up Your Wallet Fixture link

Synpress provides built-in fixtures for wallet operations. If you've used Playwright fixtures before, this will feel familiar.

Using Built-in Fixtures link

Synpress comes with built-in fixtures like MetaMask API access:

Creating Custom Fixtures link

You can also extend the test with custom fixtures in tests/fixtures/wallets.ts:

format_quote

Note: Check the official Synpress documentation for the complete fixture API reference.

Basic Test: Wallet Connection link

Let's write our first test. This is the "Hello World" of Web3 testing - connecting a wallet:

What's happening here:

  1. page.click('[data-testid="connect-wallet"]') - Triggers your dApp's wallet connection flow
  2. metamask.connectToDapp() - Synpress handles the MetaMask popup automatically
  3. Address validation - Ensures we get a valid Ethereum address format
  4. Network switching - Tests that your dApp responds to network changes

Intermediate Test: Signature Requests link

A common pattern in Web3 apps is asking users to sign messages. This is used for:

Here's how to test signature requests:

Key points:

Advanced Test: Token Swaps with Transaction Confirmation link

Now let's get into the real stuff - testing a token swap flow. This is where Web3 testing gets complex because you're dealing with:

What's happening in this test:

  1. Token Selection - Choosing which tokens to swap
  2. Amount Input - How much to swap
  3. API Integration - Verifying backend receives the order
  4. Transaction Confirmation - MetaMask popup for gas fees
  5. Error Handling - Testing edge cases like insufficient balance
  6. Rejection Handling - When users cancel transactions

Anvil Integration: Local Blockchain Testing link

Here's where Web3 testing gets interesting. Anvil (part of the Foundry toolkit) is a local Ethereum node you can spin up for testing. Think of it like a local database, but for blockchain:

First, install Foundry (which includes Anvil):

Now let's set up Anvil in your tests:

Update your Playwright config to use this setup:

Now your tests will run against a local fork of mainnet:

Why Anvil + Synpress? link

Coming from traditional E2E testing, Anvil gives you what you're used to:

  1. Speed: Instant transactions (like mocking API responses)
  2. Isolation: Each test can start with a fresh blockchain state
  3. Consistency: Deterministic behavior (no flaky tests from network issues)
  4. Real data: Fork mainnet to test against actual production contracts
  5. No setup overhead: No test accounts, no faucet tokens, no gas fees

It's like having a seeded database for each test run, but for blockchain.

Testing Without Real Wallets: Ethereum Wallet Mock link

Synpress includes a built-in mock wallet that lets you test without MetaMask or any real wallet extension. This is perfect for:

Using the Mock Wallet link

Benefits of Mock Wallet link

When to Use Mock vs Real Wallet link

Multi-Wallet Support: MetaMask and Phantom link

Synpress supports multiple wallet types:

MetaMask (Ethereum) link

Phantom (Solana) link

For Solana dApps, use Phantom:

This wallet-agnostic architecture means Synpress can expand to support more wallets in the future (Coinbase Wallet, Rainbow, etc.).

Advanced: Testing Token Approvals link

A common pattern in DeFi is token approvals. Here's how to test that flow:

Running Your Tests link

Run all tests:

Run tests in UI mode (highly recommended for debugging):

Run a specific test file:

Run tests in parallel (this is where Synpress shines):

Debugging Tips link

  1. Use --headed mode: See what's happening

  2. Use --debug mode: Step through tests

  3. Inspect MetaMask state: Add this to your test

  4. Screenshot before assertion failures:

Troubleshooting: Common Issues & Solutions link

1. MetaMask Popup Not Appearing link

Problem: You click "Connect Wallet" but MetaMask doesn't show up.

Solutions:

2. Tests Failing Due to Network Issues link

Problem: Tests work locally but fail in CI/CD.

Solutions:

3. Transaction Timeouts link

Problem: Transactions take too long and tests timeout.

Solutions:

4. Wallet State Persisting Between Tests link

Problem: Test A affects Test B because wallet state carries over.

Solutions:

5. Running Out of Test ETH link

Problem: Tests fail because wallet has no ETH for gas fees.

Solutions:

6. Flaky Tests Due to Network Congestion link

Problem: Tests pass/fail randomly due to blockchain network issues.

Solutions:

7. Debugging MetaMask State link

Problem: Need to inspect what's happening in MetaMask.

Solutions:

Common Pitfalls link

1. Not waiting for MetaMask popups link

MetaMask operations take time. Always add appropriate waits:

2. Forgetting to handle network switching link

If your test needs a specific network:

3. Not cleaning up state between tests link

If tests are interfering with each other:

4. Running out of test ETH link

When using Anvil, you can set arbitrary balances:

Real-World Example Structure link

Here's how I structure my Web3 test suite:

Environment Variables link

Create a .env.test file:

Load it in your tests:

Important Notes & Gotchas link

Test Isolation with Anvil link

Blockchain state persists between tests. If test A buys 100 tokens, test B starts with those tokens already purchased. Always reset:

Think of it like database transactions in API testing - you need to clean up between tests.

Framework Support link

Wallet Popups link

MetaMask operations can take a moment. Add small waits after triggering wallet interactions:

Conclusion link

If you're an SDET being asked to test Web3 applications, Synpress is your answer. It gives you:

The Learning Curve is Minimal link

Coming from traditional E2E testing, Synpress feels familiar:

What You Can Test Now link

You now have patterns for:

The Future is Web3 link

Web3 testing doesn't have to be mysterious or painful. With Synpress, it's just E2E testing with extra steps. As Web3 becomes mainstream, having these testing skills will be invaluable.

Start with Synpress today - your future self will thank you when Web3 testing becomes the norm, not the exception.

Resources link


Have questions or run into issues? Feel free to reach out or open an issue on the Synpress GitHub repo. The maintainers are responsive and the community is growing.

Feel free to update this blog post on GitHub, thanks in advance!

Share this post

Support me

I appreciate it if you would support me if you have enjoyed this post and found it useful, thank you in advance.

Buy Me a Coffee at ko-fi.com