This recipe uses the Dynamic JavaScript SDK (
@dynamic-labs-sdk/client + @dynamic-labs-sdk/evm + @dynamic-labs-sdk/react-hooks). See the React Quickstart for the base setup, React Hooks for provider setup, and the React Hooks Catalog for every hook the SDK exports.This recipe is specific to Jest + React Testing Library for testing React components. The mocking patterns below don’t have a vanilla JavaScript equivalent; they exist because tests render React output and drive a
DynamicProvider. If your app also calls JS SDK functions directly outside of React (in a plain module), test those the same way any JavaScript module is tested: mock @dynamic-labs-sdk/client with jest.mock and assert on the calls, without React Testing Library.Prerequisites
- Node.js installed on your machine
- Basic familiarity with Next.js and React Testing Library
- A Dynamic Labs account with an environment ID
Step 1: Create a New Next.js Project
First, create a new Next.js application using the latest version:Step 2: Install the Dynamic JavaScript SDK
Install the required packages.@tanstack/react-query is a required peer dependency of @dynamic-labs-sdk/react-hooks; every hook is built on TanStack Query.
Step 3: Add Dynamic to Your Application
Create the Dynamic client module
The client is a module-level singleton. Create it once so extensions register before any component renders.src/lib/dynamicClient.ts
Configure the Layout
In Next.js App Router, the providers need to live in a client component. Wrap your app inQueryClientProvider (outer) and DynamicProvider (inner). DynamicProvider does not include QueryClientProvider, and any hook used without both will throw.
src/app/dynamic-provider-wrapper.tsx
src/app/layout.tsx:
src/app/layout.tsx
Add a simple page
The JS SDK is headless. For this guide we’ll skip the auth form and just render a hello-world page (see the React Quickstart for a full email-OTP flow, and Authentication screens for the screens your app owns):src/app/page.tsx
Step 4: Configure Jest for Testing
Install Testing Dependencies
Install Jest and React Testing Library along with necessary type definitions:Initialize Jest Configuration
Create a basic Jest configuration file:Configure Jest for Next.js
Create ajest.config.ts file in your project root with the following configuration:
Set Up Jest Custom Matchers
Create ajest.setup.ts file in your project root to configure testing utilities:
Update Jest Configuration
Update yourjest.config.ts to include the setup file:
Step 5: Write Tests for Dynamic Components
Test the Layout Component
Create a__tests__/layout.test.tsx file to test your layout. Mock DynamicProvider so the layout renders without booting the real client, and mock the dynamicClient module so its createDynamicClient / initializeClient calls don’t run in the test environment. QueryClientProvider needs no mock; a real QueryClient is fine in jsdom.
__tests__/layout.test.tsx
Test a Page Component
Create a__tests__/page.test.tsx to test a page that doesn’t yet reference any Dynamic hooks:
__tests__/page.test.tsx
Step 6: Test Components Using Dynamic Hooks
Update Your Page to Use Hooks
Modifysrc/app/page.tsx to use the JS SDK’s React hooks for displaying user information. Every state hook returns the TanStack Query result, so read the value off data:
src/app/page.tsx
Test with Mocked Hooks
Update__tests__/page.test.tsx to mock the hooks and exercise the rendered states. Each mock must return the same shape as the real hook: a TanStack Query result with the value on data, not the value itself:
__tests__/page.test.tsx
Understanding the Mocking Strategy
When testing components that use the Dynamic JavaScript SDK, mock at two layers.1. Mock the client module
createDynamicClient and initializeClient run when src/lib/dynamicClient.ts is imported. In tests, replace the whole module with a stub so neither runs:
2. Mock the React hooks
Replace@dynamic-labs-sdk/react-hooks with jest.fn() for each hook your component uses, then drive their return values per test:
undefined:
- State and query hooks (
useUser,useGetWalletAccounts,useInitStatus,useGetNativeBalance, …) return{ data, isLoading, error, refetch }. Return the value ondata:useUser()gives you{ data: user }, not the user object. - Mutation hooks (
useSendEmailOTP,useVerifyOTP,useSignMessage,useLogout, …) return{ mutate, isPending, data, error, reset }. Mockmutatewith ajest.fn()and assert it was called with the right arguments.
Rendering with the real providers
If a test needs the real hooks rather than mocks, wrap the tree exactly as your app does:QueryClientProvider outside DynamicProvider. Create the QueryClient per test with retries disabled so a failing query fails fast instead of hanging the run.
Put this helper outside __tests__/ so Jest doesn’t treat it as a test suite:
test-utils.tsx
MissingProviderError when DynamicProvider is missing, and No QueryClient set, use QueryClientProvider to set one when QueryClientProvider is missing.
Running Your Tests
Add a test script to yourpackage.json:
Summary
You’ve set up Jest testing for a Next.js app that uses the Dynamic JavaScript SDK. The key takeaways:- Mock
@/lib/dynamicClientsocreateDynamicClientandinitializeClientdon’t run in tests. - Mock
@dynamic-labs-sdk/react-hooksand drive hook return values per test (useInitStatus,useUser,useGetWalletAccounts, etc.), returning the value ondata. - For layout tests, stub
DynamicProviderwith a pass-through component. - The SDK renders no DOM, so assert against your own components, never against Dynamic markup or test IDs.