...

/

Sharing Behaviour

Sharing Behaviour

Learn to use shared behavior to write better tests.

Often, we want to test a number of scenarios, and we end up with duplicate test code. If we have more than a few similar tests, then it will often be clearer to use shared behavior to abstract away that repetition. The simple technique is to wrap our test in a forEach loop. This can let us run the same test for a number of different values.

Using a forEach loop

Consider the example below. In the validator.service.spec.ts test suite, on lines 18 and 31 ...