javascriptunit-testingfetchsinonreadable

Mock ReadableStream


Consider following code:

fetch("/").then(response => {
  const reader = response.body.getReader();
  const decoder = new TextDecoder();

  let res = 0;

  return reader.read().then(function processResult(result) {
    if (result.done) {
      return res;
    }

    const part = decoder.decode(result.value, { stream: true });
    
    res += part.length;

    return reader.read().then(processResult);
  });
}).then(res => console.log(res));

Now I want to test it. I'm mocking fetch to return fake response that should provide some reader. I want that reader to return 2 portions of data (see pieces array):

import { stub } from "sinon";

const pieces = [
  new Uint8Array([65, 98, 99, 32, 208]), // "Abc " and first byte of "й"
  new Uint8Array([185, 209, 139, 209, 141]), // Second byte of "й" and "ыэ"
];

const fetchStub = stub(window, "fetch");

fetchStub.returns(Promise.resolve({
  body: {
    getReader() {
      // What's here?
    },
  },
}));

Is there something I can simply write in getReader or I should fully mock it like I do with fetch?


Solution

  • Mocked it manually:

    fetchStub = stub(window, "fetch");
    
    fetchStub.returns(Promise.resolve({
      body: {
        getReader() {
          let i = 0;
    
          return {
            read() {
              return Promise.resolve(
                i < pieces.length
                  ? { value: pieces[i++], done: false }
                  : { value: undefined, done: true }
              );
            },
          };
        },
      },
    }));