jefflau / jest-fetch-mock

Jest mock for fetch
MIT License
883 stars 116 forks source link

Fetch response.blob() doesn't return instance of Blob constructor #105

Closed jtenner closed 5 years ago

jtenner commented 5 years ago

I realize this problem is because of the way the Response is mocked inside node-fetch, it creates an instance of a custom Blob class, and thus TypeErrors like this occur:

TypeError: Failed to execute 'createImageBitmap' on 'Window': The provided value is not of type '(HTMLImageElement or SVGImageElement or HTMLVideoElement or HTMLCanvasElement or Blob or ImageData or ImageBitmap or OffscreenCanvas)'

The following expectation outright fails.

fetch("url")
  .then(e => e.blob())
  .then(e => {
    expect(e).toBeInstanceOf(Blob);
  });

Is there any way to expose the internal Blob class and override the global Blob so that instanceof checks will work?

jtenner commented 5 years ago

This is fixed on the master branch already

Mingling94 commented 4 years ago

Is this actually fixed? If I give fetchMock

        const body = "someData"
        fetchMock.mockResponseOnce(body);
        const blob = new Blob([body], {type: 'text/plain;charset=utf-8'});
        // do fetch and compare Blobs

I get:

    expect(received).toEqual(expected)

    Expected value to equal:
      {"payload": {Symbol(impl): {"_buffer": {"data": [115, 111, 109, 101, 68, 97, 116, 97], "type": "Buffer"}, "type": "text/plain;charset=utf-8", Symbol(wrapper): [Circular]}}
    Received:
      {"payload": {Symbol(type): "text/plain;charset=utf-8", Symbol(buffer): {"data": [115, 111, 109, 101, 68, 97, 116, 97], "type": "Buffer"}}

    Difference:

    - Expected
    + Received

    @@ -1,9 +1,9 @@
      Object {
        "payload": Blob {
    -     Symbol(impl): BlobImpl {
    -       "_buffer": Object {
    +     Symbol(type): "text/plain;charset=utf-8",
    +     Symbol(buffer): Object {
            "data": Array [
              115,
              111,
              109,
              101,
    @@ -12,14 +12,11 @@
              116,
              97,
            ],
            "type": "Buffer",
          },
    -       "type": "text/plain;charset=utf-8",
    -       Symbol(wrapper): [Circular],
        },
    -   },

I probably need to read up on Blobs to understand this difference here. Do you know how I could make assertions of the Blob type that jest-fetch-mock returns from response.blob()?