This is the Angular and React Version of this Article
The C#- Python version is here:
Testing C# and Python with Behavior-Driven Development (BDD)
Also check out
All Testing Articles here:
https://brianbraatz.github.io/search/?keyword=testing
A Brief History of BDD
Once upon a time, in the dark ages of software development (a.k.a the early 2000s), developers and testers were at war.
Developers wrote code that they thought made sense, while testers wrote test cases that developers barely understood. Chaos ensued.
Then, in 2003, Dan North had a revelation: “What if developers and testers spoke the same language?” And thus, Behavior-Driven Development (BDD) was born.
BDD took inspiration from Test-Driven Development (TDD) but aimed to make tests more human-readable, so that business people, testers, and developers could all be on the same page. Instead of cryptic unit test names, BDD focused on describing behavior in plain English using a structured format called Gherkin (yep, like the pickle).
The idea? Tests should be human-readable and describe what the system actually does. That way, even non-tech people (gasp!) could understand what was going on. So instead of writing a test that checks if 2 + 2 === 4
, you’d write:
|
|
Now the Non-Engineers and the Engineers have a common way to communicate features and how to test them .
And theis plain language, can be used directly- or semi-directly to actually “be” the test!
How Does BDD Compare to Other Testing Approaches?
Feature | Unit Testing | BDD | TDD |
---|---|---|---|
Focus | Code correctness | System behavior | Code design |
Readability | Low | High | Medium |
Collaboration | Limited | Encourages team effort | Mostly developers |
Documentation | Implicit | Self-documenting | Partial |
Ease of Writing Tests | Medium | Higher (if done right) | Medium |
Basic Idea
- Unit tests check if code does what it should.
- TDD makes you write tests first, then code.
- BDD makes you write tests in plain English (or Gherkin) so everyone understands what’s happening.
BDD in Angular
Angular loves testing, and BDD can easily be done with Jasmine & Cucumber. Here’s an example:
Jasmine BDD Example
|
|
Simple, right? Now, if you want Gherkin-style tests, you can use Cucumber.js with Angular:
Cucumber.js Example
|
|
Pair this with Protractor, and you’ve got yourself a full-fledged BDD setup.
BDD in React
React plays well with Jest & Cucumber. Here’s a BDD-style Jest test:
Jest BDD Example
|
|
And if you’re feeling fancy, you can use Cucumber.js with Jest:
Cucumber.js Example
|
|
This pairs beautifully with React Testing Library and Cypress for E2E testing.
Different Ways to Do BDD in Angular and React
- Jasmine + Karma (Angular) – The default, easiest way.
- Jest + React Testing Library (React) – The go-to testing combo for React apps.
- Cucumber.js + Protractor (Angular) – For serious Gherkin lovers.
- Cucumber.js + Cypress (React & Angular) – If you want full BDD-style E2E tests.
Conclusion
BDD is all about writing tests that make sense. It bridges the gap between techies and non-techies while making sure software behaves as expected. Whether you’re on Team Angular or Team React, BDD can level up your testing game. So go forth and write tests that actually make sense!
Key Ideas
Concept | Description |
---|---|
BDD | Behavior-driven development for better test readability |
History | Invented by Dan North in 2003 to improve unit testing |
Comparison | BDD vs Unit Testing vs TDD |
Angular BDD | Using Jasmine, Karma, Cucumber.js, and Protractor |
React BDD | Using Jest, Cucumber.js, and Cypress |
Best Practices | Write human-readable tests that describe behavior |
References
- Dan North’s Original BDD Post - Link
- Jasmine Documentation - Link
- Cucumber.js Documentation - Link
- Jest Documentation - Link
- Cypress Documentation - Link
- Protractor Documentation - Link
Happy testing! 🚀