// descript : test 할 suit
describe('test suit', () => {
// it : 테스트 코드 작성
it('test name', () => {
assert.equal(car.park(), 'stopped');
});
});
// before() : descript 실행전 한번 수행
// beforeEach() : 모든 descript 실행전 수행
// after() : descript 실행후 한번 수행
// afterEach() : 모든 descript 실행후 수행
// package.json
{
"name": "inbox",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha"
},
"author": "",
"license": "ISC",
"dependencies": {
"ganache-cli": "^6.1.0",
"mocha": "^5.1.1",
"solc": "^0.4.23",
"web3": "^1.0.0-beta.26"
}
}
// run test
npm run test
Mocha is a powerful and easy to use testing framework for Node.js. Its power comes from its flexibility. With support for all the popular test writing styles (BDD, TDD, Exports, QUnit or Require) and seemingly endless ways to output test results, it's no wonder Mocha has a 94% satisfaction rating among those who have used it.
Unlike other frameworks, it's dead simple to get started with Mocha.To install Mocha globally:
$ npm install -g mocha
To install Mocha as a project dependency:
$ npm install --save-dev mocha
Once Mocha is installed, the only other setup you'll need to do is create a test directory in your project root. That's where Mocha will look for *.js (or, alternatively, *.coffee) files to run.
You can use Node's built-in assert() function in your automated tests, but great tests require more nuanced assertions than assert() allows. If you have a favorite assertion library, go ahead and use it. If you don't, I recommend Chai. Install it the same way we installed Mocha:Globally:
$ npm install -g chai
Or for your project:
$ npm install --save-dev chai
For this example we'll be testing a simple login page to make sure that it rejects bad login attempts, and accepts good login attempts.Definitely take a look at the example test in this repo! It's heavily commented, so hopefully it's easy to read and understand.The basic structure of each test is as follows:
describe() block.describe() block, you can call special methods before(), after(), beforeEach(), and afterEach(). Those methods will run before the whole suite, after the whole suite, before each test case, and after each test case, respectively.it() inside a describe() block.describe() block will block execution of other tests until done() is called.Here's an example of a simple test of a function that adds two numbers together and returns a promise. Before each test we call a reset method on the adder. We also have a cleanUp function at the bottom to where we would put any test cleanup code.
describe("number adder"{
beforeEach(function resetAdder(done){
numberAdder.reset().then(done);
});
it("returns the sum of two positive numbers", function(done){
numberAdder(7,3)
.then(function(result){
assert(result == 10);
})
.then(done);
});
it("returns the sum of a positive and negative number", {
numberAdder(7,-3)
.then(function(result){
assert(result == 4);
})
.then(done);
});
after(function cleanUp(done){
// any test cleanup
done();
})
});
Take a look at login.js and you'll see a very similar structure.
To run your tests, just navigate to the root project folder and run $ mocha. It's that easy!
http://blog.jeonghwan.net/mocha/