Artisan Développeur

Qualité, amélioration continue, agilité.

Mochajs locally in your symfony project

How to install node and mocha locally to unit test you javascript code

I will install packages in the require-dev section of my composer.json file, because in this case, I only use nodejs and mocha in the dev, preprod and test environments and they use the requierdev.
I don’t want to install that on my production environment.

To do that, I use two composer packages:





Here the installation

You have to add this to composer.json file:

"require": {
    "mouf/nodejs-installer": "^1.0",
    "koala-framework/composer-extra-assets": "~1.1"
"extra": {
    "require-dev-npm": {
        "mocha": "*"

To use mocha locally you have to add one file to the bin directory of your symfony project

In the script section, both in « post_install_cmd » and « post_update_cmd »

"touch bin/mocha && chmod +x bin/mocha && echo '#!/bin/bash \n DIR=$( cd \"$( dirname \"${BASH_SOURCE[0]}\" )\" && pwd ) \n export PATH=$DIR/../vendor/nodejs/nodejs//bin:$PATH \n ../node_modules/.bin/mocha \"$@\"' > bin/mocha"


    "scripts": {
        "post-root-package-install": [
        "post-install-cmd": [
            [...] //do not copy/paste, it's just an example !
            "touch bin/mocha && chmod +x bin/mocha && echo '#!/bin/bash \n DIR=$( cd \"$( dirname \"${BASH_SOURCE[0]}\" )\" && pwd ) \n export PATH=$DIR/../vendor/nodejs/nodejs//bin:$PATH \n ../node_modules/.bin/mocha \"$@\"' > bin/mocha"
        "post-update-cmd": [
            [...]  //do not copy/paste, it's just an example !
            "touch bin/mocha && chmod +x bin/mocha && echo '#!/bin/bash \n DIR=$( cd \"$( dirname \"${BASH_SOURCE[0]}\" )\" && pwd ) \n export PATH=$DIR/../vendor/nodejs/nodejs//bin:$PATH \n ../node_modules/.bin/mocha \"$@\"' > bin/mocha"

This line will create the mocha file and set the execution permission.

To actually install these:

composer install

Create a js directory src/Tests

With you IDE, or

mkdir src/Tests/Js

Finally use it just like that

Run all tests files:

bin/mocha src/Tests/Js/*

Or specific one:

bin/mocha src/Tests/Js/file.js

That’s it ! You can now unit test easily your javascript code !

As always, if you have question,  better practice, suggestion comment !

symfony, composer, clear:cache issue with vagrant and virtualbox

Quick tip today,

I have just finished to setup a new development environment with vagrant for my Symfony project, I have struggled with the clear:cache command, every time this command is failing.

Virtualbox seems to be the one to blame, below some relevant links:

The solution I have chosen is to change the directories of logs and cache:

    public function getCacheDir()
        if (in_array($this->environment, ['dev', 'test'])) {
            return '/tmp/cache/' .  $this->environment;

        return parent::getCacheDir();

    public function getLogDir()
        if (in_array($this->environment, ['dev', 'test'])) {
            return '/var/log/symfony/logs';

        return parent::getLogDir();

And all is fine with Symfony and vagrant after that, composer run, do is clear:cache, and nothing is broken anymore !


Vagrant debian Stretch 9.4 for Symfony dev

I choose to have a parent directory for my vagrant projects, -p or –parents tells mkdir to create all the needed parent folders.

mkdir -p vagrant/myproject
cd vagrant/myproject

vagrant init

vagrant init
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`` for more information on using Vagrant.

Edit Vagrantfile:

In the Vagrantfile you have this by default: = "base"

edit to use the box you choose, I choose this one

info: mainpage of debian boxes = "debian/stretch64"

vagrant up

vagrant up

Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'debian/stretch64' could not be found. Attempting to find and install...
    default: Box Provider: virtualbox
    default: Box Version: >= 0
==> default: Loading metadata for box 'debian/stretch64'
    default: URL:
==> default: Adding box 'debian/stretch64' (v9.4.0) for provider: virtualbox
    default: Downloading:
==> default: Successfully added box 'debian/stretch64' (v9.4.0) for 'virtualbox'!
==> default: Importing base box 'debian/stretch64'...                                                                                
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'debian/stretch64' is up to date...                                                                     
==> default: Setting the name of the VM: myproject_default_1523353141366_70143                                                       
==> default: Clearing any previously set network interfaces...                                                                       
==> default: Preparing network interfaces based on configuration...                                                                  
    default: Adapter 1: nat                                                                                                          
==> default: Forwarding ports...                                                                                                     
    default: 22 (guest) => 2222 (host) (adapter 1)                                                                                   
==> default: Running 'pre-boot' VM customizations...                                                                                 
==> default: Booting VM...                                                                                                           
==> default: Waiting for machine to boot. This may take a few minutes...                                                             
    default: SSH address:                                                                                             
    default: SSH username: vagrant                                                                                                   
    default: SSH auth method: private key                                                                                            
    default: Vagrant insecure key detected. Vagrant will automatically replace                                                       
    default: this with a newly generated keypair for better security.                                                                
    default: Inserting generated public key within guest...                                                                          
    default: Removing insecure key from the guest if it's present...                                                                 
    default: Key inserted! Disconnecting and reconnecting using new SSH key...                                                       
==> default: Machine booted and ready!                                                                                               
==> default: Checking for guest additions in VM...                                                                                   
    default: No guest additions were detected on the base box for this VM! Guest                                                     
    default: additions are required for forwarded ports, shared folders, host only                                                   
    default: networking, and more. If SSH fails on this machine, please install                                                      
    default: the guest additions and repackage the box to continue.                                                                  
    default: This is not an error message; everything may continue to work properly,                                                                                                                                                                                           
    default: in which case you may ignore this message.                                                                                                                                                                                                                        
==> default: Installing rsync to the VM...                                                                                                                                                                                                                                     
==> default: Rsyncing folder: /var/www/myfolder/vagrant/myproject/ => /vagrant

==> default: Machine 'default' has a post `vagrant up` message. This is a message
==> default: from the creator of the Vagrantfile, and not from Vagrant itself:
==> default: 
==> default: Vanilla Debian box. See for help and bug reports

  • « vagrant ssh » to log in your new virtual machine
  • « exit » to logout
  • « vagrant destroy » to stop the virtual machine