adrianmcli
ganache-jest-example
JavaScript

๐Ÿš€ Minimal Solidity contract testing with Ganache and Jest

Last updated Dec 9, 2025
14
Stars
3
Forks
0
Issues
0
Stars/day
Attention Score
4
Language breakdown
JavaScript 100.0%
โ–ธ Files click to expand
README

Minimal Solidity contract testing with Ganache and Jest

A Truffle project is a great way to start a dapp, but sometimes you want something more lightweight. For example, instead of having to pull-off migrations, maybe you just want to compile a simple smart contract and test right-away.

No worries, the Truffle suite of tools has got you covered! Using Ganache, you can easily spawn a test blockchain for your tests.

Note: This example uses solc to compile the contract. If you prefer to use truffle-contract (makes the JSON artifact a little easier to work with), check out the truffle-contract branch here.

Dependencies

In this example, there are only 4 dependencies!

  • ganache-core โ€” Allows us to spawn a "test blockchain".
  • jest โ€” A testing framework.
  • solc โ€” Compiles our Solidity contract.
  • web3 โ€” Allows us to interact with our contract and do other Ethereum things.

Files

There are only 3 files you need to be concerned about.

SimpleStorage.sol

This is a simple Solidity contract that we will use as our example. It allows you to set and get an integer and nothing more.

compile.js

This file houses a utility function that compiles Solidity contracts and spits out JSON we can work with. Do note that we only need two things from the JSON in this example:

  • The contract ABI, and;
  • The contract bytecode.

test.js

This file houses our tests. Notice that most of the heavy lifting is done in the beforeAll hook, where we:

  • Compile the contract with solc;
  • Spawn a "test blockchain" with ganache-core, and;
  • Deploy our contract with web3.
In the afterAll hook, we simply call stop() on the Provider to clean up after ourselves.
๐Ÿ”— More in this category

ยฉ 2026 GitRepoTrend ยท adrianmcli/ganache-jest-example ยท Updated daily from GitHub