Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/adrianmcli/ganache-jest-example
🚀 Minimal Solidity contract testing with Ganache and Jest
https://github.com/adrianmcli/ganache-jest-example
ethereum ganache jest smart-contracts solidity
Last synced: 2 days ago
JSON representation
🚀 Minimal Solidity contract testing with Ganache and Jest
- Host: GitHub
- URL: https://github.com/adrianmcli/ganache-jest-example
- Owner: adrianmcli
- Created: 2018-12-31T05:23:44.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2019-04-30T08:06:47.000Z (over 5 years ago)
- Last Synced: 2025-01-03T14:50:55.485Z (7 days ago)
- Topics: ethereum, ganache, jest, smart-contracts, solidity
- Language: JavaScript
- Homepage:
- Size: 398 KB
- Stars: 15
- Watchers: 4
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
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](https://github.com/adrianmcli/ganache-jest-example/tree/truffle-contract).
## 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:
1. The contract ABI, and;
2. 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.