Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use isTestEnvironment instead of __test__ #294

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
global.__TEST__ = false

import { AppRegistry } from "react-native"
import { App } from "./app/App"
import { name as appName } from "./app.json"
Expand Down
4 changes: 1 addition & 3 deletions src/ambient.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
declare module "" {
global {
const __TEST__: boolean
}
global {}
}

declare function assertNever(val: never): void
3 changes: 2 additions & 1 deletion src/elements/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { animated, Spring } from "react-spring/renderprops-native"
import styled from "styled-components/native"
import { Color, SpacingUnit } from "../../types"
import { useColor } from "../../utils/hooks"
import { isTestEnvironment } from "../../utils/tests/isTestEnvironment"
import { Box, BoxProps } from "../Box"
import { Flex } from "../Flex/Flex"
import { MeasuredView, ViewMeasurements } from "../MeasuredView"
Expand Down Expand Up @@ -192,7 +193,7 @@ export const Button: React.FC<ButtonProps> = ({
This will result in us being able to use getByText over
getAllByText()[0] to select the buttons in the test environment.
*/}
{!__TEST__ && longestText && (
{!isTestEnvironment() && longestText && (
<MeasuredView setMeasuredState={setLongestTextMeasurements}>
<Text color="red" style={textStyle}>
{longestText ? longestText : children}
Expand Down
3 changes: 2 additions & 1 deletion src/elements/MeasuredView/MeasuredView.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useCallback } from "react"
import { ViewStyle, LayoutChangeEvent, useWindowDimensions } from "react-native"
import { isTestEnvironment } from "../../utils/tests/isTestEnvironment"
import { Box } from "../Box"

export interface ViewMeasurements {
Expand All @@ -25,7 +26,7 @@ export const MeasuredView = ({ children, setMeasuredState, show }: Props) => {
setMeasuredState(event.nativeEvent.layout)
}, [])

if (__TEST__) return null
if (isTestEnvironment()) return null

return (
<Box
Expand Down
2 changes: 0 additions & 2 deletions src/setupJest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @ts-expect-error
global.__TEST__ = true
import "@testing-library/react-native/extend-expect"

import mockSafeAreaContext from "react-native-safe-area-context/jest/mock"
Expand Down
3 changes: 2 additions & 1 deletion src/utils/hooks/useTheme.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useContext } from "react"
import { ThemeContext } from "styled-components/native"
import { AllThemesType, THEMES, ThemeType, ThemeWithDarkModeType } from "../../tokens"
import { Color, ColorDSValue } from "../../types"
import { isTestEnvironment } from "../tests/isTestEnvironment"

export const useTheme = (): {
theme: ThemeType | ThemeWithDarkModeType
Expand All @@ -14,7 +15,7 @@ export const useTheme = (): {
// if we are not wrapped in `<Theme>`, if we dev, throw error.
// if we are in prod, we will default to v2 to avoid a crash.
// if we are wrapped, then all good.
if ((__DEV__ || __TEST__) && maybeTheme === undefined) {
if ((__DEV__ || isTestEnvironment()) && maybeTheme === undefined) {
console.error(
"You are trying to use the `Theme` context but you have not wrapped your component/screen with `<Theme>`. Please wrap and try again."
)
Expand Down
3 changes: 3 additions & 0 deletions src/utils/tests/isTestEnvironment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const isTestEnvironment = () => {
return !!process.env.JEST_WORKER_ID
}