Live data from Hacker News

MirageJS: An API mocking library for frontend development

miragejs.com

41–50 of 52 posts

Re: MirageJS: An API mocking library for frontend development

#41
post #11

I believe Michael Feathers was just started a Twitter thread yesterday that talked, among other things, about moving IO to the edges of your code. If you can divide acquisition from usage, I wonder how much less effort you need to put into API mocking. Ages ago and shortly after I started testing, was the last time I had to do any low-level HTTP response processing. My coworkers could not fathom why I had divided the…

Yes, Cycle.js [1] taught me that lesson and it's been useful ever since.

[1] https://cycle.js.org/

Re: MirageJS: An API mocking library for frontend development

#42
post #26

What is the benefit of a request mocking framework versus hiding all api requests behind a module? Instead of // MyComponent.js const users = await fetch(... Use this // MyComponent.js const users = await api.getUsers() // api.js export default { getUsers() { return fetch(... } } To mock, you replace the entire api module (or individual functions).

Mirage is not just a request mocking framework. It comes with an ORM and a way to define factories, so you can create random data with ease, while you are testing your app.

I am sure that you can do all without Mirage and I am sure that your solution might fit your project best, but I doubt that it will take you the same amount of effort

(I have been using Mirage for the past 4 years and I love it)

Re: MirageJS: An API mocking library for frontend development

#45
post #11

I believe Michael Feathers was just started a Twitter thread yesterday that talked, among other things, about moving IO to the edges of your code. If you can divide acquisition from usage, I wonder how much less effort you need to put into API mocking. Ages ago and shortly after I started testing, was the last time I had to do any low-level HTTP response processing. My coworkers could not fathom why I had divided the…

I believe this is similar to [Uncle's Bob clean architecture theory](https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-a...) where you split the infrastructure from the business logic. Also similar to the repository pattern. In general, you are able to achieve loose coupling between your modules and thus better unit testing.

But being able to go down to the HTTP level and mock responses there provides a different benefit. You are able to do end to end tests but isolated to your own service. I always do that (alongside unit tests) so I can even catch errors outside of my code base, i.e. in my dependencies.

Re: MirageJS: An API mocking library for frontend development

#46
post #26

What is the benefit of a request mocking framework versus hiding all api requests behind a module? Instead of // MyComponent.js const users = await fetch(... Use this // MyComponent.js const users = await api.getUsers() // api.js export default { getUsers() { return fetch(... } } To mock, you replace the entire api module (or individual functions).

The benefit is doing integration tests, testing the glue between your components. By mocking down to the HTTP level you can be sure that the code will work end to end, which is inside your whole codebase.

Imagine that fetch suddenly has a bug (extreme example but it could be another open-source library). Your tests above wouldn't catch it.

I always strive to do at least a few integration tests like these and not stand purely on individual unit tests.

Re: MirageJS: An API mocking library for frontend development

#48
post #22

Hello! I'm Sam and I built Mirage JS to be a framework-agnostic API mocking library. Mirage is extracted it from an addon that's been widely used in the Ember ecosystem for the past 4 years, at companies like Apple, Heroku, Square and Footlocker. Nearly all frontends need to talk to an API, no matter what framework they're built with! So over the past year I've made Mirage work with any tool or test runner. I really…

Phenomenal landing page. The first text I read is a ~10-word description of the value proposition. The next thing my eyes are drawn to is a visual demo. The first thing I can click is a link to use it myself. Plus, it's all visually very appealing without being distracting. Well done.

We spent a long time on it so that really means a lot! Thanks for the kind words :)

Re: MirageJS: An API mocking library for frontend development

#50
post #9

I'm a bit of a noob so hang with me here. So you install it as a dev dependency via npm. So is it running ... with the client app as a proxy between the client application and just waiting for the API requests and taking those responding? Is it possible to configure some delay in responses?

We use mirage with test-controlled response timings to be able to acceptance test loading states. Works great.

You can also configure a universal delay if you’re using mirage in development and want to get a feel for your app with a certain amount of latency.

Post reply on HN