I am new to nodejs and currently searching for framework to test nodejs and angular based client side application.
I have google and i found that mocha,jasmine etc. various framework available for testing JavaScript.
But can any one explain which framework should i use to test my nodejs and angular based client side application?
I would recommend using the framework Jasmine. It's a BSD style testing framework. If you search for testing on the angular site this article comes up. It suggest using Protractor. I know for a fact that several Angular JS tutorials recommend Jasmine and there is a lot of material on testing for Angular using Jasmine.
Either of these frameworks should work for you.
Mocha can definitely be used to test both client and server side code, and I'm fairly sure Jasmine can too.
For all of my node testing I use a combination of the following:
mocha (https://npmjs.org/package/mocha) for a test runner
chai (https://npmjs.org/package/chai) for an assertion library
supertest (https://npmjs.org/package/supertest) to test apis.
Some of my other team members used to use jasmine but everyone has since moved from jasmine to mocha.
An example test for apis with this configuration would look like this:
var should = require('chai').should();
var request = require('supertest');
describe('your test file description', function(){
var url = 'http://localhost:8080',
describe("POST: your url", function() {
it('include useful description of test', function(done){
request(url)
.post('/endpoint')
.set('Accept', 'application/json')
.set('Accept-Language', 'en_US')
.send({
your : "data"
})
.end(function(err, res) {
if (err) { throw err; }
res.status.should.equal(200);
res.body.should.have.property('id');
done();
});
});
});
});
Related
I'm making a project using create-react-app. There is a configured server and so on. I'm using react-router-dom for routing in my app. There is 'Comments' component. When it starts render itself it goes to my local json file and takes comments from there using ajax. When user clicks 'submit' It sends POST request with form's fields to the same json file. I have code for adding a new object to my json file. It should work when user in '/api/comments' route . This is the code for adding a new object to my json file (requires express):
`app.post('/api/comments', function(req, res) {
fs.readFile(COMMENTS_FILE, function(err, data) {
if (err) {
console.error(err);
process.exit(1);
}
var comments = JSON.parse(data);
var newComment = {
id: Date.now(),
author: req.body.author,
text: req.body.text,
};
comments.push(newComment);
fs.writeFile(COMMENTS_FILE, JSON.stringify(comments, null, 4),
function(err) {
if (err) {
console.error(err);
process.exit(1);
}
res.json(comments);
});
});
});`
But I don't know where I shoud put this code if I'm using 'create-react-app' and it uses it's own configured server (as far I know). Maybe there is a way to change server which 'create-react-app' uses and put there this code to handle this route? Or maybe there is a way to handle this route using 'react-router'?
If I understand your question correctly the code you have posted here is server side code. The app you have made using create-react-app is a front end application and therefore does not have any server side code. You could however host a second server that would expose the api routes you need and then call into that server using a http library like axios.
I'm using a create-react-app and express as my api server. Setting up express to run alongside webpack-dev-server is a supported feature of create-react-app.
I use npm-run-all to fire-up both the client and proxy express api server in my start-up script defined in package.json. Here is what I believe is all that I needed to do:
In my webpack.config.dev.json file I defined a proxy setting in the devServer json block. Specifically:
proxy: { "/api": "http://localhost:3001" },
In my package.json file I configured a start script that uses npm-run-all to fire up both the react-app and express simultaneously.
I use server.js to fire-up express; this is where I store the equivalent of the code you outlined in your question.
I am writing a node application using express JS. Usually in this application we consumes res apis and merge them in one object and send it to angular application.
As a rest client I am using superagent. One of my colleague has written reverse proxy code using http-proxy-middleware.
Using superagent I make a call like below and it does not use http-proxy-middleware as i have mentioned in code.
let request = require("superagent");
request.get("applications-api/1/anotherendpoint")
.set("Content-Type", "application/json")
.set("x-application", applicationId)
.then((response) => {
res.send(response.body);
})
.catch((err) => {
res.json(err)
});
Usually http call should go through middleware since we have that middleware used in express server like but it does not do. In middleware we are using actual endpoint when path matches to "^applications-api", but its not happening. Superagent expecting url which starts from http://endpoint.com/1/anotherendpoint but I want it to be used from proxy-middleware.
Is there anything to do with superagent configuration in order to use http-proxy-middleware ?
Kindly suggest. Any help would be greatly appreciated.
Thanks
I am using node.js express to build simple rest API, I had built a API like
app.get('/sites/:site/:building/:floor',function(req,res){
var building = req.params.building, floor = req.params.floor, site = req.params.site;
console.log('query site ',site,building, floor);
.....
}
when client did the AJAX request in angular.js
$http.get('/sites/london')
.success(function(data) {
}).error(function(data, status, headers, config) {
});
the server doesn't respond to this kind of URL, unless I changed the express API to
app.get('sites/:site',function(){})
app.get('sites/:site/:building',function(){})
app.get('sites/:site/:building/:floor',function(){})
But the way above is too tedious so I wonder if it is possible to build API in one sentence app.get('sites/:site/:building/:floor',function(){})
The following stackoverflow answer should help you out. But to answer your question, the below should work
app.get('sites/:site?/:building?/:floor?',function(){})
I'm trying to test an angular module using jasmine-node, my test is setup as
var fs = require('fs');
var angular = require('../bower_components/angular/angular')
var fsm = require('../filesystemModel');
describe("get files",function(){
it("should get files from the file system",function(){
console.log(fsm);
});
});
Unfortunately, this fails to load angular, and therefore, the filesystemModel breaks because angular is undefined. I think this is because angular is looking for window and document, which I don't think are provided in jasmine-node.
I know node-webkit talks about using chromedriver for testing, but I'm trying to be consistent with tests for modules, some of which are available outside of node-webkit, some which require node-webkit.
Any suggestions on getting jasmine-node testing with node-webkit? Or node testing with karma is another option.
You can use Karma which runs the tests in real browsers.
I need the Meteor server to handle a very simple POST request not coming from the application client. With Express, I'd just do something like app.post('/something', function....
Is there an equivalent in Meteor? If not, how should I set this up, startup an Express server in a is_server context?
Meteor does not yet have the built in functionality to provide a restful API.
You can build basic routing into our application using Backbone, as in the Meteor example provided here: http://meteor.com/examples/todos
You can do something like this:
var AppRouter = Backbone.Router.extend({
routes: {
"": "dashboard",
"home": "dashboard",
"profile": "profile",
},
profile: function () {
Session.set("current_view", "profile")
this.navigate('profile', {trigger: true});
},
Also take a look at: How to expose a RESTful Web Service using Meteor
Alternatively you can serve RESTful APIs with Meteor using the meteor-collectionapi Atmosphere package. See also Is Meteor an option, if i need an additional REST API?.