Skip to content

Commit 2c3363e

Browse files
committed
Provide only allowed node environments to run logs
1 parent 87024fd commit 2c3363e

File tree

2 files changed

+48
-31
lines changed

2 files changed

+48
-31
lines changed

src/index.test.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,21 @@ import { act, renderHook } from '@testing-library/react'
33
import { useEffect, useState } from 'react'
44

55
describe('useLog', () => {
6+
const OLD_ENV = process.env
67
const consoleLog = jest.spyOn(console, 'log').mockImplementation(() => {})
78
const consoleGroup = jest.spyOn(console, 'group').mockImplementation(() => {})
89

910
beforeEach(() => {
1011
jest.useFakeTimers()
12+
jest.resetModules()
13+
process.env = { ...OLD_ENV }
14+
process.env.NODE_ENV = 'dev'
1115
})
1216

1317
afterEach(() => {
1418
jest.runOnlyPendingTimers()
1519
jest.useRealTimers()
20+
process.env = OLD_ENV
1621
})
1722

1823
it('exists', () => {
@@ -163,4 +168,12 @@ describe('useLog', () => {
163168
expect(consoleLog).toBeCalledTimes(11)
164169
expect(consoleGroup).toBeCalledTimes(6)
165170
})
171+
172+
it('does not render anything in production', () => {
173+
process.env.NODE_ENV = 'production'
174+
175+
const { result } = renderHook(useLog)
176+
renderHook(() => result.current.log('Test'))
177+
expect(consoleLog).not.toBeCalled()
178+
})
166179
})

src/index.tsx

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const CSS_COMPONENT = 'color: DodgerBlue'
44
const CSS_CHANGE = 'color: green; font-weight: bold;'
55
const CSS_SUB_VALUE = 'color: SlateGray; font-weight: thin;'
66

7+
const ALLOWED_NODE_ENVS = ['dev', 'development']
8+
79
export interface UseLogReturn {
810
log: <T>(value: T) => void
911
}
@@ -72,44 +74,46 @@ export function useLog(): UseLogReturn {
7274
const clonedValue = JSON.parse(JSON.stringify(value))
7375
const prevValueRef = useRef<T>()
7476

75-
return (() => {
76-
const isUnmounting = useRef(false)
77-
useEffect(() => {
78-
return () => {
79-
isUnmounting.current = true
80-
}
81-
}, [])
77+
if (ALLOWED_NODE_ENVS.includes(process.env.NODE_ENV ?? '')) {
78+
return (() => {
79+
const isUnmounting = useRef(false)
80+
useEffect(() => {
81+
return () => {
82+
isUnmounting.current = true
83+
}
84+
}, [])
8285

83-
useEffect(() => {
84-
print({
85-
label: 'On mount',
86-
value: clonedValue,
87-
type: PrintTypes.Mount,
88-
})
86+
useEffect(() => {
87+
print({
88+
label: 'On mount',
89+
value: clonedValue,
90+
type: PrintTypes.Mount,
91+
})
8992

90-
prevValueRef.current = value
93+
prevValueRef.current = value
9194

92-
return () => {
95+
return () => {
96+
print({
97+
label: 'On unmount',
98+
value: clonedValue,
99+
type: PrintTypes.Unmount,
100+
prevValue: prevValueRef.current,
101+
})
102+
}
103+
}, [])
104+
105+
useEffect(() => {
93106
print({
94-
label: 'On unmount',
107+
label: 'On change',
95108
value: clonedValue,
96-
type: PrintTypes.Unmount,
109+
type: PrintTypes.Change,
97110
prevValue: prevValueRef.current,
98111
})
99-
}
100-
}, [])
101-
102-
useEffect(() => {
103-
print({
104-
label: 'On change',
105-
value: clonedValue,
106-
type: PrintTypes.Change,
107-
prevValue: prevValueRef.current,
108-
})
109-
110-
prevValueRef.current = value
111-
}, [value])
112-
})()
112+
113+
prevValueRef.current = value
114+
}, [value])
115+
})()
116+
}
113117
}
114118

115119
return { log }

0 commit comments

Comments
 (0)