-
Notifications
You must be signed in to change notification settings - Fork 5
/
02-testing.ts
68 lines (58 loc) · 1.89 KB
/
02-testing.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
*
* -------------
* 02-testing.ts
* -------------
*
* Testing is the ability of asserting that a system behaves as expected.
* As obvious as it may seem, testing can be very tricky if the program is coupled
* to implementation details and has implicit (hidden dependencies) that we can't control.
* Thankfully as we saw by the end of the previous chapter, Effect leverages
* the dependency inversion principle and explicit dependencies to favor testability.
*/
import * as assert from "node:assert";
import { Effect, pipe, Context, Exit } from "effect";
/**
* Let's just add some boilerplate to emulate a test runner
*/
function test(name: string, testEffect: Effect.Effect<void, unknown, never>) {
return pipe(
testEffect,
Effect.runSyncExit,
Exit.match({
onFailure: () => console.log(`FAILED: "${name}"`),
onSuccess: () => console.log(`PASSED: "${name}"`),
})
);
}
class User {}
class UserCreationError {}
interface UserRepository {
createUser: () => Effect.Effect<User, UserCreationError, never>;
}
const UserRepository = Context.GenericTag<UserRepository>("UserRepository");
const dummyUseCase = pipe(
UserRepository,
Effect.flatMap(({ createUser }) => createUser())
);
test(
"Should produce a UserCreationError",
pipe(
Effect.gen(function* ($) {
const result = yield* $(dummyUseCase);
assert.deepStrictEqual(result, new User());
}),
/**
* We can provide a fake implementation of the UserRepository and this approach
* is valid for any dependency that is required within the scope of the resources
* using during the test.
*/
Effect.provideService(UserRepository, {
createUser: () => Effect.succeed(new User()),
})
)
);
/**
* As we can see, testing is very easy with Effect and it was thought from the ground
* up to allow an effect description to be decoupled from its implementation details.
*/