What is Mocha?
Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simp...
For further actions, you may consider blocking this person and/or reporting abuse
How can i store value from one API call & use it in next ?
You can nest API calls, an example below
I hope it makes sense?
Hi! Thank you For responding, but it is still not working for me, the variable I am storing from the first API call turns out to be null when accessed in 2nd API call.
My Scenario: 1st Post call - Get an access token which I am successfully able to capture , 2nd Post call - Use this access token to view authenticated data.
Yeah, your process seems right, not quite sure why you're getting null. Can I see a screenshot of the code?
This is what i have.. i am not sure what I am doing wrong.
Here I do get the token from Authurl but when I access it for next call .. i don't get the token hence it fails
---- Code --
var authtoken ="";
describe('/Get Welcome Message', () => {
it('Generate Auth token', (done) => {
chai.request(testConfig.baseUrl)
.post(testConfig.authUrl)
.send(testConfig.loginCred)
.set('_format', 'json')
.end((err, res) => {
expect(err).to.be.null;
authToken = res.text.slice(10,-21);
console.log("authToken : "+JSON.stringify(res.text.slice(10,-21)));
expect(res).to.have.status(200);
chai.request(testConfig.baseUrl)
.get(testConfig.cardsDefaultsUrl)
.set('Authorization',authtoken)
.set('Content-Type','application/json')
.set('componentName', 'banner')
.set('_format', 'json')
.end((err, res) => {
console.log("Token : "+authtoken);
expect(err).to.be.null;
expect(res).to.have.status(200);
console.log("Response : "+JSON.stringify(res));
});
done();
});
});
----- end ---
Okay, have you tried logging the error you're getting on the second API call to see what the error is?
Yes ...when i log the err .. its null.
But the response shows a 401 , which is an authentication error. Also I print the auth token which turns out to be null. Hence I believe the token is not getting passed to the 2nd API .
That's wired, let's try making the first request in a before hook, and then use the token where needed.
You can search here and see an implementation for hooks: mochajs.org/
Hi! I have the code working now but the before/before each didn't seem to work as expected. I realized I was making a mistake while parsing the JSON response.
Thanks a lot for helping.
Great tutorial! What icons are you using on the screenshot of the file structure?
Thanks, John. I installed a vs code extension called Material Icon Theme.
Thanks! I will give it a try.
Hi!
I have an issue that I face with my tests, when I run the tests it doesn't exit after execution hence I added --exit to my mocha command & it worked as expected.
But now due to a requirement, my code gets picked up by jest for the execution of unit tests + my API test hence run by the command "jest --coverage" and everything gets stuck because the mocha code that gets picked up for execution doesn't exit.
Please let me know if you have any suggestions on the same.
Thanks for this, i really love the analogy you used in explaining it
I'm glad you enjoyed it.
Hi! Ez.
Do you provide test to check if an HTML element is DOM injected after a GET ?
It would be great to read you about that ;)