Karma among with Jasmine, RequireJS and PhantomJS

This time something about JavaScript. Everybody likes JavaScript :P.

I’d like to write quick guide how to set up working tests environment.

First, what we need:

Let get all required things by NPM. Create a package.json with content:

Now run the command:

Great. We have now all required dependencies.

Next thing is to configure Karma. There are two ways of doing this, manually by creating a karma config file or using karma creator (karma init command). I prefer doing it manually. Create a new file and name it karma.conf.js. Here’s my file with comments:

Basically what you need to change is basePath and files / exclude. But that’s not all. We need to create another file which will run asynchronous our test with RequireJS. Let’s place this file in the tests and name it main.js. This is the only file which is included by karma. Content of the file:

Variable window.__karma__.files comes from karma.conf.js files section. We need to filter them as we want to require only tests files. The source files will be declared as dependencies of each test file.

Let’s create a example test file tests/exampleSpec.js and put in it:

We are defining a spec which has source file as a dependency. RequireJS injects it’s to our spec and we can now test it with Jasmine. How cool is that?

Let’s define our example via src/example.js:

The last thing which need to be done is to export the variable which will tell Karma where PhantomJS binary is:

Now let’s run Karma:

Finally you should see something like this:

karma

If you have any problems with PhantomJS – maybe you need to install some libs required by it. Check it’s website for more info. Other solution is to try different browser – Chrome, Firefox etc. But this can’t be done via SSH… 🙂

Any problems, suggestions, feel free to comment.

 

SSH + XDebug + PhpStorm

Hi.

CLI debugging. I never did that. Until today. My tests started to fail and I had no idea why.

So that’s my story why I started to debug tests in the CLI via SSH.

Here’s a list of ingredients:

  • PhpStorm,
  • remote SSH (I’m using Vagrant),
  • PHP + XDebug.

First thing what we need to do is to configure the PhpStorm.

Open your setting and go to Languages & FrameworksPHPDebugDBGp Proxy.

dbgp proxy

  1. Fill the field IDE key with key you choose (I chose PHPSTORM and I recommend it).
  2. In the Host field put the IP which is seen by remote (run echo $SSH_CLIENT on the remote to see it).
  3. Default Port is 9000 and you should not change it.

Next thing to configure is Language & FrameworksPHPServers.

server

  1. Put the server Name.
  2. In the Host field type the IP address, same as on DBGp Proxy.
  3. If you set other Port for DBGp Proxy then change it here.
  4. As we are working on XDebug you don’t have to change the Debugger :).
  5. My remote has different folders structure so I had to set path mapping, probably you will have to do the same.

Ok PhpStorm is ready to go.

Now go to SSH and run this command:

In the first line put your Server Name which you’ve just set. In the second just update the remote_host with the IP address and change the idekey to yours.

The last thing to do is to enable listening for debug connections in the PhpStorm. Just click the button in the top right corner:

enable debug

Ah I forgot, please add some breakpoints! And then you can run your tests / scripts etc.

 

Enjoy 😀

DevDocs – because your current docs sucks

I’m pretty sure that you use many documentations, for frameworks, languages and libraries. You have to add many pages to favourites or google for them. But there is a solution for this. And it’s ultra practical.

Just go to http://devdocs.io.

Quick info:

  • multiple docs,
  • stores all data in the browser storage,
  • support fuzzy search, yaay!
  • dark / light theme,
  • ultra quick.

How to use it? Simply choose docs which you want to have in the left sidebar. Then type something in search bar and instantly you will have the results 🙂 How cool is that?

So scrap all your favourites and start using DevDocs!

PHP built in server and sessions

Hi!

Today I was struggling with Codeception tests. I’m working on Vagrant machine which has Apache2 installed.

Thing is that to view the page on my host machine I’m utilising Apache. But in the acceptance tests I’m using PHP built in server. To be specific https://github.com/tiger-seo/PhpBuiltinServer is used. It allows to run server as tests are running and destroy it at the end. It also supports many options.

Acceptance tests were broken. The Tester could not login. After some investigation I found that the issue was caused by read/write permissions for session. By default my Vagrant tries to store the session files in the /var/lib/php/session. As I opened my project in Apache earlier the permissions were given for it. There are two options to solve this:

  • change the path for session files,
  • change the permissions for the current path.

I choose first option. To be specific I changed it only for server used by tests. The extension allows to specify custom .ini file. So my file tests/php.ini look like this:

Also here’s part of my codeception.yml file:

Now session are working in tests 🙂

 

Cheers!

Bumping Postgres version on Vagrant + CentOS + Chef + postgresql cookbook

Today I was trying to bump the Postgres on my Vagrant to 9.4. The cookbook I’m using: https://supermarket.chef.io/cookbooks/postgresql.

Thing is that the cookbook defines a recipe yum_pgdg_postgresql. So to configure everything just add attributes, here’s example from my cookbook attributes default.rb file:

First line is very important. Current version of cookbook postgresql provides up to 9.2 (source https://github.com/ngs/chef-postgresql/blob/master/attributes/default.rb#L200) version so we have to specify URL to fetch v9.4. As you can see it must specify all required data, postgresql version, os version and os technology used. All available versions for rpm are here http://yum.postgresql.org.

Second lines ensures that recipe yum_pgdg_postgresql will be included.

The last is obvious, version of postgresql to use.

Vagrant and NFS – problems?

Hi.

Recently I heard that using NFS is much faster than default shared folders in the Vagrant with VirtualBox. After some research I started to upgrade my Vagrantfile. Here is snippet how to make you shared partition NFS type:

And that’s all!

But.

Yesterday I had a problem. When I tried to up the vagrant it failed. My recent changes (I changed the username on my OS X) broke something in the NFS configuration. Quick search and found the solution. Seems that I had wrong entries in the file /etc/exports. Here is bash fix:

After that all works like a charm 🙂

The machine started to work very fast. It should be set as a default sharing method…

 

PS. If you are working on the Windows, try a plugin https://github.com/winnfsd/vagrant-winnfsd

 

Cheers, Damian.