Skip to content

Commit

Permalink
Provide only allowed node environments to run logs
Browse files Browse the repository at this point in the history
  • Loading branch information
dolfbarr committed Nov 27, 2022
1 parent 87024fd commit 2c3363e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 31 deletions.
13 changes: 13 additions & 0 deletions src/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@ import { act, renderHook } from '@testing-library/react'
import { useEffect, useState } from 'react'

describe('useLog', () => {
const OLD_ENV = process.env
const consoleLog = jest.spyOn(console, 'log').mockImplementation(() => {})
const consoleGroup = jest.spyOn(console, 'group').mockImplementation(() => {})

beforeEach(() => {
jest.useFakeTimers()
jest.resetModules()
process.env = { ...OLD_ENV }
process.env.NODE_ENV = 'dev'
})

afterEach(() => {
jest.runOnlyPendingTimers()
jest.useRealTimers()
process.env = OLD_ENV
})

it('exists', () => {
Expand Down Expand Up @@ -163,4 +168,12 @@ describe('useLog', () => {
expect(consoleLog).toBeCalledTimes(11)
expect(consoleGroup).toBeCalledTimes(6)
})

it('does not render anything in production', () => {
process.env.NODE_ENV = 'production'

const { result } = renderHook(useLog)
renderHook(() => result.current.log('Test'))
expect(consoleLog).not.toBeCalled()
})
})
66 changes: 35 additions & 31 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const CSS_COMPONENT = 'color: DodgerBlue'
const CSS_CHANGE = 'color: green; font-weight: bold;'
const CSS_SUB_VALUE = 'color: SlateGray; font-weight: thin;'

const ALLOWED_NODE_ENVS = ['dev', 'development']

export interface UseLogReturn {
log: <T>(value: T) => void
}
Expand Down Expand Up @@ -72,44 +74,46 @@ export function useLog(): UseLogReturn {
const clonedValue = JSON.parse(JSON.stringify(value))
const prevValueRef = useRef<T>()

return (() => {
const isUnmounting = useRef(false)
useEffect(() => {
return () => {
isUnmounting.current = true
}
}, [])
if (ALLOWED_NODE_ENVS.includes(process.env.NODE_ENV ?? '')) {
return (() => {
const isUnmounting = useRef(false)
useEffect(() => {
return () => {
isUnmounting.current = true
}
}, [])

useEffect(() => {
print({
label: 'On mount',
value: clonedValue,
type: PrintTypes.Mount,
})
useEffect(() => {
print({
label: 'On mount',
value: clonedValue,
type: PrintTypes.Mount,
})

prevValueRef.current = value
prevValueRef.current = value

return () => {
return () => {
print({
label: 'On unmount',
value: clonedValue,
type: PrintTypes.Unmount,
prevValue: prevValueRef.current,
})
}
}, [])

useEffect(() => {
print({
label: 'On unmount',
label: 'On change',
value: clonedValue,
type: PrintTypes.Unmount,
type: PrintTypes.Change,
prevValue: prevValueRef.current,
})
}
}, [])

useEffect(() => {
print({
label: 'On change',
value: clonedValue,
type: PrintTypes.Change,
prevValue: prevValueRef.current,
})

prevValueRef.current = value
}, [value])
})()

prevValueRef.current = value
}, [value])
})()
}
}

return { log }
Expand Down

0 comments on commit 2c3363e

Please sign in to comment.