0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Mochaテスト実行方法

Posted at

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)

0
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?