Tips and tricks

I have seen tests where people split up the Arrange-Act-Assert by putting in place comments:

function testMapping(){
  //Arrange
  …
  //Act
  …
  //Assert
  …
}

You're going to wear your fingers to the bone typing those comments for every single test. Instead I just split them up with a blank line. The separation is clear and anybody who knows Arrange-Act-Assert will instantly recognize what it is that you're doing. You'll have seen the example code in this chapter split up in this fashion.

There are countless JavaScript testing libraries available to make your life easier. Choosing one may depend on your preferred style. If you like a gherkin-style syntax then cuumber.js might be for you. Otherwise try mocha, either on its own, or with the chai BDD style assertion library , which is is fairly nice. There are also testing frameworks such as Protractor which are specific to Angular apps (although you can use it to test other frameworks with a bit of work). I'd suggest taking a day and playing with a few to find your sweet spot.

When writing tests, I tend to name them in a way that makes it obvious that they are tests and not production code. For most JavaScript I follow camel case naming conventions such as testMapping. However, for test methods I follow an underscored naming pattern When_building_a_castle_size_should_be_correctly_set. In this way the test reads more like a specification. Others have different approaches to naming and there is no "right" answer, so feel free to experiment.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset