Effect CommunityEC
Effect Community2w ago
8 replies
bebenzer

Organizing Test Layers and Mocks in TypeScript for Unit Tests

for unit tests, how would you organise your test layers?
static testLayer = Layer.succeed(this,
    PolicyRepository.make({
        findByIDs: () => Effect.succeed([]),
        getRules: () => Effect.succeed([]),
    })
)

I started by defining the testLayer as a static method on my service, but if I have some tests where I want to have a different behaviour I obviously can't update the testLayer without modifying the behaviour of other tests.
should I instead define my test layers in my test files, more or less like you would do with "normal" code where I would have multiple classes that implements my interface: class PolicyRepoMockSuccess implement PolicyRepo , class PolicyRepoMockEmptyResponse implement PolicyRepo and so on.

or are there other patterns?

I like how you can do it with go with mocks, specifically the mockery lib where I can define the implementation for one of the methods in the test:
func TestFoo() {
  mockService := foomock.NewService()
  mockService.On("GetRules", mock.Anything).Returns(nil)
  ...
}

func TestBar() {
  mockService := foomock.NewService()
  mockService.On("GetRules", mock.Anything).Returns([a,b,c])
  ...
}
Was this page helpful?