Skip to content

Getting "ReferenceError: jest is not defined" when on userEvent touch events #1568

@JoseLion

Description

@JoseLion

Describe the bug

I'm getting a "ReferenceError: jest is not defined" error whenever I use a touch event method of userEvent. I'm not testing with Jest--I'm using Mocha.js and react-native-testing-mocks (a package of my authoring). It seems the problem is that this function uses jest.fn() to mock a couple of properties. A workaround to the problem is to add a replacement of jest.fn to the global object:

function noop(): void {
  // do nothing...
}

Object.assign(global, { jest: { fn: () => noop } });

According to the documentation, RNTL is "tested to work with Jest, but it should work with other test runners as well." However, the library seems highly coupled to Jest. Another example of this is that when I use the workaround, I get a warning about not using fake timers, even when I have RNTL_SKIP_AUTO_DETECT_FAKE_TIMERS=true in my environment variables.

Error stack trace

TypeError: event.persist is not a function
      at handler (Pressability.js:478:15)
      at callback (node_modules\@testing-library\react-native\src\user-event\utils\dispatch-event.ts:23:5)
      at C:\Repos\react-native-testing-mocks\node_modules\@testing-library\react-native\src\act.ts:31:24
      at actImplementation (node_modules\react\cjs\react.development.js:2512:16)
      at C:\Repos\react-native-testing-mocks\node_modules\@testing-library\react-native\src\act.ts:30:25
      at dispatchEvent (node_modules\@testing-library\react-native\src\user-event\utils\dispatch-event.ts:22:11)
      at emitPressablePressEvents (node_modules\@testing-library\react-native\src\user-event\press\press.ts:85:16)
      at async basePress (node_modules\@testing-library\react-native\src\user-event\press\press.ts:64:5)
      at async basePress (node_modules\@testing-library\react-native\src\user-event\press\press.ts:73:3)
      at async basePress (node_modules\@testing-library\react-native\src\user-event\press\press.ts:73:3)
      at async Object.press (node_modules\@testing-library\react-native\src\user-event\press\press.ts:22:3)
      at async wrapAsync (node_modules\@testing-library\react-native\src\helpers\wrap-async.ts:22:22)

Expected behavior

The library should not be so tightly coupled to Jest so it can be used with other test runners. The usage of jest.fn() can be easily replaced with something like a noop() function.

Steps to Reproduce

  1. Setup test without Jest. For example, you can use Mocha.js together with react-native-testing-mocks.
  2. Create a simple test where you can use userEvent.press(..)
  3. The error shows when the test runs

Screenshots

N/A

Versions

npmPackages:
@testing-library/react-native: ^12.4.3 => 12.4.3
react: 18.2.0 => 18.2.0
react-native: ^0.73.5 => 0.73.5
react-test-renderer: ^18.2.0 => 18.2.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions