Skip to content

Commit

Permalink
Add sleep (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
henriqueleite42 authored Oct 26, 2021
1 parent cb8040d commit c7ebb2f
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 1 deletion.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Removed

## [1.4.0] - 2021-10-26

### Added

- `sleep`

### Changed

### Removed

## [1.3.0] - 2021-10-20

### Added
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ npm i @techmmunity/utils
| function | description |
| ---------- | ----------------------------------------------- |
| `cleanObj` | Remove undefined and null values from an object |
| `sleep` | Await the amount of time specified (in seconds) |

## `get*`

Expand Down
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = {
testEnvironment: "node",
moduleDirectories: ["node_modules", "src"],
resetMocks: true,
testTimeout: 15000,
coverageThreshold: {
global: {
statements: 99.17,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@techmmunity/utils",
"version": "1.3.0",
"version": "1.4.0",
"main": "index.js",
"types": "index.d.ts",
"license": "Apache-2.0",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

export * from "./lib/clean-obj";
export * from "./lib/sleep";

/**
* ---------------------------------------------------------------------------
Expand Down
6 changes: 6 additions & 0 deletions src/lib/sleep/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Await the amount of time specified (in seconds)
*/
export const sleep = (seconds: number): Promise<void> =>
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
new Promise(resolve => setTimeout(resolve, seconds * 1000));
36 changes: 36 additions & 0 deletions src/tests/sleep.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
*
* True
*
*/

import { isBetween } from "../lib/is-between";
import { sleep } from "../lib/sleep";

describe("sleep", () => {
it("with 1 second", async () => {
const timeBefore = Date.now();

await sleep(1);

const timeAfter = Date.now();

const offset = timeAfter - timeBefore;

// Cannot test the exact time, so test a range
expect(isBetween(offset, 1000, 1010)).toBeTruthy();
});

it("with 10 seconds", async () => {
const timeBefore = Date.now();

await sleep(10);

const timeAfter = Date.now();

const offset = timeAfter - timeBefore;

// Cannot test the exact time, so test a range
expect(isBetween(offset, 10000, 10010)).toBeTruthy();
});
});

0 comments on commit c7ebb2f

Please sign in to comment.