fake-fetch icon indicating copy to clipboard operation
fake-fetch copied to clipboard

es module output

Open LarsDenBakker opened this issue 6 years ago • 2 comments

The code published to NPM is commonjs, which can't be run in the browser without transformations.

It would be great if you could provide an output using standard es modules, so that this library can run in the browser directly. As an example, sinon uses rollup to generate an esm bundle (https://github.com/sinonjs/sinon).

LarsDenBakker avatar Aug 25 '19 10:08 LarsDenBakker

Hi @LarsDenBakker, what's your use case? Why are you trying to use fake-fetch in browser env? In case you are using karma you can try browserify preprocessor as described here https://github.com/msn0/fake-fetch#remarks

msn0 avatar Aug 26 '19 08:08 msn0

I'm trying to use fake-fetch in the browser to test my code which runs in the browser.

For example:

<script type="module" src="./my-test.js"></script>
import fakeFetch from 'fake-fetch';

beforeEach(fakeFetch.install);
afterEach(fakeFetch.restore);

it("should fetch what you need", async () => {
  fakeFetch.respondWith({"foo": "bar"});

  await fetch('/my-service', {headers: new Headers({accept: 'application/json'})});
  expect(fakeFetch.getUrl()).toEqual('/my-service');
  expect(fakeFetch.getMethod()).toEqual('get');
  expect(data._bodyText).toEqual('{"foo":"bar"}');
  expect(fakeFetch.getRequestHeaders()).toEqual(new Headers({accept: 'application/json'}));
});

The goal is to not have to run any complex build tooling but to provide a standards-compliant module.

LarsDenBakker avatar Aug 26 '19 12:08 LarsDenBakker