ReferenceError: ReadableStream is not defined while running jest tests
Asked Answered
P

0

8

My web app reads local files using a custom ReadableStream (in-order that I won't need to have a max file size that the platform supports) and it works great, both for correctly formatted files and error files. My app is built using React-Redux-Saga.

I'm now attempting to add limited e2e tests to my code to test the state management. I'm testing that when sending redux actions the state updates correctly. I'm testing the saga's using the package redux-saga-tester.

While running the jest client tests that read local files using the ReadableStream I built, I got an error ReferenceError: ReadableStream is not defined. What I understood is that the jsdom environment that jest uses is missing the ReadableStream implementation. I than added the web-streams-polyfill package to polyfill the ReadableStream class.

Now the validate file test is passing as in the browser:

test('Select and validate log file', async () => {
  const testFile = testFileBuilder.valid();
  storeTester.dispatch(fileSelected(testFile));
  await storeTester.waitFor(FILE_VALIDATED);
  const state = storeTester.getState().file;
  expect(state.fileValidated).toBeTruthy();
});

My problem is that when I run a second validation test with an invalid file the test passes but prints an error to the console.

The test:

test('Select an invalid file - JSON error', async () => {
    const testFile = testFileBuilder.errorJSON();
    storeTester.dispatch(fileSelected(testFile));
    await storeTester.waitFor(FILE_ERROR);
    const state = storeTester.getState().file;
    expect(state.fileValidated).toBeFalsy();
    expect(state.fileError).toBeTruthy();
  });

The error that is printed after the test completes successfully:

e.error node_modules/jsdom/lib/jsdom/virtual-console.js:29
    Error: Error: connect ECONNREFUSED 127.0.0.1:80
        at Object.dispatchError (/home/noams/dev/web/visual-log-viewer/client/node_modules/jsdom/lib/jsdom/living/xhr-utils.js:65:19)
        at Request.client.on.err (/home/noams/dev/web/visual-log-viewer/client/node_modules/jsdom/lib/jsdom/living/xmlhttprequest.js:676:20)
        at Request.emit (events.js:194:15)
        at Request.onRequestError (/home/noams/dev/web/visual-log-viewer/client/node_modules/request/request.js:881:8)
        at ClientRequest.emit (events.js:189:13)
        at Socket.socketErrorListener (_http_client.js:392:9)
        at Socket.emit (events.js:189:13)
        at emitErrorNT (internal/streams/destroy.js:82:8)
        at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
        at process._tickCallback (internal/process/next_tick.js:63:19) undefined

Any subsequent file validation tests fail.

I'd appreciate any advice of how to tackle this issue.

Preamplifier answered 23/5, 2019 at 6:51 Comment(2)
Share code, what you have done so far??Diep
Thanks TUSHAR, I added code and clarifications.Preamplifier

© 2022 - 2024 — McMap. All rights reserved.