Validating data in the form action
Now we’re all set up for errors on the client, but we need the server code to actually do the validation checks. We’ll add two checks: one to check that the name is not empty, and one to check that the date can be parsed into a valid Date
object.
Each of these checks needs four unit tests: the first to ensure we break early without adding the birthday; the next to check the 422
error code; then one to check the error message text; and finally, one to check that the original data is passed back. (In Chapter 8, Creating Matchers to Simplify Tests, you’ll see how to build a matcher that will roll up three of these tests into one single test.)
The beforeEach function
This section introduces the beforeEach
function, which is used to run setup code before each of the tests within the describe
block. It is a useful tool for reducing duplication within your test suites. You can consider it as part of the Arrange phase of your...