We need a way to test contracts ⇒ we will set up a custom Mocha test runner that can somehow test Solidity code !

We need a way to deploy our contracts to public networks ⇒ we will set up a deploy script to compile + deploy our contract

boilerplate-inbox.zip

run npm install on this folder to install all dependencies

Dans un fichier compile.js

const path = require("path");
const fs = require("fs");
const solc = require("solc");

const inboxPath = path.resolve(__dirname, 'contracts', 'Inbox.sol');
const source = fs.readFileSync(inboxPath, 'utf8');

// console.log(solc.compile(source, 1));
// module.exports = solc.compile(source, 1);
module.exports = solc.compile(source, 1).contracts[':Inbox'];

dans package-json

"scripts": {
    "test": "mocha",
    "compile":"compile.js"
  },

il nous reste plus qu’a taper dans la console

node compile

pour retrouver ce genre de résultat

Untitled

Nous allons ensuite creer un dossier ‘test’ et creer le fichier inbox.test.js à l’interieur

const assert = require("assert");
const ganache = require("ganache-cli");
const Web3 = require('web3');

<aside> 💡 Versionning de WEB3 !!

v0.x.x = “primitive” interface , only callbacks for async code

v1.x.x = Support for promises + async / await

</aside>

Web3 providers

providers are a communication layer to connect web3 and networks / test networks (like Ganache). Provider is the Phone , and web3 and ganache are two people trying to communicate through the telephone. web3 is an instance of Web3

const ganache = require("ganache-cli");
const Web3 = require('web3');
const web3 = new Web3(ganache.provider());

Mocha is a Test Running Server, very general purpose testing framework

@Mocha functions 
it
//	Run a test and make an assertion
describe
	// Groups together 'it' functions
beforeEach
	// Execute some general setup code