Automated API Testing using node-fetch and mocha

preview_player
Показать описание
We review briefly the three tier web architecture. Then we discuss testing the API or middle layer directly, without the browser. We write an automated API test using nodejs, mocha, and the node-fetch library.

To create an empty npm project, we click the File, New Window option in VS Code.
Then we click File, New Folder
We choose where we want the folder to live, then we name it
Now that we have our new project folder created, with click View, Terminal
In the terminal we initialize an empty NodeJS project with the command: npm init
We hit enter through all the prompts
To see what we have so far, run the command: npm run test
Notice, there is nothing really there?
Let's install mocha so we can start writing some tests: npm install mocha --save
"scripts":{
"test":"mocha"
}
Run the command again and see what happens: npm run test
What does it say? What folder is it looking for? The test folder.
Right-click in the Explorer in VS Code and click new folder, name it test.
Now we have a test folder, but it's empty!
import assert from 'assert';
This is part of the mocha library you installed.

Now run the test, what do you think will happen: npm run test
Why did it fail? What do we need to change?

We need to add the actual login call to the endpoint, and replace "" with a valid token. Then the length test should pass.

Finish the code by following the video, then be sure to push the code to your Github Repository.
Рекомендации по теме