Skip to content

Testing

Tyler edited this page May 21, 2017 · 1 revision

Testing Methodology

Client

No testing yet...

API

No testing yet...

Snippet

Currently only the snippet portion has automated testing setup.

Now, the snippet is not a JavaScript module, so the testing offers a challenge. It is solved by having a conditional export of each component that requires testing. You'll notice at the end of the snippet file snippet/index.js a series of lines that look like this:

exports.pipe = pipe

This will make pipe() function importable from the .test.js files. It is set up as a conditional for when node is available.

We will be using the standard file structure for jest which includes naming test files like: filename.test.js

These files will be placed in the snippet/test directory.

Because of the small size of tested file we will be keeping test suites to single functions.

Testing Stack

  1. Jest: is our testing framework.
  2. Sinon: this is a standalone spy, stub, and mocking framework for JavaScript. It's useful as a test 'probe'.

How To Add Tests

  1. I've added a new function! Arg! What do I do?
  2. First export the function in testing exports section at the bottom of the file: exports.functionName = functionName
  3. Now create a new file in the snippet/test with the name in the format functionName.test.js
  4. In the test file import the function const functionName = require('../index.js').functionName
  5. The basic format for testing is: describe('functionName()', () => it("returns X"))
  6. Nested describes() are allowed and encouraged.
  7. The phrases attached to these syntactic functions should document their own process and the code. So try to make it as clear as possible.
  8. Create unit tests! I know this is a big black box for some people. But it can't all be explained it here. Tests are created for pipe() as a template if you'd like! You can also check the documentation for Jest and Sinon.
  9. Check coverage. When you run yarn test it will output a list of coverages. If you'd like specific highlights you can checkout the automatically generated snippets/coverage directory which will generate an easy to use webpage containing coverage information.

Tips

  • Pure functions are much easier to test.
  • Functions that do one thing are much easier to test than functions that do multiple things.
  • Tests can be written before your code, try writing them as a guide for what sort of code you need to end up with in the end.
  • Coverage generation is only a guide, there are lots of cases you'd be better off testing that coverage checkers may not notice. Think about edge cases, and test those.

Running tests

You can run yarn test or npm test or even just jest if you have it installed globally. All the configuration is found in package.json so it should be automatically handled without arguments.

Clone this wiki locally