Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
renovate
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
GitHub Mirror
Renovate Bot
renovate
Commits
58fe0bdc
Unverified
Commit
58fe0bdc
authored
1 year ago
by
Sergei Zharinov
Committed by
GitHub
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
refactor(date): Use Jest fake timers (#21631)
parent
62d051f2
Loading
Loading
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
lib/util/date.spec.ts
+30
-23
30 additions, 23 deletions
lib/util/date.spec.ts
with
30 additions
and
23 deletions
lib/util/date.spec.ts
+
30
−
23
View file @
58fe0bdc
import
{
DateTime
}
from
'
luxon
'
;
import
{
getElapsedDays
,
getElapsedHours
,
...
...
@@ -5,34 +6,40 @@ import {
getElapsedMs
,
}
from
'
./date
'
;
const
ONE_MINUTE_MS
=
60
*
1000
;
const
ONE_HOUR_MS
=
60
*
ONE_MINUTE_MS
;
const
ONE_DAY_MS
=
24
*
ONE_HOUR_MS
;
describe
(
'
util/date
'
,
()
=>
{
const
Jan1
=
new
Date
(
new
Date
().
getFullYear
(),
0
,
1
);
const
t0
=
DateTime
.
fromISO
(
'
2020-10-10
'
);
beforeAll
(()
=>
{
jest
.
useFakeTimers
();
});
beforeEach
(()
=>
{
jest
.
setSystemTime
(
t0
.
toMillis
());
});
it
(
'
returns elapsed days
'
,
()
=>
{
const
elapsedDays
=
Math
.
floor
(
(
new
Date
().
getTime
()
-
new
Date
(
Jan1
).
getTime
())
/
ONE_DAY_MS
);
expect
(
getElapsedDays
(
Jan1
.
toDateString
())).
toBe
(
elapsedDays
);
afterAll
(()
=>
{
jest
.
useRealTimers
();
});
it
(
'
returns elapsed minutes
'
,
()
=>
{
const
elapsedMinutes
=
Math
.
floor
(
(
new
Date
().
getTime
()
-
new
Date
(
Jan1
).
getTime
())
/
ONE_MINUTE_MS
);
expect
(
getElapsedMinutes
(
new
Date
(
Jan1
))).
toBe
(
elapsedMinutes
);
describe
(
'
getElapsedDays
'
,
()
=>
{
it
(
'
returns elapsed days
'
,
()
=>
{
const
t
=
t0
.
minus
({
days
:
42
});
expect
(
getElapsedDays
(
t
.
toISO
()
!
)).
toBe
(
42
);
});
});
describe
(
'
getElapsedMinutes
'
,
()
=>
{
it
(
'
returns elapsed minutes
'
,
()
=>
{
const
t
=
t0
.
minus
({
minutes
:
42
});
expect
(
getElapsedMinutes
(
t
.
toJSDate
())).
toBe
(
42
);
});
});
describe
(
'
getElapsedHours
'
,
()
=>
{
it
(
'
returns elapsed hours
'
,
()
=>
{
const
elapsedHours
=
Math
.
floor
(
(
new
Date
().
getTime
()
-
new
Date
(
Jan1
).
getTime
())
/
ONE_HOUR_MS
);
expect
(
getElapsedHours
(
Jan1
.
toISOString
())).
toBe
(
elapsedHours
);
// ISOstring
expect
(
getElapsedHours
(
Jan1
)).
toBe
(
elapsedHours
);
// JS Date
const
t
=
t0
.
minus
({
hours
:
42
});
expect
(
getElapsedHours
(
t
.
toISO
()
!
)).
toBe
(
42
);
// ISOstring
expect
(
getElapsedHours
(
t
.
toJSDate
())).
toBe
(
42
);
// JS Date
});
it
(
'
returns zero when date passed is invalid
'
,
()
=>
{
...
...
@@ -40,10 +47,10 @@ describe('util/date', () => {
});
});
describe
(
'
getElapsedM
illisecond
s
'
,
()
=>
{
describe
(
'
getElapsedMs
'
,
()
=>
{
it
(
'
returns elapsed time in milliseconds
'
,
()
=>
{
const
elapsedMs
=
new
Date
().
getTime
()
-
new
Date
(
Jan1
).
getTime
(
);
expect
(
getElapsedMs
(
Jan1
.
toISO
String
())).
toBe
(
elapsedMs
);
const
t
=
t0
.
minus
({
milliseconds
:
42
}
);
expect
(
getElapsedMs
(
t
.
toISO
()
!
)).
toBe
(
42
);
});
});
});
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment