A set of code examples for my talk: Writing Clean JavaScript Tests: Avoiding 10 Common Mistakes That Make Our Test Code Messy.
The common mistakes (and their solutions) illustrated here are:
- Duplicating test setup code
- Not properly grouping tests
- Repeating complicated setup steps
- Hiding test data
- Not using dynamic tests
- Leaking state between tests
- Writing confusing (or broken) async tests
- Unclear test descriptions
- Neglecting tests
- Not writing tests
Slides for the talk can be found here
The src/
folder contains all of the modules/functions/classes that will be used in the tests.
The test-messy/
folder contains versions of the tests containing the common mistakes. These are numbered 01
through 10
corresponding with the list above.
The test-clean/
folder contains versions of the tests with the mistakes fixed. These are named with the same corresponding numbers.
npm test
will run the tests with the Jest test runner
npm run lint
will run the ESLint task.
This project will also run both tasks on pre-commit, and prevent committing if one or both fail. NOTE: Since one of the 'messy' tests contains intentional ESLint errors, --no-verify
must be used when committing (unless the errors get fixed!).