-
Notifications
You must be signed in to change notification settings - Fork 10
Testing
No testing yet...
No testing yet...
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.
- Jest: is our testing framework.
- Sinon: this is a standalone spy, stub, and mocking framework for JavaScript. It's useful as a test 'probe'.
- I've added a new function! Arg! What do I do?
- First export the function in testing exports section at the bottom of the file:
exports.functionName = functionName
- Now create a new file in the
snippet/test
with the name in the formatfunctionName.test.js
- In the test file import the function
const functionName = require('../index.js').functionName
- The basic format for testing is:
describe('functionName()', () => it("returns X"))
- Nested
describes()
are allowed and encouraged. - The phrases attached to these syntactic functions should document their own process and the code. So try to make it as clear as possible.
- 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. - 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 generatedsnippets/coverage
directory which will generate an easy to use webpage containing coverage information.
- 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.
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.