Giter Club home page Giter Club logo

mimic's Introduction

Mimic

Build Status npm version Known Vulnerabilities

Seamless client side mocking https://mimic.js.org

short introduction

What is mimic?

Mimic is a tool for mocking server responses on the client side.

Using Mimic in your project

Add to your packages:

npm install mimic --save-dev

Import in your app:

import 'mimic';

Once you reload the application, the Mimic logo will appear in bottom right corner.

Enjoy!

Loading Mimic only in development environments

To load mimic only in development environments your application will need to be aware if it runs in development or production mode.

If you are using webpack the most common way is to use the DefinePlugin to define NODE_ENV environment variable. Please see webpack DefinePlugin for more information.

If you are using create react app this is applied by default which you can read more about it here.

Once your application is aware of the environment you need to add a conditional require or import statement to load Mimic. For example:

if (process.env.NODE_ENV === 'development') {
  require('mimic');
}

Or using the dynamic import spec (you must have babel and babel-plugin-syntax-dynamic-import installed to use this syntax).

if (process.env.NODE_ENV === 'development') {
  import('mimic');
}

Using Mocks tracked by git

If you want to use mocks which will be committed into git and you're using webpack you can use the following code to import the mocks on application start:

import mimic from 'mimic';
const mocks = require.context(__dirname + '/mocks', true, /\.json$/);
mocks.keys().forEach((key) => mimic.import(JSON.stringify(mocks(key))));

This assumes your mocks are placed in the mocks directory which is located at the root of the project. In the mocks directory you have exports for either complete scenarios or separate mock requests.

Guides

Other Usages

The main and the most obvious use case for mimic is when the API for a UI feature is not ready or incomplete, but it can be helpful in more situations. For example:

  • You have a demo of the product, but the API server is unstable or you need to use fake data.
  • You want to work on a feature outside of company's VPN
  • You need access to API, but don't have an internet connection or it is unstable.

In the cases above, you can use the "recording" feature to record all the real responses from server by hand and then use them for your needs in the future.

Contributing to Mimic

  1. Clone the repo

  2. Install dependencies (at the root of the repo):

    npm install
    
  3. Run the project

    npm start
    
  4. Build for deployment

    npm run build
    
  5. Please submit PRs to the next branch, as we keep master as the latest version of mimic.

Acknowledgements

Thanks Ilya Birman for the new UI for Mimic 2.0

License

The MIT License (MIT)

Copyright (c) 2015 Maayan Glikser

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

mimic's People

Contributors

a1b4 avatar adamkleingit avatar alexilyaev avatar borisd avatar dmitrika avatar eladrealcommerce avatar everdimension avatar ilyagelman avatar kmichal avatar maxvrn avatar morsdyce avatar ronlanger avatar sanusart avatar servis avatar wuchangming avatar xdamman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mimic's Issues

Library challenges

I've read the design doc, nice!

I think we can't use Angular 2 if we're building a library.
But we can use TypeScript since it compiles to ES5.

I'm thinking it should be as lightweight as possible.
I'd go for building a flat design ourselves with Flexbox and CSS3 stuff.
That means dropping IE9 support, I don't think we should support it anyway.

Detect browser features and select an interceptor strategy to use

Some browsers support more advanced features such as service workers which let us have greater control over HTTP requests.

Detect if service workers are available and apply the service workers strategy.
If this feature is not available then default back to XHR interceptors.

Overriding rules

If we want an ability to enable multiple scenarios simultaneously, how to handle the situation if you have two enabled scenarios mocking the same url with different response? Both in API and UI.

Investigate Embedding UI as Iframe

Investigate embedding the UI as an Iframe while still being able to communicate with the local API to capture requests and forward them to the UI.

Get rid of inline styles

Search for all files with style= and use a class name instead.

Also...
Use the CSS classes for the method names instead of in the JS
Were already added in _vars.scss and _helpers.scss, just need to use a className instead of hard coding.

Share mocks between ports

When I run my app in a new port (i.e. 8081 instead of 8080) the mocks are not available. Thanks :)

Allow scoping mocks of different apps running on the same port

For example if I run ThetaRay, and Tradency, and each one has its own mocks, they should be scoped to the application name.

This should be optional, i.e. if you don't specify app name it will all reside on 'default' scope - just to make it easier for newbies to start using BDSM.

`checkParamsEquality` throws an error

TypeError: requestParams[key].match is not a function in bdsm/lib/api/interceptor.js

The code:

  checkParamsEquality(requestBody, responseParams) {
    const requestParams = UrlUtils.parseRequestBody(requestBody);
    const parameters    = Object.keys(requestParams);

    for (let key of parameters) {
      const regex = responseParams[key] ? new RegExp(responseParams[key]) : null;

      if (regex && !requestParams[key].match(regex)) {
        return false;
      }
    }

    return true;
  }

requestBody:
"{"AccountID":21431,"StartTime":"2015-11-01T00:00:00"}"

responseParams:
{ AccountID: 21431 }

I fails because requestParams[key] is a number value and not a string.

@morsdyce @ilyagelman
I had a really hard time understanding what this function does, and it's still a puzzle why do we need the RegExp.
Why not save the request body on the mock and compare them as is?
If it's not an option, am I to assume this function should compare 2 objects?
Perhaps it's a good time to add Lodash.

Improve XHR Interceptor to distinguish between scenarios and mocked requests

Improve XHR interceptor logic to activate the correct set of rules needed to run at that point of time.

If a scenario is active it should go over all the mocked requests associated to the scenario and get the active mocked requests for that scenario.

If mocked requests is active go over all the mocked requests and get the active mocked requests.

only active mocked requests must intercept XHR requests, all other requests must pass through.

New mock doesn't show on dropdown

When creating a new mock and saving it, the dropdown doesn't update immediately. You need to close BDSM and open it in order to choose the new mock.

Separate UI toggle to a react element

Refactor the UI toggle to be a react element and render it in the page context (separate from the main UI).

In order to increase maintainability.

Add support for JSONP

Add support for JSONP requests. This is most likely only achievable using service workers and will only be available for supporting browsers

ask for confirmation before deleting scenarios

Currently when deleting a scenario there is no confirmation asking you if you really want to delete it which is bad because the delete button could be clicked by mistake (or by a cat on your keyboard).

Add a confirmation modal that checks if you really want to delete this resource.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.