Introduction
Test-Driven Development(TDD) and Behavior-Driven Development(BDD) are prevalent development techniques in the programming world. While performing unit tests, one must know about both of these concepts, understand the differences, and know the functionality of TDD and BDD.
In this article, you will learn about TDD and BDD as development techniques and understand its implementation in Jest through test() and it(). If you are unfamiliar with the Jest framework or writing a Unit test on Jest, we strongly recommend that you check out our Getting Started with Jest and Writing First Unit Test on Jest articles.
Test-Driven Development
Test-Driven Development(TDD) is an iterative approach combining programming, creating unit tests, and refactoring. In simple terms, TDD is a software development practice focusing on creating unit test cases before developing the actual code. The test process drives software development in TDD as the name suggests.
TDD is a structuring practice enabling developers and testers to obtain optimized code that proves to be resilient in the long term. Here, developers create small test cases for every feature based on their initial understanding. The primary purpose of this technique is to modify or write new code only if the tests fail, preventing duplication of test scripts.
The Process
The process of Test-Driven Development can boil down to three major phases. Different implementations of TDD work on these three phases at the core.
- Firstly, Developers need to create precise unit tests that verify the functionality of specific features. The test needs to execute, so they must ensure that it compiles. In most cases, the test is going to fail. That is a meaningful failure, though, as developers create compact tests based on their assumptions of how the feature will behave.
- After the test fails, developers can make the minimal changes required to correct the code, and when re-executed, the test runs successfully.
- Once the test runs successfully, they check for redundancy or code optimizations to enhance the overall performance. It ensures that they do not affect the external behavior of the program by the code refactor.
Key Benefits
- TDD Promotes better program design and higher code quality.
- TDD Reduces project development time.
- Provides scope for detailed project documentation
- TDD leads to the development of a flexible and easy codebase to maintain.
- TDD fosters more accessible addition and testing of new functionalities at later stages of development.
Example:
There are essentially two ways to run tests in jest: test () and it(). According to the official documentation of Jest, it(name, fn, timeout) is an alias for test(name, fn, timeout). Both these methods work in essentially the same way.
The elementary test suite can help explain TDD a little bit, but remember that the actual implementation of TDD is on more significant projects.
Program:
//sum.test.js
//import sum
const { sum } = require("./sum");
describe("Addition of Two Number functionality test", () => {
test("Add 1 + 2 should be 3", () => {
expect(sum(1, 2)).toBe(3);
});
test("it should fail if string as parameter", () => {
expect(sum("Hello", "World")).toEqual(
Error("Expecting number type as parameter")
);
});
});Now that we have the test, we can create the file that creates the function in the root function.
Program:
//sum.js
const sum = (a, b) => {
if (typeof a !== "number" || typeof b !== "number")
return Error("Expecting number type as parameter");
return a + b;
};
module.exports = { sum };Output:






