testing-library / react-hooks-testing-library

🐏 Simple and complete React hooks testing utilities that encourage good testing practices.
https://react-hooks-testing-library.com
MIT License
5.25k stars 230 forks source link

jsdom environment issues #963

Closed ljrahn closed 9 months ago

ljrahn commented 1 year ago

Hello team. I am facing 2 issues, and maybe they should be created in separate issues, but I will see if this is okay. I am encountering issues when trying to use jsdom environment in jest. I reproduce the issues in the following repo and briefly explain the issues in the README: https://github.com/ljrahn/test-hooks.

Issue 1 - fetch is not defined:

it seems that when using jsdom environment, fetch is not defined and we need to install a separate fetch dependency then set in the globals. I would expect that jsdom would already have fetch defined no? The README explains how to workaround the issue.

Issue 2 - issues transpiling some dependencies when using jsdom environment but does not occur when using node environment.

I can reproduce the issue with firebase-admin (in my actual full project i use firebase admin in my tests to clean them up so its kind of a necessity). i end up hitting this error when trying to import:

Test suite failed to run

    Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation

    Details:

    /Users/lucasrahn/batcave/projects/lendus/test-hooks/node_modules/jose/dist/browser/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export { compactDecrypt } from './jwe/compact/decrypt.js';
                                                                                      ^^^^^^

    SyntaxError: Unexpected token 'export'

      2 | import useCounter from "./useCounter";
      3 | import { act } from "react-test-renderer";
    > 4 | import { getAuth } from "firebase-admin/auth";
        | ^
      5 |
      6 | test("should use counter", () => {
      7 |   const { result } = renderHook(() => useCounter());

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1496:14)
      at Object.<anonymous> (node_modules/jwks-rsa/src/utils.js:1:116)
      at Object.<anonymous> (node_modules/jwks-rsa/src/JwksClient.js:2:33)
      at Object.<anonymous> (node_modules/jwks-rsa/src/index.js:1:126)
      at Object.<anonymous> (node_modules/firebase-admin/lib/utils/jwt.js:22:14)
      at Object.<anonymous> (node_modules/firebase-admin/lib/auth/token-verifier.js:23:15)
      at Object.<anonymous> (node_modules/firebase-admin/lib/auth/base-auth.js:25:26)
      at Object.<anonymous> (node_modules/firebase-admin/lib/auth/tenant-manager.js:23:21)
      at Object.<anonymous> (node_modules/firebase-admin/lib/auth/auth.js:22:26)
      at Object.<anonymous> (node_modules/firebase-admin/lib/auth/index.js:26:16)
      at Object.require (test/useCounter.test.jsx:4:1)

we can fix this issue by changing the testEnvironment to "node" in jest.config.js, but obviously our hook wont work then because it requires some sort of dom environment in order to create elements.

any help would be greatly appreciated! thank you

mpeyper commented 9 months ago

Hi @ljrahn,

Sorry, I missed the notification for this issue. Did you get it sorted in the end?

Setting up the test environment is not really in our scope so I would suggest seeking help from the jest/jsdom folks instead.