Assert(js) Speaker Series: advice on starting out and building code confidence with testing

Jesse Palmer
Assert(js) Testing Conf
4 min readJan 31, 2018

Below is a Q&A session to answer some common questions about testing. Some of these insights come from my book, Testing Angular Applications, and some from my professional experience.

What testing tools and/or workflow do you use?

I do a lot of testing for Angular applications. I use Karma for unit testing, Protractor for End-to-End testing, and the Angular-CLI for setting up, configuring and installing testing tools.

Setting up an environment for testing is a snap if you use the Angular-CLI. In the past, setting up your testing environment could be challenging and time-consuming. Now it can be done in a matter of minutes.

Once a project is set up with the Angular-CLI there are tons of handy commands that you can execute. Generating code-coverage reports, changing browsers to test against, watching for code changes so that tests automatically run and much more are available at your fingertips right out of the box.

A lot of people in the JS world don’t have a lot of experience in testing. How would you recommend they get started?

If you are just getting started with JS testing, then I would recommend writing tests for pure functions. Pure functions are relatively easy to test since there are no side effects. The function just takes in a value and returns a value. Examples of these could be testing a function that adds two numbers together or a function that takes in a string and returns a string that is in title case.

The way I would get started with this is to install Karma and Jasmine and then create some sample pure functions to write tests against. If you need to help find some examples, check out my repo at https://github.com/jesselpalmer/powerbelt that contains a lot of pure functions along with tests.

This exercise will get you use to creating JavaScript tests along with the common testing tools. Once you are done, I would move onto writing tests for an application. If you don’t have an app you can write tests against, then I would create a quick and dirty app. A lot of JavaScript projects come with scaffolding tools to set up your project. For Angular apps you have the Angular-CLI that I spoke about earlier, for React app you could create an app using the Create React App, and so on.

Once you have the project setup, I would create a basic todo list app and write tests against it. If you have a production app to write tests off then I would just dive in and start writing tests. Google is your friend here. If you would like a more step-by-step process to lead you then I would check out some tutorials online or books.

How would you convince a developer to start including testing in their workflow?

Writing tests right away will increase a developers confidence in their code. A lot of time I hear developers afraid to refactor their code, scared to add to existing production code or worries about upgrading a dependency. Most of the time this occurs because people are nervous that they may break something.

You know what will help with detecting the breaking of code? Of course you do, it’s testing!

So if someone is “too busy” to write tests, then I would mention to them that to be a professional, responsible developer that they need to write tests. They should take their craft seriously and want to do the best job they can do. What better way to prove that you care about the longevity of an application than writing tests for it.

When or if the developer happens to move onto another project the developers who inherit the project will undoubtedly appreciate it. So writing tests is a great way to build Karma (pun intended). Plus, a lot of companies will expect that you know how to write tests, so developers should start strengthening those skills now to remain competitive.

How do you convince management or clients that testing is important?

Testing is crucial to the health of an application. In the long run, a tested applications will save time which in turn will save money. All of which clients and managers will be very interested in.

I like to think about testing as an investment. You pay up front and as the application scales having an appropriate amount of testing pays dividends.

If managers or clients have the experience of working on an untested medium to large application then they will have more than likely experienced the pain of brittle code and perhaps even a rewrite if the code base has gotten particularly bad.

When management or clients don’t think that developers should be spending time writing tests, then I would point out that just cranking out feature code or bug fixes without tests will eventually catch-up with them. When it does it could potentially be very painful and adding new code can become a struggle.

You just need to remind the folks responsible for the project that writing tests increase code quality and leads to faster development, not slower.

For conference talk details visit https://www.assertjs.com

--

--