How to test the Node js application with mocha-phantomjs - node.js

I need to test my Node js apllication with mocha-phantomjs.I have tried the below code to test the app but i'm getting error as 'ReferenceError: Can't find variable: require'.How to resolve this.
test.html
<html>
<head>
<title> Tests </title>
<link rel="stylesheet" href="./node_modules/mocha/mocha.css" />
</head>
<body>
<div id="mocha"></div>
<script src="../node_modules/mocha/mocha.js"></script>
<script src="../node_modules/should/lib/should.js"></script>
<script>
mocha.ui('bdd');
mocha.reporter('html');
</script>
<script src="test.js"></script>
<script>
if (window.mochaPhantomJS) { mochaPhantomJS.run(); }
else { mocha.run(); }
</script>
</body>
</html>
test.js
var module=require('../lib/requiredModule');
var should = require('chai').should();
describe('Testing',function(){
it('Save Data',function(){
module.save(content,function(err,res){
should.not.exist(err);
});
});
});
While running the html file as mocha-phantomjs test/test.html i'm getting error as
ReferenceError: Can't find variable: require

So, I think your problem is that running your tests via the test runner basically runs them as if they were client side. Thus, it will not be able to find your native node modules (like require). You can try loading require.js in directly. Or, just use
<script src="../node_modules/chai/chai.js"></script>
<script>
mocha.ui('bdd');
mocha.reporter('html');
var should = chai.should; // This will give you access to chai should.
</script>
So you will not need to require anything that you src to. Again, think of this like you are doing everything client side.

Take a look at browserify, which enables you to automatically include npm libraries: https://github.com/substack/node-browserify
Also recommended, are connect-browserify for auto-reloading in development https://github.com/andreypopp/connect-browserify and asset-rack https://github.com/techpines/asset-rack for automatic bundling in production.

Related

NodeJs app on Heroku displays blank page after deployment

I have a nodejs app (NodeJs+ React+ MongoDb) deployed on heroku . I am connecting to mongoDb database using mLab , when i run npm start from heroku console it says App listening on PORT: 47147 and mongoose connection successfull . But when i take the app (https://sheltered-spire-71661.herokuapp.com) It displays nothing . What may be the reason ? Any help is appreciated .
Adding the screenshot of the heroku console
and log
UPDATE
FYI i run the Heroku app in local , and it works perfect without any problem . I don't know the problem when deploying in Heroku . Screenshot below
UPDATE 2
index.html
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>LMS</title>
<link rel="shortcut icon" href="/assets/images/favicon.ico" />
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.8/css/materialize.min.css">
<link rel="stylesheet" type="text/css" href="/assets/style.css">
<link rel="stylesheet" href="https://unpkg.com/react-tabs#2/style/react-tabs.css">
<link href="https://cdn.jsdelivr.net/npm/react-big-calendar#0.19.0/lib/css/react-big-calendar.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-2.2.4.js" integrity="sha256-iT6Q9iMJYuQiMWNd9lDyBUStIq/8PuOW33aOqmvFpqI=" crossorigin="anonymous"></script>
<script src="https://use.fontawesome.com/8ff98e4aea.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.8/js/materialize.min.js"></script>
<script src="/assets/javascript.js"></script>
</head>
<body>
<div id="app"></div>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.7.7/babel.min.js"></script>
<script src="/bundle.js"></script>
</body>
</html>
When i have index.html like this in Heroku , its throwing me an error in console like
Uncaught SyntaxError: Unexpected token <
I thought its failing to understand the index.html file and i changed the bundle loader script like <script type="text/babel" src="/bundle.js"></script>
The error is gone but displaying blank page . Further i thought its not properly serving the static assets from this SO question Deploy to heroku : Uncaught SyntaxError: Unexpected token < . Then i taken view page source and checked files from the browser
and
<script src="/assets/javascript.js"></script>
and it was showing the code (https://my.herokuapp.com/assets/style.css and https://my.herokuapp.com/assets/javascript.js is working) . But when i access /bundle.js its redirecting back to the heroku app , unlike in local its showing the bundle js code . Express is serving static files like
//Landing
app.get("/", autoRedirect, function(req, res){
res.sendFile(path.resolve(__dirname, "public", "index.html"));
});
//Public files <this needs to stay right below app.get("/")!!!!
app.use(express.static(__dirname + "/public"))
Is it a problem of Heroku doesnt understand the bundle.js file ? I went to the Heroku app console and built the app once more with npm run build and the bundle.js is generated successfully , that also didn't made any changes to the app loader .

Serving React build with Express shows nothing

I have this project structure:
app
server.js
frontend
build
index.html
build directory is the result after npm run build in my React app (created with create-react-app). There are more files but I omit them for the example.
I want to serve the React app as static files, so in server.js, I have this:
app.use(express.static(path.resolve(__dirname, 'frontend/build')));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'frontend/build', 'index.html'));
});
app.listen(9001);
I navigate to http://localhost:9001 in my browser and there are no errors but the screen is blank. React app is not shown.
If I run the React app (from inside frontend directory) with npm start, it works properly (in default webpack port 3000).
This is the build/index.html file (pretty printed):
<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
<meta name="theme-color" content="#000000"><link rel="manifest" href="/manifest.json">
<link rel="shortcut icon" href="/favicon.ico">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js" integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous"/>
<title>React App</title><link href="/static/css/main.65027555.css" rel="stylesheet"></head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<script type="text/javascript" src="/static/js/main.a4d4d402.js"></script>
</body>
</html>
If I navigate to http://localhost:9001/static/js/main.a4d4d402.js, I can see the javascript file.
I found the error. There was an error in the bootstrap imports in index.html. I was closing a tag twice. In my question looks well because I have pretty printed by hand and maybe I have corrected it somehow.

