TDD, clone & Readme

Summary

  1. Status of the project and TDD applied.
  2. _.clone
  3. Readme

Session

So, after the holidays AirCrewMentor had, it was time to get our hands back on work.

I subscribed to Let’s code Javascript to learn the technique of James Shore with the TDD style and I found it really useful.

AirCrewMentor was already pressing me to use this sort of Workflow and I have to say that now I can see why was he so persistent, it is a really good way of code that guarantee that your code is delivered well tested and free of errors.

So James Shore has the following workflow (basically speaking):

  • Check lint (if there are errors the test are not performed, they need to be fixed first)
  • Test the code.

James uses Jake for his automation tool that includes every process to do.

I am feeling comfortable with Make so I decided to adapt the TDD with it.

Here you can see the makefile I am actually using:

SHELL = /bin/bash
MAKEFLAGS += --no-print-directory --silent
export PATH := ./node_modules/.bin:$(PATH):./bin
LINT_DIR = $(wildcard *.js test/*.js lib/*.js test/**/*.json spikes/*)

setup:
  npm install

grunt-server:
  grunt server

path:
  echo $$PATH

which:
  which grunt

# run test with dependencies lint and jscs
test: setup lint style
  echo "Test started"
  mocha test/

# Dev mode for continuous testing
dev:
  mocha --watch test

lint:
  echo "Linting started..."
  jshint $(LINT_DIR)
  echo "Linting finished without errors"
  echo

style:
  echo "Checking style..."
  jscs $(LINT_DIR)
  echo

# Continuous Integration Test Runner
ci: lint style test
  echo "1. Make sure 'git status' is clean."
  echo "2. Build on the integration box."
  echo "3. 'git checkout integration'"
  echo "4. 'git merge branch_desired_to_bring_to_integration --no-ff --log'"
  echo "5. 'git checkout master'"

.PHONY: test

I’ve discovered JSCS, a tool I didn’t know it was there, so I decided to add it to my workflow. It has been a good discovery for me, a pain in the ass sometimes but my code-style is better, so I am going to use it.

1. Status of the project and TDD applied.

So, I showed AirCrewMentor the project I created, with the workflow and how I used to improve the formatMoney function I had.

To see all the test passing was a blessing, as well as the code well checked without lint or style errors.

I have to admit that I am proud of what I’ve done, how I am using constants into the Makefile to easier everything, how the workflow is really flowing and doing everything we want to, AirCrewMentor taught me since the beginning how to do it, but now I am the one using it at my wish, I love it, I know it is not a big thing but it is a thing I’ve done.

AirCrewMentor loved the work I’ve done and how I have been able to adapt and learn the TDD technique, even so I still have a lot to learn ahead of me.

I am really happy he liked it that much.

2. _.clone

So, viewing the code, we went over my way of assigning the default values, I wasn’t respecting the original options object passed to the function, so we created the following test to be sure of it:

  it("should not modify original options", function () {
    var options = {
      "places": 4
    };
    formatMoney(2000, options);
    expect(options).to.eql({"places": 4});
  });

And it was failing. 😦

So we saw the _.clone method from Lodash, that could be implemented the following way:

options = _.defaults(_.clone(options || {}), defaultOptions);

but we saw many ways of doing the same, we finally went and created our own method:

  clone = _.compose(JSON.parse, JSON.stringify);

Now the test was passing 🙂

In the following Screenshot of node.js we can see how our method clone will work.

Screenshot 2015-07-11 19.49.32

3. Readme

AirCrewMentor explained how important is the Readme file in our project and how he uses it as a guideline of the project.

Homework.

  • Get the code ready for npm.
Thanks @aircrewmentor for everything you are doing.
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s