expo / sentry-expo

MIT License
202 stars 83 forks source link

Fails to import on test with Jest #327

Closed dwkoogt closed 1 year ago

dwkoogt commented 1 year ago

Summary

I have both @sentry/.* and sentry-expo defined in transformIgnorePatterns.

● Test suite failed to run

TypeError: Cannot read properties of undefined (reading 'Bare')

> 11 | import * as Sentry from 'sentry-expo';

at Object.Bare (node_modules/sentry-expo/src/sentry.ts:18:82)
at Object.require (node_modules/sentry-expo/src/index.ts:1:1)

Managed or bare workflow? If you have ios/ or android/ directories in your project, the answer is bare!

managed

What platform(s) does this occur on?

iOS

SDK Version (managed workflow only)

48

Environment

expo-env-info 1.0.5 environment info: System: OS: macOS 13.0.1 Shell: 5.8.1 - /bin/zsh Binaries: Node: 16.14.2 - ~/.nvm/versions/node/v16.14.2/bin/node Yarn: 1.22.15 - ~/.nvm/versions/node/v16.14.2/bin/yarn npm: 8.5.0 - ~/.nvm/versions/node/v16.14.2/bin/npm Managers: CocoaPods: 1.11.3 - /Users/dwkoogt/.rbenv/shims/pod SDKs: iOS SDK: Platforms: DriverKit 22.1, iOS 16.1, macOS 13.0, tvOS 16.1, watchOS 9.1 IDEs: Xcode: 14.1/14B47b - /usr/bin/xcodebuild npmPackages: expo: ~48.0.6 => 48.0.6 react: 18.2.0 => 18.2.0 react-dom: 18.2.0 => 18.2.0 react-native: 0.71.3 => 0.71.3 react-native-web: ~0.18.9 => 0.18.12 npmGlobalPackages: eas-cli: 3.7.2 expo-cli: 6.0.8 Expo Workflow: bare

Reproducible demo or steps to reproduce from a blank project

Just include Sentry in the App.

import React from 'react';
import App from './App';

import renderer from 'react-test-renderer';

it('renders without crashing', () => {
  const rendered = renderer.create(<App />).toJSON();
  expect(rendered).toMatchSnapshot();
});
dwkoogt commented 1 year ago

I found the problem. I had a mock for expo-constants and didn't have everything needed in it.

conorstrejcek commented 1 year ago

@dwkoogt I'm running into this now, and I did have to update something in my expo-constants mock, but that did not resolve the issue for me. Which values did you have to set in your mock?

conorstrejcek commented 1 year ago

@dwkoogt I ended up figuring it out from looking through the sentry-expo source, I needed to export the ExecutionEnvironment enum from the expo-constants mock:

export enum ExecutionEnvironment {
  Bare = "bare",
  Standalone = "standalone",
  StoreClient = "storeClient",
}