From 019c5cce8fe0a0b9ae5bbd246f46b8593d3a7024 Mon Sep 17 00:00:00 2001 From: Mario Pinto Date: Mon, 9 Sep 2024 10:36:16 +0100 Subject: [PATCH] doc(testing): Add `testing` section --- docs/CONTRIBUTING.md | 64 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/docs/CONTRIBUTING.md b/docs/CONTRIBUTING.md index e3e0d2d..332868d 100644 --- a/docs/CONTRIBUTING.md +++ b/docs/CONTRIBUTING.md @@ -5,3 +5,67 @@ - `main` is the main branch. - `beta` is the branch where the latest changes are merged into. - / + +## Testing + +### Monad tests + +From testing perspective, we are applying a role based testing approach. In this case, +the role is monad, and we create tests cases for each monad. + +check the `src/monads/monads.test.ts` folder for more information. + +So, you have just to new test cases for your new monad that you are going to implement. + + +### Matchable tests + +From testing perspective, we are applying a role based testing approach. In this case, +the role is matchable, and we create tests cases for each matchable. + +check the `src/matchable/matchable.test.ts` folder for more information. + +So, you have just to new test cases for your new matchable that you are going to implement. + + +### Specific tests for each monad + +Each `monad` has a particular way to be created or a particular API. So, we have to create +specific tests for them. + +for example, the 'Try' monad has a particular way to be created, so we have to create +tests for that. + +```typescript +import { describe, it, expect } from 'vitest'; +import { Failure, Success, Try } from './try'; + + it.each([ + { type: 'Success', executable: () => 2, expected: new Success(2) }, + { + type: 'Failure', + executable: () => { + throw new Error(); + }, + expected: new Failure(new Error()), + }, +])('should create $type correctly', ({ executable, expected }) => { + expect(Try.execute(executable)).toEqual(expected); +}); +``` + +or the 'Option' monad has a particular API, so we have to create tests for that. + +```typescript +import { describe, it, expect } from 'vitest'; +import { None, Option, Some } from './option'; + +it.each([ + { type: 'Some', option: Option.of(2), expected: 2 }, + { type: 'None', option: Option.of(undefined), expected: 2 }, +])('$type should handle getOrElse operation correctly', ({ option, expected }) => { + expect(option.getOrElse(2)).toEqual(expected); +}); +``` + +