728x90
Testing With Mocha
Mocha is a testing framework used to make test cases. Running tests consistently ensures newly added features are well integrated with previous ones.
You can test such as :
- Creating records
- Reading records
- Updating records
- Deleting records
Install Mocha like below
npm install mocha --save
// Installing mocha is not required for production setting.
npm install mocha --save-dev
Assert
Assert is a built-in module in Node.js. It evaluates a value parameter and if it is not true, throw error.
assert(value, message) // message is optional
Mocha configuration with Package.json
Configurate Mocha like below in Package.json to use Mocha with command npm test.
"scripts": {
"test": "node_modules/.bin/mocha $(find your/foler/directory -name '*.js') --recursive -w",
"start": "nodemon"
},
Handling Asynchronous Request With Mocha
Saving model into database is an asynchronous request. Deliver 'done' function parameter provided by Node.js and call it after the asynchronous request is done.
myModel.save()
.then(function(done) {
// do what is needed
done(); // finish the asynchronous request
});
When the done parameter is not properly delivered
When delivered
728x90
'창고 3 > [Dev] My Readme' 카테고리의 다른 글
몽고 DB 기초 6 (0) | 2022.01.11 |
---|---|
몽고 DB 기초 5 (0) | 2022.01.10 |
몽고 DB 기초 3 (0) | 2022.01.06 |
몽고 DB 기초 2 (0) | 2022.01.05 |
몽고 DB 기초 1 (0) | 2022.01.04 |
댓글