Test all your wagmi-dependant components without having to modify any existing file #2072
lefuncq
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi there,
It's been a few days since I tried coming up with a solution to mock Wagmi successfully.
I tried to find a working solution because most required setting up a local chain which is not ideal when you want entirely isolated test environments with flexibility, control, and deterministic behaviors.
So here's my workaround. I hope this helps!
I currently use
wagmi/core
with Next.js, but you can quickly adapt it to fit any framework and/or with the regularwagmi
package. It primarily relies on Webpack's aliases.Since I use Next.js for me it was
next.config.js
, below is my configuration.As you can see if
process.env.NODE_ENV
equalstest
(which I set manually within my package.json), then it will resolve all wagmi imports tomockWagmi.js
.Here I use
window
object to store callbacks from watch methods and the connection state. This is pretty convenient if you run e2e integrations tests, but if you run unit tests, you can useprocess.env
to store your states. As a matter of fact, any globally accessible variable that will be shared between your test files and your component can do the job.And you're all set!
Your app will be calling these mock implementations, and you won't depend on any local or remote chains while being able to simulate different scenarios!
Still, my file is short, and yours might become bigger depending on your testing needs and what you use within the
wagmi
package.Beta Was this translation helpful? Give feedback.
All reactions