Grunt: How to correctly setup .html file for mocha testing?

I am new to grunt and tests and currently I have setup like this:
Grunt running tasks after which I have build folder where I have my compiled index.html and all his resources (css, js, images)
Now I want to run tests using grunt-mocha-phantomjs and it needs additions to my index.html like:
<link href="../node_modules/mocha/mocha.css" media="screen" rel="stylesheet" type="text/css" />
Inside head tag and:
<div id="mocha"></div>
<script src="../node_modules/mocha/mocha.js" type="text/javascript" charset="utf-8"></script>
<script src="../node_modules/chai/chai.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
mocha.ui('bdd');
expect = chai.expect;
</script>
<script src="test.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
mocha.run();
</script>
Inside body tag.
I want to be able to open that modified index.html in web-browser too. (I have setup apache for it and example test works great both in grunt and web-browser)
So I am planning using task grunt-contrib-copy to copy all files from my build directory to test directory and add those lines to index.html and then launch mocha_phantomjs task.
Is this correct way to do it?
Which NpmTask can do it?
Not sure about "Is this correct way to do it?" part but I solved it.
I used grunt-html-build and its Removing parts feature.
Removing phantom's code for build folder, and keep it for testing folder.
It works great.

how to use adapter.js of webRTC-adapter?

I am writing a WebRTC application and have the following problem:
I want to use the adapter.js library.
I have the following index.html:
<html>
<header>
<script src='../out/adapter.js'></script>
<script src='../out/main.js'></script>
</header>
<body>
<video id="localVideo" width='500' autoplay></video>
</body>
</html>
and my main.js looks like:
var adapter=require('webrtc-adapter');
var localVideo=document.querySelector('video#localVideo');
navigator.getUserMedia(media_constraints, handleUserMedia, handleUserMediaError);
function handleUserMedia(stream) {
localStream = stream;
adapter.attachMediaStream(localVideo, stream);
console.log('Adding local stream.');
}
but my browser logs the error: Uncaught ReferenceError: require is not defined
require is used (and defined) in Node.js environments to load modules. (Not exclusively, for more information check here).
If you downloaded an adapter.js version from here and include it the way you do (via script tags), you can simple delete the require(...) line and you should be good to go.
Edit: added an example
console.log(adapter.browserDetails.browser);
<script src="http://webrtc.github.io/adapter/adapter-latest.js"></script>
if you have checked installing adapter js?
check
npm webrtc-adapter --version
if not showing up in node, install it to your app
npm install --save webrtc-adapter

Getting TypeError: App.Router is undefined after building ember.js

Posting here as requested original post can be found here
Hello, I would like to build ember.js using Ubuntu 13.
I have cloned the official Github project, cd into the project and as described in the readme file I did:
bundle install
rake dist
no error is shown on screen and as a result I get a directory shown in the image
I would like to use ember and ember-data, so I include
ember.js
ember-data-deps.js
files in my test project.
The problem is that I am getting a TypeError: App.Router is undefined
I am using this at my client.js file to init ember
this.App = Ember.Application.create();
App.Router.map(function() {
this.route('contributors');
this.route('contributor', {path: '/contributors/:contributor_id'});
});
Am I doing something wrong in the build process?
Should I include some other js files in my project?
Thank you in advanced.
The TypeError: App.Router is undefined error is because ember.js is not loaded correctly or in the correct order.
To get ember-data (that is separate from ember.js) you have to clone this repo (https://github.com/emberjs/data) and follow the build instructions in the readme file, it's straight forward, and once you have the dist directory from the ember-data build process get the file ember-data.js development version or ember-data.min.js for production (well, production... ember-data is still considered unstable for production environments).
here is a simple ember project setup using ember-data:
index.html
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>ember app</title>
</head>
<body>
<script type="text/x-handlebars">
hello world!
</script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript" src="http://builds.emberjs.com.s3.amazonaws.com/handlebars-1.0.0-rc.3.js"></script>
<script type="text/javascript" src="http://builds.emberjs.com.s3.amazonaws.com/ember-latest.js"></script>
<script type="text/javascript" src="http://builds.emberjs.com.s3.amazonaws.com/ember-data-latest.js"></script>
<script type="text/javascript" src="app.js"></script>
</body>
</html>
app.js
var App = Ember.Application.create({
ready: function () {
console.log("app started...");
}
});
hope it helps

Resources