`globalThis.jest` trap object tricks other libraries into non-native behavior and failing to load
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 45/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- babel, react-native, typescript
- Lĩnh vực
- developer-experience, testing
Hướng nghiên cứu
Start by reading packages/runtime/src/jest-mock.ts and its call site in initialize.ts, then reproduce with the linked repo to watch typeof globalThis.jest send react-native-reanimated down its IS_JEST path in src/common/constants/platform.ts. Weigh the three suggestions: the Babel-preset route lives in @react-native-harness/babel-preset, the error-hint route in the test runner. Done means a test importing react-native-reanimated loads the native module and passes, with no fake jest global visible to libraries.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
What happens
Any test file whose imports reach react-native-reanimated fails before a single test is registered:
● Test suite failed to run
[Reanimated] `setCSSEventHandler` is not available in JSReanimated.
at setCSSEventHandler (…)
at initializeReanimatedModule (…)
Test files that never import Reanimated run fine in the same session.
Why
- Harness's runtime defines
globalThis.jeston the device (packages/runtime/src/jest-mock.ts, called frominitialize.ts, added in #12) as a Proxy that throws when used, to tell people to import fromreact-native-harness. It is defined withwritable: false, configurable: false. - Reanimated and Worklets feature-detect Jest by that global:
(// react-native-reanimated/src/common/constants/platform.ts export const IS_JEST: boolean = typeof globalThis.jest !== 'undefined' || process.env.NODE_ENV === 'test';react-native-worklets/src/platformChecker.tshas the same check.) typeof globalThis.jestis'object'under Harness, so Reanimated pickscreateJSReanimatedModule()over the native module. Its own initialisation then callssetCSSEventHandler, which the JS module does not implement, and the import throws.
So the warning object is taken for Jest itself by a library that only checks
whether the global exists. Because the property is non-configurable, a setup
file cannot remove it.
Workaround
Removing the setupJestMock() call from @react-native-harness/runtime's
initialize (via a package-manager patch) fixes it: Reanimated loads its
native module and the tests pass.
Suggestions
There might be good reasons against any of those, but:
- Catch it at build time. Harness already ships its own Babel preset
(@react-native-harness/babel-preset) that processes test files. It could
flag any use of a bare jest identifier there and fail the bundle with the
same "import from react-native-harness instead" message. Nothing exists at
runtime, so no library can detect anything, and it only looks at test
files. - Improve the error that already happens. Without the global, jest.fn() throws a plain "jest is not defined" error by itself. Harness's test runner could recognise that error and append its hint. It needs no global and no transform, and the wording becomes only slightly less direct.
- Leave it to tooling. A lint rule or a type declaration that marks jest as
unusable in *.harness.ts files would show the mistake in the editor. This
is the weakest of the three, since it only helps people who run the linter
or the type checker.
Happy to test a fix.
System Info
System:
OS: Linux 7.2 Arch Linux
CPU: (24) x64 AMD Ryzen 9 7900X3D 12-Core Processor
Memory: 72.49 GB / 93.44 GB
Shell:
version: 5.9.2
path: /usr/bin/zsh
Binaries:
Node:
version: 24.22.2
path: /usr/bin/node
Yarn: Not Found
npm:
version: 12.2.2
path: /usr/bin/npm
Watchman:
version: 20260727.012849.0
path: /usr/bin/watchman
SDKs:
Android SDK: Not Found
IDEs:
Android Studio: AI-262.10968.63.2622.16730706
Languages:
Java:
version: 21.0.13.0.3
path: /usr/bin/javac
Ruby:
version: 3.4.12
path: /usr/bin/ruby
npmPackages:
"@react-native-community/cli":
installed: 20.2.0
wanted: ^20.1.3
react:
installed: 19.3.0
wanted: 19.3.0
react-native:
installed: 0.88.0
wanted: 0.88.0
npmGlobalPackages:
"*react-native*": Not Found
Android:
hermesEnabled: true
newArchEnabled: true
iOS:
hermesEnabled: Not found
newArchEnabled: Not found
React Native Harness Version
1.7.0
Reproduction
https://github.com/bennouli/harness-reanimated-jest-global-repro
Steps to reproduce
react-native-harness 1.7.0, react-native-reanimated 4.7.1, react-native-worklets 0.13.1. React Native 0.88, Expo SDK 58
Android emulator
A test file that imports anything that imports react-native-reanimated:
import { describe, test, expect } from 'react-native-harness'
import Animated from 'react-native-reanimated'
describe('reanimated', () => {
test('loads', () => {
expect(Animated).toBeDefined()
})
})
Run react-native-harness.
- Ngôn ngữ chính
- TypeScript
- Star
- 341
- Fork
- 25
- Merge trung bình
- 9 giờ 46 phút
- Pull request đã merge (30 ngày)
- 1
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của callstackincubator/react-native-harness
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
-
afterEach hooks run outer suite first, unlike JestCó thể đã có người làm @TheSeydiCharyyev đã nhận 26 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
callstackincubator/react-native-harness#296 · 1 bình luận ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
callstackincubator/react-native-harness#128 · 1 bình luận ·
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 58/100
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 52/100
Tất cả issue của callstackincubator/react-native-harness
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
audacity/audacity.github.io#118 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
crxjs/chrome-extension-tools#1261 ·
-
Resources: Update work of fuzhouCó thể đã có người làm @rmt-svc đã nhận hôm nay. Đang mởresources
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 64/100
railmapgen/rmp-gallery#4245 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 83/100
mike-north/AgentMonitors#717 ·
Maintainer thường phản hồi trong vòng 1 ngày