FusionAuth/fusionauth-react-sdk

[RFC] Utilize Mock Server Worker for tests

Closed this issue · 5 comments

NOTE: this issue tracks the discussion around the decision to even do this. This is not the implementation issue.

Situation

The FusionAuth React SDK currently extensively uses function mocking in order to test functions that access the network.

Challenge

While this ensures that a function by a particular name is called, it isn't a very robust way to check that a function does what it's supposed to do.

Question

How can we test functions that access the network in a more robust way that gives us more confidence in the results of our tests?

Answer

Let's use Mock Service Worker (MSW). MSW is the industry standard API mocking library that allows you to write client-agnostic mocks and reuse them across any frameworks, tools, and environments. It's well supported and has plenty of resources and documention.

Basically, we can utilize it to setup Service Workers that intercept network requests and respond with what we want to test. I.e. a function that accesses GET /some-endpoint won't need to be mocked. It will make it's request normally and the Service Workers will intercept the call and respond accordingly.

We're able to easily test multiple response types as well, so we can make sure any network failures are handled in an appropriate way as well.

This will let the code run that should run and the surface area of our tests will be increased and so will the accuracy of our tests.

MSW is also framework agnostic which means we could reuse the work we do for the other FusionAuth SDKs, increasing our confidence in those tests as well.

What's the lift? 💪

The FusionAuth React SDK doesn't hit very many endpoints (about 6), so even accounting for variations, we're not looking at too much setup time.

After setup, we just need to update the tests to remove the mocked calls. This also won't be very time consuming as there aren't many functions to worry about.

Estimate: 1 developer 1 day

Requesting comments and thoughts from @mooreds.

Requesting comments and thoughts from @lyleschemmerling

I don't think is a bad idea, but as I just commented on #89 I do think we should be using the sdk example repo as the primary limtus for the functionality of the sdk. This will ensure that it does work with FusionAuth the way that we intend, and we also expect our customer we use these as well as a guide for setting up their own applications. Extra mocking in-repo would be nice as a bonus but I don't think is required

@lyleschemmerling Testing the SDKs in the example and quickstart repos is a great thing to do but that doesn't take away the benefit of having more robust tests within the project itself. We can't easily incorporate that style of testing in CI/CD processes in GitHub during releases - we can setup up some E2E testing like that but it's more complicated than setting up MSW.

I'm fine marking this as a "maybe later" thing though if we don't see a big benefit for right now. 👍

let's keep this as "maybe later"