MochaはNode.jsで動く、JS用のテストフレームワーク。
"Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple and fun. Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases. Hosted on GitHub."
https://mochajs.org/
mochaのインストール
$ npm install mocha --save
ユニットテスト用のコード
test.js
const assert = require('assert');
const ganache = require('ganache-cli');
const Web3 = require('web3');
//Instantiate Web3
const web3 = new Web3(ganache.provider());
class Car {
park(){
return 'stopped';
}
drive(){
return 'vroom';
}
}
describe('Car',()=>{
it('park should return string', ()=>{
//instantiate
const car = new Car();
//assert value
assert.equal(car.park(), 'stopped');
});
});
package.jsonを適宜変更する。test:"mocha"へ。
package.json
{
"name": "inbox-ethereumtest1905",
"version": "1.0.0",
"description": "",
"main": "compile.js",
"directories": {
"test": "test"
},
"dependencies": {
"ganache-cli": "^6.4.3",
"solc": "^0.4.25",
"web3": "^1.0.0-beta.35"
},
"devDependencies": {
"mocha": "^6.1.4"
},
"scripts": {
"test": "mocha"
},
$ npm run test
> inbox-ethereumtest1905@1.0.0 test /ethereum/inbox-ethereumtest1905
> mocha
Car
✓ park should return string
1 passing (17ms)