What is 1.mocha?
Mocha is a popular amount of JavaScript testing framework.
Basic usage of 2.mocha
1. Using NPM to install the Mocha package, I did not select a global installation. Just add mocha dependencies in the Mocha folder of Package.json.
Such as
{ "name": "Mocha-test", "version": "0.0.1", "dependencies": { "KOA": "2.4.1", "SuperTest": "3.0.0", "Mocha": "3.0.2" }, " Scripts ": { " test ":" Mocha " }, " Exclude ": [ " Node_modules " ], " Keywords ": [ " test ", " Mocha " ]}
Test notation for 2.mocha
Const ASSERT = require (' Assert '); Const SUM= require ('.. /hello ');d Escribe (' Test Hello.js ', () ={describe (' Test sum function ', () = ={before (function() {Console.log (' Before: '); }); After (function() {Console.log (' After. '); }); Beforeeach (function() {Console.log (' Beforeeach: '); }); Aftereach (function() {Console.log (' Aftereach. '); }); It (' Sum () should return 0 ', () ={assert.strictequal (sum),0); }) It (' Sum (1) should return 1 ', () ={assert.strictequal (sum (1), 1); }) It (' Sum ' should return 3 ', () ={assert.strictequal (sum (), 3); }) It (' Sum (should) return 6 ', () ={assert.strictequal (sum ((6),); }) })})
First Mocha must have an assertion module. The assert in the figure is from node. js. Simply assert a class that does some comparison operation on the incoming data.
The two parameters of the describe function are the description of the test and the code area of the test. Describe can nest describe functions. You can better describe the test logic on the hierarchy.
It function is the function that really runs the test. The first parameter is still the description of the test, and the second parameter is a function. What you put in there is what you want to test.
These are the examples in the course of Liu Xuefeng's teacher. Specific source code and tutorials can go to see. Just do a study note here. A summary of yourself.
3.mocha How to start a test
There are three ways to Liaoche Teacher's tutorial. I was actually trying. The first method is of no use.
The first way I used it was to add "script" to the Package.json file: {"Test": "Mocha"}. The command line then switches to the directory in the test file to enter NPM test.
The second method is to add Mocha dependencies on the webstorm. (Just because I used the webstorm, Liaoche teacher used the Vscode)
Just put it on the map.
You can test directly in the IDE with one click. Very convenient
3. Some questions and answers that arise
This code will be in the Liaoche teacher's tutorial
Let expression = await fs.readfile ('./data.txt ', ' utf-8 ');
In fact, if you write like this, the editor will make a direct error. Requires you to join the callback function. And it's not going to work so well to write an await.
Because Fs.readfile is an asynchronous function. No value returned.
An await must be followed by a Promise object.
So we have to rewrite this function.
Let ReadFile = (Path,format) +{ returnnew Promise (resolve,reject) +{ == {if(err) reject (err); Resolve (data);} ) }}
Put the complete code
varReadFile =function(Filename,format) {return NewPromise (Resolve, reject) ={fs.readfile (Filename,format, (err,data)={ if(Err) reject (err); Resolve (data); })})};const FS= Require (' FS '); Module.exports= Async () ={Let expression= await readFile (__dirname+ '/data.txt ', ' utf-8 '); LET FN=NewFunction (' return ' +expression); Let R=fn (); Console.log (' Calculate: ${expression}=${r} '); returnR;}
And there's another problem. Using a relative path in the first parameter path of Fs.readfile will cause an error to be read. is because the default current path in this parameter is incorrect. It is best to use absolute paths. Or the form of __dirname+ ' ... '.
4. Finally
Because asynchronous and HTTP tests are relatively brief in the tutorial.
So there's not much worth writing about.
Because of the new contact with these things. New framework.
Many APIs are not known. It's not going to work. Do not understand the design idea of the frame.
So you can only wait to use these frameworks later to do some concrete examples. There are more discoveries and insights.
This is the first place to be here today.
node. JS Learning Note 1-mocha