BedrockStreaming / superagent-mock

superagent plugin allowing to simulate HTTP calls by returning data fixtures based on the requested URL
MIT License
170 stars 46 forks source link

Better usage when mocking client #14

Open ncuillery opened 9 years ago

ncuillery commented 9 years ago

It's okay to write these two lines of code

var config = require('./superagent-mock-config'); 
require('superagent-mock')(request, config);

in test files (preprocessor, beforeEach or whatever) but when you use superagent-mock in a client app (running in the browser), you had to append these lines somewhere in your source files.

How about a webpack loader (a postLoader I think) ?

I'm not a webpack expert but I think it could do the job and keep your sources clean :metal:

fdubost commented 9 years ago

Good idea ! :+1:

Feel free to make a PR :wink:

elisherer commented 7 years ago

I use in webpack.config:

    new webpack.DefinePlugin({
      ...
      __IS_MOCKED__: JSON.stringify(argv.env.mock),
    }),

And inside my src/index.js:

if(__IS_MOCKED__) {
  require('./mocks');
}

The mocks folder index file will have the mock configuration together with superagent-mock require. The uglification process will remove it from the production build (provided no --env.mock was added to its build script)

Don't see it as "dirty" so much.