Skip to content

Commit f6c1a86

Browse files
committed
mock Date.now so persistance tests don't rely on actual time (needed for updatedAt and snapshot testing)
1 parent d179454 commit f6c1a86

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

packages/gatsby/src/redux/__tests__/__snapshots__/index.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ Object {
6464
"path": "/my-sweet-new-page/",
6565
"pluginCreatorId": "",
6666
"pluginCreator___NODE": "",
67-
"updatedAt": 1606471951096,
67+
"updatedAt": 1,
6868
},
6969
},
7070
"pendingPageDataWrites": Object {

packages/gatsby/src/redux/__tests__/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ describe(`redux db`, () => {
9191
const initialComponentsState = _.cloneDeep(store.getState().components)
9292

9393
beforeEach(() => {
94+
// mock Date.now so Date.now() doesn't change in between tests
95+
const RealDateNow = Date.now
96+
let DateNowCallCount = 0
97+
// simulate passage of time by increasing call counter (instead of actual time value)
98+
Date.now = jest.fn(() => ++DateNowCallCount)
99+
94100
store.dispatch(
95101
createPage(
96102
{
@@ -107,6 +113,8 @@ describe(`redux db`, () => {
107113
)
108114
)
109115

116+
Date.now = RealDateNow
117+
110118
writeToCache.mockClear()
111119
mockWrittenContent.clear()
112120
})
@@ -133,6 +141,8 @@ describe(`redux db`, () => {
133141
// make sure data was read and is not the same as our clean redux state
134142
expect(data.components).not.toEqual(initialComponentsState)
135143

144+
// we ignore pages in this assertion, because it contains `updatedAt` timestamp field that
145+
// will change depending on time tests were executed
136146
expect(data).toMatchSnapshot()
137147
})
138148

0 commit comments

Comments
 (0)