Skip to content

Commit c7ebb2f

Browse files
Add sleep (#22)
1 parent cb8040d commit c7ebb2f

File tree

7 files changed

+56
-1
lines changed

7 files changed

+56
-1
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313

1414
### Removed
1515

16+
## [1.4.0] - 2021-10-26
17+
18+
### Added
19+
20+
- `sleep`
21+
22+
### Changed
23+
24+
### Removed
25+
1626
## [1.3.0] - 2021-10-20
1727

1828
### Added

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ npm i @techmmunity/utils
5151
| function | description |
5252
| ---------- | ----------------------------------------------- |
5353
| `cleanObj` | Remove undefined and null values from an object |
54+
| `sleep` | Await the amount of time specified (in seconds) |
5455

5556
## `get*`
5657

jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module.exports = {
1313
testEnvironment: "node",
1414
moduleDirectories: ["node_modules", "src"],
1515
resetMocks: true,
16+
testTimeout: 15000,
1617
coverageThreshold: {
1718
global: {
1819
statements: 99.17,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@techmmunity/utils",
3-
"version": "1.3.0",
3+
"version": "1.4.0",
44
"main": "index.js",
55
"types": "index.d.ts",
66
"license": "Apache-2.0",

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
export * from "./lib/clean-obj";
10+
export * from "./lib/sleep";
1011

1112
/**
1213
* ---------------------------------------------------------------------------

src/lib/sleep/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* Await the amount of time specified (in seconds)
3+
*/
4+
export const sleep = (seconds: number): Promise<void> =>
5+
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
6+
new Promise(resolve => setTimeout(resolve, seconds * 1000));

src/tests/sleep.spec.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
*
3+
* True
4+
*
5+
*/
6+
7+
import { isBetween } from "../lib/is-between";
8+
import { sleep } from "../lib/sleep";
9+
10+
describe("sleep", () => {
11+
it("with 1 second", async () => {
12+
const timeBefore = Date.now();
13+
14+
await sleep(1);
15+
16+
const timeAfter = Date.now();
17+
18+
const offset = timeAfter - timeBefore;
19+
20+
// Cannot test the exact time, so test a range
21+
expect(isBetween(offset, 1000, 1010)).toBeTruthy();
22+
});
23+
24+
it("with 10 seconds", async () => {
25+
const timeBefore = Date.now();
26+
27+
await sleep(10);
28+
29+
const timeAfter = Date.now();
30+
31+
const offset = timeAfter - timeBefore;
32+
33+
// Cannot test the exact time, so test a range
34+
expect(isBetween(offset, 10000, 10010)).toBeTruthy();
35+
});
36+
});

0 commit comments

Comments
 (0)