Test driven development in Javascript

Howdy Friends,
Hope you guys are doing well. I always had question on how to test JavaScript and wanted to learn about it, after learning how to do JS test successfully I created this small example on how to test your JavaScript. Hope you guys like it.
In simple terms it is nothing but writing tests first corresponding to what you need; then writing code to satisfy that test.
Let’s understand how to do this.
A complete code of this tutorial is here.
Create a folder and name it with anything you want to.
Open up your terminal, create a new directory, navigate into that directory and give the below command.
npm init
Fill up the questions as desired and then open up the project in your editor.
Let’s write the tests first
We will be using the mocha, chai framework to write our tests.
Go ahead and install it using the below command.
npm install mocha chai –save-dev
(I would strongly recommend if you install this globally. To install globally run this command
npm install -g mocha chai –save-dev
)
Now create a file test.js in the same directory and add the below code.
var Name = require(‘./index.js’);
var expect = require(‘chai’).expect;

describe(‘Firsttest’, function(){
it(‘It should return my name’, function(){
var name = new Name();
var result = name.sayname();
expect(result).to.equal(‘Adesh Shah);
})
})

If you had written tests before then this would look familiar. It’s nothing but a simple test case which expects the result to be equal to Adesh Shah. As you could see Adesh Shah is the string returned by sayname prototype.
Now let’s go ahead and create a new index.js file in your project directory and add the below code.
function myname() {

}
myname.prototype.sayname = function() {
return ‘Adesh Shah’;
}
module.exports = myname;

This does nothing but return a string. As you could see there is a function myname with a prototype sayname which returns the string. This would pass the tests which we wrote above in our test.js file.

Now open up ur terminal, navigate to this project directory and simply enter the below command

mocha

Horayy!! You will see screen like this

3

Congratulations!!! It simply says that our test has passed successfully.
To create a more easier way to run tests open up package.json and in the scripts, test: add mocha.
Your package.json should look as shown below.
{
“name”: “LearnTest”,
“version”: “1.0.0”,
“description”: “learn testing”,
“main”: “index.js”,
“scripts”: {
“test”: “mocha”
},
“author”: “Adesh”,
“license”: “”,
“devDependencies”: {
“chai”: “^3.5.0”,
“mocha”: “^2.4.5”
}
}
Now just run this command this will would get the tests to run
npm test
Command line is done, but we have to try this in browser too right so let’s begin with testing in browser.
For that we were going to use zuul. Zuul is a program that is used to gate the source code repository of a project so that changes are only merged if they pass tests. The main component of Zuul is the scheduler.
– Google.
To install zuul simply run the below command.
npm install zuul –save-dev
Then create a new file called .zuul.yml in your project directory and add the below code.
ui: mocha-bdd
browsers:
– name: chrome
version: latest
– name: firefox
version: latest
Now open up package.json and modify the test script as shown below.
“test”: “mocha && zuul –local 1991 –test.js”
So, now you package file will look like this.
{
“name”: “LearnTest”,
“version”: “1.0.0”,
“description”: “learn testing”,
“main”: “index.js”,
“scripts”: {
“test”: “mocha && zuul –local 1991 –test.js”
},
“author”: “Adesh”,
“license”: “”,
“devDependencies”: {
“chai”: “^3.5.0”,
“mocha”: “^2.4.5”
}
}
Save your files and now run this command in Terminal
npm test
You should screen as below

4

Copy the url and open it in browser :
You should screen like this on your browser

5
Whoooaa!! We did it. We have finished testing our application on the browser window as well.
Hope you guys liked it. Cheers!!

Author -Adesh shah

Error Handling with PHP

Error Handling with PHP

Hi Friends,
Hope you are all well. I have been always struggling with PHP session variables or any data when I am passing from one PHP page to other PHP page, so I found this method which is very useful to check if that value is passing or not when you enter in web application.
In simple terms it is nothing but writing logOutput function.
A big thanks to Professor Sebastian Lenczewski who taught me this testing concept.
Let’s understand how to do this.
A complete code can be found here.
To illustrate this methodology let’s consider a simple PHP form application which takes two input field.
Create a new folder and name it anything, I have name mine Error_Handling. Make sure you store it in right place as we are going to right server – side code that is PHP. There are different local server available depending on which OS you are using to name some MAMP, WAMP, LAMP, XAMPP.
Let’s start by creating two php file one with index.php and second with test.php. In the index.php we are going to write a simple Form code I have started my session at a top to store my all input field data in session. Go ahead and copy the below code in index.php .


Name:
email:


Before we move to test.php file, let’s understand the main part of this blog which log.php file where I am creating one .txt document.
Create a new file in the PHP project directory and name it log.php and copy the below code.
” . $text . “\n”;
//$logFile = fopen(“serverLog.txt”, “w”) or die(“Unable to open file!”);
$logFile = fopen(“serverLog.txt”, “a”) or die(“Unable to open file!”);
fwrite($logFile, $logFormatedText);
fclose($logFile);
}
?>
Great, now if you see in the above code all I have done is created a function name logOutput which creates a new serverLog.txt file in the project whenever I call logOutput function in test.php.
Now that we have done this lets go ahead and move to test.php code. Copy this code in test.php file.

In the above code I have used require(log.php) . This will include the log.php in test.php file without wasting lines in test.php. Again it’s good method to use include or require.
If you had written if(isset) before then this would look familiar. It’s nothing but a simple test case which expects the result when i enter Name and Email when I execute index.php.
The main thing to look for here is my logOutput function. It’s pretty straight forward I am just displaying my session variables in text file.
Now we have our backend ready lets go ahead and run this program.

1

I have run my index.php file and entered Name and Email. After you click on Submit test.php file will run, but it will be blank file, so just go ahead check your folder there will be serverLog.txt file created which will show us the following result.

2
Boom! Here it goes it shows my Name and Email in serverLog.txt file.
So whenever you are using with passing data from one page to other page with PHP this log file will help you debug the error of passing the data.
Hope this helped you guys.
Thank you
Cheers!

Author- Adesh Shah