Parse-SDK-JS icon indicating copy to clipboard operation
Parse-SDK-JS copied to clipboard

jest tests break if parse module is imported in a jsdom environment

Open dr-skot opened this issue 3 years ago • 1 comments

New Issue Checklist

Issue Description

In a jsdom environment, any test that directly or indirectly imports parse (or parse/node) gets terminated due to an UnhandledPromiseRejection. Mocking with jest.mock doesn't help.

The error message is "ReferenceError: indexedDB is not defined"

Steps to reproduce

Run this test:

/*
 * @jest-environment jsdom
 */
import Parse from 'parse'; // or 'parse/node'
jest.mock('parse'); // or don't, it doesn't make a difference

describe('a test', () => {
  it("shouldn't break just because we import the parse module", () => {
    expect(1).toBe(1);
  });
});

Actual Outcome

node:internal/process/promises:246
          triggerUncaughtException(err, true /* fromPromise */);
          ^

[UnhandledPromiseRejection: This error originated either by throwing inside of an async 
function without a catch block, or by rejecting a promise which was not handled with .catch(). 
The promise rejected with the reason "ReferenceError: indexedDB is not defined".] {
  code: 'ERR_UNHANDLED_REJECTION'
}

Process finished with exit code 1

Expected Outcome

Test passed (expect(1).toBe(1)).

Notes

Outside of a jsdom environment, this error doesn't happen. (You can verify this by running the same test without the @jest-environment decorator at the top.)

So maybe the parse SDK is checking for window or document, or something else that jsdom provides, and then expecting something else that jsdom doesn't provide?

But the issue also occurs if you import parse/node instead of parse—ie, when the SDK shouldn't be expecting a browser environment.

Environment

(not a server issue, but...) Server

  • Parse Server version: 4.5.0
  • Operating system: MacOS 12.4
  • Local or remote host (AWS, Azure, Google Cloud, Heroku, Digital Ocean, etc): back4app.com

(not a db issue, but...) Database

  • System (MongoDB or Postgres): MongoDB
  • Database version: 3.6
  • Local or remote host (MongoDB Atlas, mLab, AWS, Azure, Google Cloud, etc): back4app.com

Client

  • Parse JS SDK version: 3.4.2

Logs

See Actual Outcome above

dr-skot avatar Jul 01 '22 16:07 dr-skot

Thanks for opening this issue!

  • 🚀 You can help us to fix this issue faster by opening a pull request with a failing test. See our Contribution Guide for how to make a pull request, or read our New Contributor's Guide if this is your first time contributing.

Closing via https://github.com/parse-community/Parse-SDK-JS/pull/1569

dplewis avatar Jan 27 '23 20:01 dplewis