Path: blob/master/node_modules/atomic-sleep/test.js
3965 views
'use strict'1const test = require('tape')2const sleep = require('.')34test('blocks event loop for given amount of milliseconds', ({ is, end }) => {5const now = Date.now()6setTimeout(() => {7const delta = Date.now() - now8const fuzzyDelta = Math.floor(delta / 10) * 10 // allow up to 10ms of execution lag9is(fuzzyDelta, 1000)10end()11}, 100)12sleep(1000)13})1415if (typeof BigInt !== 'undefined') {1617test('allows ms to be supplied as a BigInt number', ({ is, end }) => {18const now = Date.now()19setTimeout(() => {20const delta = Date.now() - now21const fuzzyDelta = Math.floor(delta / 10) * 10 // allow up to 10ms of execution lag22is(fuzzyDelta, 1000)23end()24}, 100)25sleep(BigInt(1000)) // avoiding n notation as this will error on legacy node/browsers26})2728}2930test('throws range error if ms less than 0', ({ throws, end }) => {31throws(() => sleep(-1), RangeError('sleep: ms must be a number that is greater than 0 but less than Infinity'))32end()33})3435test('throws range error if ms is Infinity', ({ throws, end }) => {36throws(() => sleep(Infinity), RangeError('sleep: ms must be a number that is greater than 0 but less than Infinity'))37end()38})3940test('throws range error if ms is not a number or bigint', ({ throws, end }) => {41throws(() => sleep('Infinity'), TypeError('sleep: ms must be a number'))42throws(() => sleep('foo'), TypeError('sleep: ms must be a number'))43throws(() => sleep({a: 1}), TypeError('sleep: ms must be a number'))44throws(() => sleep([1,2,3]), TypeError('sleep: ms must be a number'))45end()46})4748