Effect CommunityEC
Effect Community3y ago
6 replies
attila

Stubbing the Clock service

How do I stub the Clock service? I would expect something like this to work but it doesn't:

import assert from "node:assert";
import { test } from "node:test";
import { Clock, Effect, Layer, pipe } from "effect";

const now = new Date();

test("clock", () =>
    pipe(
      Effect.clock,
      Effect.flatMap((clock) => clock.currentTimeMillis),
      Effect.map((n) => assert.equal(n, now.getTime())),
      Effect.updateService(Clock.Clock, (clock) => ({
        ...clock,
        currentTimeMillis: Effect.succeed(now.getTime()),
      })),
      Effect.provideLayer(Layer.succeed(Clock.Clock, Clock.make())),
      Effect.runSync
    ));


Fails with:
AssertionError [ERR_ASSERTION]: 1693486099895 == 1693486099878
Was this page helpful?