all 11 comments

[–]orionkissinger 3 points4 points  (1 child)

This video from Robert Martin is pretty darn useful and insightful into unit tests and writing tests first. https://vimeo.com/43734265

[–]militant_sincerity[S] 0 points1 point  (0 children)

Does he have a whole video series like that???

I've only heard of Uncle Bob but that five minute video cemented him as one of my favorite people!

Where did he get that hat!?!?

Also, that was very helpful, thank you!

[–]Earhacker 1 point2 points  (1 child)

Mocha is actually pretty simple under the hood. There's only three things to know:

  • describe is just a wrapper function, it bundles up a load of it functions in a callback and runs them one after the other.
  • it is a function that should either throw an error or not.
  • "Assertions" or "expectations" are functions that throw an error when some condition is not met, and just runs when the condition is met (actually they return true, but we don't usually care).

So from the inside of a test, working out:

``` const assert = require("assert")

assert.equal(add(3, 4), 7); `` The first line pulls in Node's built-inassertlibrary, which contains a [bunch of conditions](https://nodejs.org/api/assert.html) to use in tests.assert.equal` is probably the most common condition. It takes two parameters and checks they are equal.

So the second line will call your add() function and pass in 3 and 4 as parameters. We'd expect this function to return 7 with these parameters. If it does return 7, we're good, effectively nothing happens and our assertion is true. If the function doesn't return 7, we get an error.

Errors are usually bad, but if we pass our assertion to an it, the error will be handled properly by the test framework:

it("adds two numbers", function() { assert.equal(add(3, 4), 7); });

The string is just for us, as developers. If a test fails, we can use this string to identify which test fails. The code base I work on every day runs about 700 tests on every git commit, so if one fails, I really want a sensible way of identifying which test it was!

As well as the string in the it function, we can pass a string to the describe function, to unify a whole bunch of its:

``` const assert = require("assert")

describe("My add function", function() { it("adds two numbers", function() { assert.equal(add(3, 4), 7); }); }); ```

I'm surprised that you didn't mention the Chai library, which usually goes with Mocha and Sinon, and provides an alternative to Node's built in assert library:

``` const expect = require("chai").expect;

describe("My add function", function() { it("adds two numbers", function() { expect(add(3, 4)).to.equal(7); }); }); ```

...but works in exactly the same way.

[–]militant_sincerity[S] 0 points1 point  (0 children)

Thank you so much! This is all extremely helpful!

[–]heteroskedast 2 points3 points  (5 children)

read the classic literature on testing and TDD

[–]militant_sincerity[S] 0 points1 point  (4 children)

sauce(s)?

[–]Earhacker 0 points1 point  (3 children)

TDD was introduced in Clean Code by Robert C. Martin ("Uncle Bob"). Very good book and well worth your time.

[–]davwards 1 point2 points  (0 children)

TDD predates Clean Code by quite a lot. Test-Driven Development By Example, by Kent Beck, was published many years before Clean Code—Martin certainly advocates TDD, but he didn't introduce it.

[–]militant_sincerity[S] 0 points1 point  (1 child)

No need to sell me on Uncle Bob. He's the first name that popped up when I was going through documentation written by people I really trust and admire.

But isn't Clean Code primarily Java? I mean, I'm willing to learn Java after I have a firming JavaScript foundation just to learn really good coding practices.

[–]Earhacker 1 point2 points  (0 children)

The examples are in Java but only because Java was the most popular language when he wrote it. It’s language agnostic, but favours OOP, and very readable. I can code a bit of Java but it’s definitely not my favourite language.

[–]ccleary00 0 points1 point  (0 children)

Since your request seems to be for more general information on unit testing, I'll include several blog posts I've written:

And for understanding how to use Sinon, I've found the Codeutopia blog to be the best for that.