Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Writing Code with TDD | Advanced Techniques and TDD Workflow
Testing JavaScript Code

bookWriting Code with TDD

test.js

test.js

copy

When practicing test-driven development (TDD), you always begin by writing a test that describes the behavior you want from your code before you actually implement the feature. In the code above, you start by writing a test for a capitalize function that should take a string and return it with the first letter in uppercase. At this point, the function does not exist yet, so the test will fail. This is an important part of the TDD cycle because it confirms that your test is correctly set up and that the feature is not already implemented.

Next, you write just enough code to make the test pass. In this example, you implement the capitalize function so that it takes the first character, makes it uppercase, and adds the rest of the string. After this change, running the test should result in a passing test.

The final step in the TDD cycle is to refactor your code. This means you look for ways to improve the code without changing its behavior. You might clean up variable names, remove duplication, or optimize logic. In this simple example, the function is already straightforward, so no further changes are needed. By following these steps—write a failing test, make it pass, then refactor—you ensure your code is both reliable and maintainable.

1. In TDD, what should you do after making a failing test pass?

2. Why do you start with a failing test in TDD?

question mark

In TDD, what should you do after making a failing test pass?

Select the correct answer

question mark

Why do you start with a failing test in TDD?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 5

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Suggested prompts:

Can you explain why starting with a failing test is important in TDD?

What are some common mistakes to avoid when practicing TDD?

Can you give more examples of simple functions to practice TDD with?

Awesome!

Completion rate improved to 7.14

bookWriting Code with TDD

Deslize para mostrar o menu

test.js

test.js

copy

When practicing test-driven development (TDD), you always begin by writing a test that describes the behavior you want from your code before you actually implement the feature. In the code above, you start by writing a test for a capitalize function that should take a string and return it with the first letter in uppercase. At this point, the function does not exist yet, so the test will fail. This is an important part of the TDD cycle because it confirms that your test is correctly set up and that the feature is not already implemented.

Next, you write just enough code to make the test pass. In this example, you implement the capitalize function so that it takes the first character, makes it uppercase, and adds the rest of the string. After this change, running the test should result in a passing test.

The final step in the TDD cycle is to refactor your code. This means you look for ways to improve the code without changing its behavior. You might clean up variable names, remove duplication, or optimize logic. In this simple example, the function is already straightforward, so no further changes are needed. By following these steps—write a failing test, make it pass, then refactor—you ensure your code is both reliable and maintainable.

1. In TDD, what should you do after making a failing test pass?

2. Why do you start with a failing test in TDD?

question mark

In TDD, what should you do after making a failing test pass?

Select the correct answer

question mark

Why do you start with a failing test in TDD?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 5
some-alt