include angular in jasmine-node - node.js

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.

Related

Jest testing React component using Electron-Remote in ElectronJS

I'm currently developing an ElectronJS application using React for the UI. In some React components I'm using nodeJS packages via Electron remote, so I import packages from the main process into the renderer process. Looks like this:
main.js
global.moment = moment;
Component.js
const remote = window.require('electron').remote;
const moment = remote.getGlobal('moment');
Everything is working fine until I've started to implement unit testing with Jest.
I've created the first following test case:
it('renders without crashing', () => {
const div = document.createElement('div');
ReactDOM.render(<App />, div);
});
Running this test gives me the following output:
TypeError: window.require is not a function
I have no idea how to solve this problem, maybe someone has an idea how to fix this. Maybe somebody have an idea how to setup unit tests for React in Electron which allows to use nodeJS packages in the React component.
I'm thankful for any help.
Try to add this in src/setupTest.js
window.require = require;
reference: Jest Testing Electron/React Component using window.require

Plan .js server vs webpack vs

I'm really confused. I started learning to use node.js with MEAN stack. Before I used webpack and browserfy without really understanding it.
What confuses me is the following:
Express fires up a server and I can handle the requests
Webpack fires up a server
Browserify fires up a server
simply typing in plain js e.g. var http = require('http'); http.createServer(function (req, res) { ... fires up a server
Well, Webpack and Browserfy (as far as I understand) also bundle js files. How does the logic "under the hood" works and do they bundle everything I code and send it to the client (E.g. my DB login)?
I read this one Webpack vs webpack-dev-server vs webpack-dev-middleware vs webpack-hot-middleware vs etc , which told me webpack uses express under the hood. So maybe express also uses the plan .js server under the hood?
Well, I can go on like this forever. I am a little confused.
Well, what and where are the differences and how do thee apps work (together)?
First of all express use the core API and module of node.js like http module .
express uses the http module to create the server at specific port so
app.listen(3000);
will simple be like this
var http = require('http);
var server = http.createServer() ;
server.listen(3000) ;
server.on('request',function(req,res){
// here express will do all its magic
// and handle the request and response for you under the hood
})
Second things is that webpack and other bundling tools is used for bundling files and assets in the front end not the back end and they can create simple server for listening for changes in your files to give you other features like
+ live reload
+ hot module replacement
but also you can use webpack in the back end to use things like babel-loader or use the hot module replacement feature
so express works for the back end
and webpack use it in the front end
you can create different ports on each server and communicate between them via ajax API like fetch
and that's how actually it should work .
learn more
understanding express.js
understanding express and node fundamentals
webpack.js concepts and documentation

Swagger generate Node.JS Express server code

I have Swagger 2.0 documentation, and I would like to create a Node.JS server stub from the existing Swagger spec.
When I use the Swagger Editor, it has the option to generate Node.js server stubs, but the generated file uses the connect NPM libraries.
I would prefer to use Express, and have the application folder structure of a general Express application. Is there a way to modify the generation of the Node.JS server stub to be compatible with Express?
The easy answer is to change var app = require('connect')(); to var app = require('express')(); in nodejs-server-server/index.js. But it's not optimal since the generated code does not take use of the functionality of Express.
It seems like there will be a express code generator in the next version of swagger-codegen.
You could also use swaggerize-express to do the server stub generation.

Browserify the node.js http server

We created a simple js file, intending to find out if http.createServer works on client browser or not:
var http = require("http")
var server = http.createServer()
server.listen(9024, function () {
console.log("demo server listening on port 9024")
})
and embedded it into a html after browserify.
Display the html in chrome, unfortunately, it always fails on line 2 on http.createServer():
"Uncaught Type Error: undefined is not a function"
We also played around with "serve-browserify" a bit without success.
We have attempted the same thing on both chrome and firefox, and on Linux and Windows. All failed.
Searching through the web, there are quite a few examples for browserify http into the browser.
They all appear to be simple invocation of browserify. However we don't seem to be able to get the same good result.
Your help will be greately appreciated.
You can't use Node.js modules in the browser. All Browserify does is bundling CommonJS modules, it does not allow you to run server side code in the browser.

Testing framework for nodejs and angular based client side application

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();
});
});
});
});

Resources