Skip to content

Commit 78f3a03

Browse files
committed
Mock time in tests
1 parent d9dab87 commit 78f3a03

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed

src/index.test.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import { useLog } from './index'
2+
import * as utils from './utils'
23
import { act, renderHook } from '@testing-library/react'
34
import { useEffect, useState } from 'react'
45

56
describe('useLog', () => {
67
const OLD_ENV = process.env
8+
jest.spyOn(utils, 'getCurrentTime').mockReturnValue('09:38 PM')
79
const consoleLog = jest.spyOn(console, 'log').mockImplementation(() => null)
810
const consoleGroup = jest
911
.spyOn(console, 'group')
1012
.mockImplementation(() => null)
13+
1114
const consoleGroupCollapsed = jest
1215
.spyOn(console, 'groupCollapsed')
1316
.mockImplementation(() => null)
@@ -70,14 +73,14 @@ describe('useLog', () => {
7073
* Set Initial Values
7174
*/
7275
expect(consoleGroup).toBeCalledWith(
73-
`Mount in %c<TestComponent /> %c@ ${new Date().toLocaleTimeString()}`,
76+
'Mount in %c<TestComponent /> %c@ 09:38 PM',
7477
'color: DodgerBlue',
7578
'color: SlateGray; font-weight: thin;',
7679
)
7780
expect(consoleLog).toBeCalledWith(' On mount: null')
7881

7982
expect(consoleGroup).toBeCalledWith(
80-
`Change in %c<TestComponent /> %c@ ${new Date().toLocaleTimeString()}`,
83+
'Change in %c<TestComponent /> %c@ 09:38 PM',
8184
'color: DodgerBlue',
8285
'color: SlateGray; font-weight: thin;',
8386
)
@@ -90,7 +93,7 @@ describe('useLog', () => {
9093
'color: green; font-weight: bold;',
9194
)
9295
expect(consoleGroup).toBeCalledWith(
93-
`Change in %c<TestComponent /> %c@ ${new Date().toLocaleTimeString()}`,
96+
'Change in %c<TestComponent /> %c@ 09:38 PM',
9497
'color: DodgerBlue',
9598
'color: SlateGray; font-weight: thin;',
9699
)
@@ -113,7 +116,7 @@ describe('useLog', () => {
113116
logRerender()
114117
})
115118
expect(consoleGroup).toBeCalledWith(
116-
`Change in %c<TestComponent /> %c@ ${new Date().toLocaleTimeString()}`,
119+
'Change in %c<TestComponent /> %c@ 09:38 PM',
117120
'color: DodgerBlue',
118121
'color: SlateGray; font-weight: thin;',
119122
)
@@ -136,7 +139,7 @@ describe('useLog', () => {
136139
logRerender()
137140
})
138141
expect(consoleGroup).toBeCalledWith(
139-
`Change in %c<TestComponent /> %c@ ${new Date().toLocaleTimeString()}`,
142+
'Change in %c<TestComponent /> %c@ 09:38 PM',
140143
'color: DodgerBlue',
141144
'color: SlateGray; font-weight: thin;',
142145
)
@@ -158,7 +161,7 @@ describe('useLog', () => {
158161
logUnmount()
159162
})
160163
expect(consoleGroup).toBeCalledWith(
161-
`Unmount in %c<TestComponent /> %c@ ${new Date().toLocaleTimeString()}`,
164+
'Unmount in %c<TestComponent /> %c@ 09:38 PM',
162165
'color: DodgerBlue',
163166
'color: SlateGray; font-weight: thin;',
164167
)
@@ -261,7 +264,7 @@ describe('useLog', () => {
261264
expect(consoleGroupCollapsed).toHaveBeenCalled()
262265
// first call, first parameter for group name should exist
263266
expect(consoleGroupCollapsed.mock.calls[0][0]).toBe(
264-
`Mount in %c<TestComponent /> %c@ ${new Date().toLocaleTimeString()}`,
267+
'Mount in %c<TestComponent /> %c@ 09:38 PM',
265268
)
266269
})
267270

src/utils.test.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
import { getGroupLabel, getComponentName, print, getPrinter } from './utils'
1+
import * as utils from './utils'
22
import { _PrintConfig, _PrintTypes, Printer } from './types'
33

4+
const { getGroupLabel, getComponentName, print, getPrinter } = utils
5+
46
describe('utils', () => {
7+
jest.spyOn(utils, 'getCurrentTime').mockReturnValue('09:38 PM')
58
describe('getGroupLabel', () => {
69
it('renders', () => {
7-
expect(getGroupLabel(_PrintTypes.Change)).toEqual(
8-
`Change %c%c@ ${new Date().toLocaleTimeString()}`,
9-
)
10+
expect(getGroupLabel(_PrintTypes.Change)).toEqual('Change %c%c@ 09:38 PM')
1011
})
1112

1213
it('renders with component name', () => {
1314
expect(getGroupLabel(_PrintTypes.Mount, 'TestComponent')).toEqual(
14-
`Mount in %c<TestComponent /> %c@ ${new Date().toLocaleTimeString()}`,
15+
'Mount in %c<TestComponent /> %c@ 09:38 PM',
1516
)
1617
})
1718
})
@@ -66,7 +67,7 @@ describe('utils', () => {
6667
print(printProps)
6768

6869
expect(consoleGroup).toHaveBeenCalledWith(
69-
`Change in %c<SomeComponentName /> %c@ ${new Date().toLocaleTimeString()}`,
70+
'Change in %c<SomeComponentName /> %c@ 09:38 PM',
7071
undefined,
7172
undefined,
7273
)
@@ -79,7 +80,7 @@ describe('utils', () => {
7980
print({ ...printProps, prevValue: 'Some Previous value' })
8081

8182
expect(consoleGroup).toHaveBeenCalledWith(
82-
`Change in %c<SomeComponentName /> %c@ ${new Date().toLocaleTimeString()}`,
83+
'Change in %c<SomeComponentName /> %c@ 09:38 PM',
8384
undefined,
8485
undefined,
8586
)
@@ -119,7 +120,7 @@ describe('utils', () => {
119120

120121
expect(consoleGroup).not.toHaveBeenCalled()
121122
expect(consoleGroupCollapsed).toHaveBeenCalledWith(
122-
`Change in %c<SomeComponentName /> %c@ ${new Date().toLocaleTimeString()}`,
123+
'Change in %c<SomeComponentName /> %c@ 09:38 PM',
123124
undefined,
124125
undefined,
125126
)

src/utils.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import * as utils from './utils'
12
import { Printer, _PrintConfig, _PrintTypes, _SupportedConsole } from './types'
23

3-
export function getCurrentDate(): string {
4+
/* istanbul ignore next */
5+
export function getCurrentTime(): string {
6+
// No need in testing Date module
47
return new Date().toLocaleTimeString()
58
}
69

@@ -12,9 +15,9 @@ export function getGroupLabel(
1215
? `in %c<${String(componentName)} /> `
1316
: '%c'
1417
const typeWrapper = `${String(type)} `
15-
const dateWrapper = `%c@ ${getCurrentDate()}`
18+
const timeWrapper = `%c@ ${utils.getCurrentTime()}`
1619

17-
return `${typeWrapper}${componentNameWrapper}${dateWrapper}`
20+
return `${typeWrapper}${componentNameWrapper}${timeWrapper}`
1821
}
1922

2023
export function getComponentName(): string {

0 commit comments

Comments
 (0)