Live data from Hacker News

MirageJS: An API mocking library for frontend development

miragejs.com

21–30 of 52 posts

Re: MirageJS: An API mocking library for frontend development

#21
post #10

I would rather make a component that takes data as props. Now make a component on top that runs a network request and injects the network response into it. Now you have a reusable component independent of the api. Your network function can be tested individually. Your top level component doesn’t need testing as that is the part that would be mocked. You can now test your actual component without any mocking. If you n…

We use the "container" pattern for this via graphql (the container component handles all the data fetching and logic therein, the underlying component is usually attempted to be pure / functional / stateless. )

Re: MirageJS: An API mocking library for frontend development

#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.

Re: MirageJS: An API mocking library for frontend development

#23
post #16
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! Yes! Yes! I've been preaching this approach ever since watching Gary Bernhardt's talk on "Boundaries" [1]. Splitting conditional logic from dependencies makes code so much easier to test. If you can do that, there's no need to mock anything. You can just pass simple data objects into your conditional logic for testing, and use a handful of integration tests to validate the end-to-end flow. [1] https://www.destro…

Bertrand Meyer talked a similar strategy. I believe he called them decisions and actions. There's a lot of requirements tied up in decisions.

With the exception of callbacks (and async solves that for single-issue scenarios), arguments don't need to be mocked. They're just arguments.

Re: MirageJS: An API mocking library for frontend development

#24

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…

Kudos. Miracle follows the similar philosophy shared also by Ember that i know will be the future of the webdev

Re: MirageJS: An API mocking library for frontend development

#25
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.

same. the small demos on the landing page are amazing. for once, i got a lot from a library by not even moving away from the landing page!

Re: MirageJS: An API mocking library for frontend development

#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).

Re: MirageJS: An API mocking library for frontend development

#27
This is cool. Mockability is one reason I've been so heavily biased to choosing GraphQL for my backend APIs recently. GraphQL makes it super easy to (a) first define a contract between client and server, and then (b) front end teams can trivially mock this out to develop the front end while the backend team actually implements the API.

Glad to see something like mirage solving this across all different types of API frameworks.

Re: MirageJS: An API mocking library for frontend development

#28
You beat me to this idea but I'm glad you did. Nice library. Mock API's are quite the productivity multiplier. I use them extensively for local development, prototyping, testing, and demoing. I've hacked similar things for work; first one was based on jsondb and the second one was based on axios-mock-adapter.

Colorizing[1] the console logs for quickly parsing success and failure might be a nice addition. [1]: https://developers.google.com/web/tools/chrome-devtools/cons...

edit: grammar

Re: MirageJS: An API mocking library for frontend development

#29
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 first page of the docs list a few reasons.

https://miragejs.com/docs/getting-started/introduction

"Use a client-side interceptor to handle your app's network requests. ... This is the most flexible approach, but it requires you to start from scratch in each project, and leaves it up to you to enforce conventions across your apps."

"Importantly, because Mirage mocks the HTTP boundary instead of the JavaScript code your app uses to make network requests, you never need to modify your application code to account for whether your app is talking to Mirage or to your real production backend."

Re: MirageJS: An API mocking library for frontend development

#30
I've used mirage extensively throughout the past 5+ years across multiple projects/teams and it truly is a great tool. In a recent project we had multiple "scenarios" defined that represented different high-level states of the app. We added a page that allowed switching between the scenarios during local development and it was a huge boon to productivity, while also allowing us to write certain automated smoke tests that otherwise would have been more cumbersome with e.g. selenium.

Working on a React project recently, I was surprised that there wasn't an option like mirage, but now that there is, I highly recommend it!

Post reply on HN