Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
8c01e68f
Commit
8c01e68f
authored
Feb 05, 2021
by
Paul Slaughter
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update FE guide docs for fake date
- This is now faked by default
parent
fbaa3c90
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
29 additions
and
3 deletions
+29
-3
doc/development/testing_guide/frontend_testing.md
doc/development/testing_guide/frontend_testing.md
+29
-3
No files found.
doc/development/testing_guide/frontend_testing.md
View file @
8c01e68f
...
...
@@ -651,15 +651,41 @@ Non-determinism is the breeding ground for flaky and brittle specs. Such specs e
### Faking `Date` for determinism
Consider using
`useFakeDate`
to ensure a consistent value is returned with every
`new Date()`
or
`Date.now()`
.
`Date`
is faked by default in our Jest environment. This means every call to
`Date()`
or
`Date.now()`
returns a fixed deterministic value.
If you really need to change the default fake date, you can call
`useFakeDate`
within any
`describe`
block, and
the date will be replaced for that specs within that
`describe`
context only:
```
javascript
import
{
useFakeDate
}
from
'
helpers/fake_date
'
;
describe
(
'
cool/component
'
,
()
=>
{
useFakeDate
();
// Default fake `Date`
const
TODAY
=
new
Date
();
// ...
// NOTE: `useFakeDate` cannot be called during test execution (i.e. inside `it`, `beforeEach`, `beforeAll`, etc.).
describe
(
"
on Ada Lovelace's Birthday
"
,
()
=>
{
useFakeDate
(
1815
,
11
,
10
)
it
(
'
Date is no longer default
'
,
()
=>
{
expect
(
new
Date
()).
not
.
toEqual
(
TODAY
);
});
});
it
(
'
Date is still default in this scope
'
,
()
=>
{
expect
(
new
Date
()).
toEqual
(
TODAY
)
});
})
```
Similarly, if you really need to use the real
`Date`
class, then you can import and call
`useRealDate`
within any
`describe`
block:
```
javascript
import
{
useRealDate
}
from
'
helpers/fake_date
'
;
// NOTE: `useRealDate` cannot be called during test execution (i.e. inside `it`, `beforeEach`, `beforeAll`, etc.).
describe
(
'
with real date
'
,
()
=>
{
useRealDate
();
});
```
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment