TDD is one of the most hotly discussed subjects in the software development world. Even the most carefully constructed applications grow to the point where debugging and ensuring quality becomes difficult. Test-driven development (TDD) helps with this tremendously by ensuring that all parts of your application are covered by tests. In this course, Shaun Wassell explores the foundational techniques and tools for unit and integration tests. Along the way, he zooms out to examine how they all fit together. Shaun also highlights TDD's strengths and weaknesses and provides real-world examples that showcase how TDD can fit into your development workflow.
- Learning objectives
- Advantages and disadvantages of TDD
- The basic TDD cycle
- The purpose of unit testing
- Testing basic and asynchronous functions
- How integration tests fit into TDD
- JavaScript libraries for integration testing
- Improving your TDD workflow
- Checking the test coverage of the codebase
node -v // v10.15.1
npm -v // 6.6.1
npm install
Mocha JS // test runner and testing framework
Chai // assertion library
Writing tests for your production code before you actually write the production code
- The goal of test-driven development is code quality.
- There are good and bad tests.
- There are many different opinions about the right way to do TDD
- TDD forces us to clarify our thinking.
- TDD improves communication between developers.
- TDD prevents code rot, a slow deterioration of software quality over time.
- TDD improves the structure of our production code, to create loosely coupled modular code.
- Allows developers to make 'worry-free' changes.
- TDD takes longer at first.
- TDD isn't always a favourite with management.
- Beware of writing bad tests!
- [RED] Write a failing test.
- [GREEN] Write production code to make the test pass.
- [REFACTOR] Refactor the code you wrote.
- Readable
- Isolated
- Thorough
- Explicit
- Unit Tests - Test very specific, low-level pieces of functionality
- Integration Tests - Ensure that the individual pieces of your application work together correctly
- End-to-End (E2E) Tests - Ensure your entire application works as seen from the viewpoint of a user
- Unit tests cover the smallest pieces of functionality: functions.
- Our test should cover a representative range of inputs.
- If you can break your production code without breaking your tests, they're not thorough enough.
- Units tests are you used to test the outcomes of pure functions, functions without states or side effects.
- A testing environment/test runner
- A testing framework
- An asserting library
"Mocks" and "stubs" are the most common test doubles. "Don't mock what you don't own." Pure functions equals loosely coupled code.
- Single-Service Integration Test
- Test a single piece, or "service" for your application end to end, independent of the other pieces.
- Suspectible to Outside Change
- Boudary Integration Tests
- Test the communcation between different pieces of your application.
- Don't use test double for boundary tests.
- GET -> '/users/abc' -> returns { id, username, email } "Status Code OK 200" && Content-type: JSON Header
Learning Functional Programming with JavaScript React: Creating and Hosting a Full-Stack Site React: Testing and Debugging