jasmine-node pass ressults - node.js

i´m trying to check several requirements on a html body, received by the node-module request with jasmine-node.
The normal case would be to write something like this:
describe("check website body", function() {
it("should contain 1st requirement", function(done) {
request("url...", function(error, response, body) {
//check here the body for the first requirement
done();
});
});
it("should contain 2nd requirement", function(done) {
request("url...", function(error, response, body) {
//check here the body for the 2nd requirement
});
done();
});
});
What i would like to have is to call the website directly after the describe line and than pass body to the specific tests.
UPDDATE: To clarify: My intention is to only call the url once for my tests, not twice.
could someone can give me a hint how to do this?
Thanks and kind regards

You already had the right place in your example, just missing the code!
request("url...", function(error, response, body) {
//check here the body for the first requirement
expect(body).toContain('something');
done();
});

Related

stopping postman after request is done

I am trying to create a Weather API using node. In my controller file, I have this code which is run for the /check route.
controller.js:
//Check Weather
exports.check = (req, res) => {
UserModel.check(req.body.city)
};
model.js:
//Check Weather
function getData(city) {
url = "something";
request(url, function (err, response, body) {
if(err){
console.log('error:', error);
} else {
console.log('body:', body);
}
});
}
exports.check = (city) => {
city = city.toLowerCase();
let values = getData(city);
console.log(city);
return(values);
};
route:
app.post('/check', [
UsersController.check
]);
When I run this, it functions properly and the correct thing is logged in the console. However, after I send a request in Postman and the console.log shows up, Postman seems to be hung up as seen in this pic. Is there anyway I can make it so that Postman stops sending the request after return or console.log?
Postman is waiting for a response from the server. Your code is not currently sending any response, so postman seems 'hung up' as it is waiting. Try changing the line saying UserModel.check(req.body.city) to say res.send(UserModel.check(req.body.city)) so it will send the data returned from your UserModel.check function back as the response. Alternatively, if you don't want to send back the returned value, you could just add res.send(PutWhateverYouWantSentHere) after the function call.

how to send fetched data node js

Hello i have a request which fetch some json data from third party API:
request({
url: 'https://api.steampowered.com/IEconService/GetTradeOffers/v1/?key=MYAPIKEY&get_sent_offers=1&active_only=1&format=json',
json: true
}, (err, responser, body, undefined) => {
tradeItems = JSON.stringify(body.response['trade_offers_sent'][0].items_to_give);
});
How can i send tradeItems fetched data to offer.addTheirItems value?
client.on('webSession', function(sessionID, cookies) {
manager.setCookies(cookies, function(err) {
if (err) {
console.log(err);
process.exit(1);
return;
}
let offer = manager.createOffer("https://steamcommunity.com/tradeoffer/new/?partner=123456789&token=1234");
offer.addTheirItems();
offer.setMessage("");
offer.send(function(err, status) {
if (err) {
console.log(err);
return;
}
First, that's are javascript's async issue.
The solution is in many ways.
change the request function to async function. and make tradeItems variable to outside from request function.
I recommend request-promise module
move below codes to in upper code's callback function.
This is a simple answer because your sample code is separated into two parts.

How to mock seneca calls in Node.js using jasmine-node?

I am using express module and below is the code of app.js
app.post('/test_url', function(request, response){
seneca.client({type: 'http',port: '3000',host: 'localhost',protocol: 'http'}).act({role: 'sample_role', cmd: 'save',firstname: request.params.firstname}, function (err, result) {
console.log("Inside Seneca act");
response.json(result);
})
});
Below is the test file where I am writing the test case for above code.
describe("POST /test_url/:firstname", function() {
it("should return status code 200", function(done) {
<b>//here I want to mock the call for seneca.client so that I can test if the call has been made with the required parameters.</b>
<b>//Also I would like to use the above mock object to further mock the call for act so that I can check if the act method has been called with the required parameters.'</b>
//Main purpose behind doing so is that I do not want the seneca methods to get actually called, and only want to test if the call has been made.
request.post("http://localhost:3000/test_url/sara", function(error, response, body) {
//some verification method on the mock object so as to confirm that both the calls i.e 'seneca.client' and 'seneca.client().act' have been called with the appropriate parameters
expect(body).toContain("success");
done();
});
});
});
I tried to mock the seneca calls using jasmine spy and sinon but still the call was actually being going to the method and the the callback function was also invoked resulting in the console.log("Inside Seneca act"); being called, which is not what I expect.
describe("POST /test_url/:firstname", function() {
it("should return status code 200", function(done) {
var senecaCall = sinon.stub(seneca, 'client');
//or spyOn(seneca, "client");
request.post("http://localhost:3000/test_url/sara", function(error, response, body) {
expect(body).toContain("success");
done();
});
});
});

Is it possible to add information to the error message in supertest

I'm utilizing supertest with mocha to test a nodejs express app. Everything is fine expect I'd like a little more descriptive error messages. That's not to say the messages are currently bad, they're not. I'd just like a little more information.
For example:
it('should successfully post my data and return a valid JSON', function(done) {
.post(url)
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.send(data)
.expect(201, resultBody)
.end(function(err, res) {
if (err) return done(err);
done();
});
});
If an error should occur, such that the resultBody doesn't match the actual result, it'll print out a nice + expected - actual message. But I would also like to see other information (maybe the authentication or the header).
My current solution is to do the following:
.end(function(err, res) {
if (err) {
console.log(res.headers);
console.log(res.statusCode);
console.log(res.body);
return done(err);
}
done();
});
But the console.log messages appear inline with the "it" messages, before the passing/pending/failing messages and not with the actual error.
Is it possible, without too much extra effort, to put extra information with the error message?
You can fix it using the Language Chains of Chai in expect and should adding Assertion Styles:
var foo = false;
expect(foo, 'foo must be true').to.be.true;
//AssertionError: foo must be true: expected false to be true

How can I test if my Express app's response contains certain HTML tags / text?

I'm running an Express Node server, and using Mocha and Supertest to test my routes.
I would like to be able to test for the existence of certain text in the response for one of my Express routes, as so:
it('should display form text input', function(done) {
request(app)
.get('/')
.end(function (err, res) {
if (err) {
return done(err);
}
res.text.should.include('class="text-input-wname');
done();
});
});
However, when I run this test, I get the following error:
Uncaught TypeError: undefined is not a function
The res.text prints out fine to the console. I know that should.include() is meant to check for the existence of an element in an array, so presumed this might not work.
But what is the correct way to parse the response body to check for the existence of some text?
Have you tried it with the match-assertion, like:
res.text.should.match(/class="text-input-wname/)

Resources