Skip to content

ericcornelissen/node-assert-time

Repository files navigation

assert-time

Assert the duration of a (blocking or async) function under test.

Note that blocking tests still block the event loop, it is just asserted that it did not take longer than expected.

Usage

Generic Example

var assertTime = require('@ericcornelissen/assert-time');

var fut = require('./index.js');
var timeout = 100;

// will throw/reject if slow
assertTime(fut, timeout);

// will call `onSlow` if slow, `onTime` otherwise
assertTime(
  fut,
  timeout,
  function onSlow(duration) {
    // ...
  },
  function onTime(duration) {
    // ...
  }
);

With AVA, blocking tests don't cause tests to fail due to a timeout. Async tests do.

var test = require('ava').default; // ^8
var assertTime = require('@ericcornelissen/assert-time');

var fut = require('./index.js');
var timeout = 100;

test('timing test, variant #1', function (t) {
  // Causes an assertion error
  t.doesNotThrow(function () {
    assertTime(fut, timeout);
  });
});

test('timing test, variant #2', function (t) {
  // Causes a test error
  assertTime(fut, timeout);
  t.pass();
});

With tape, blocking tests don't cause tests to fail due to a timeout. Async tests do.

var test = require('tape'); // ^5
var assertTime = require('@ericcornelissen/assert-time');

var fut = require('./index.js');
var timeout = 100;

test('timing test, variant #1', function (t) {
  // Causes a test error
  t.doesNotThrow(function () {
    assertTime(fut, timeout);
  });
  t.end();
});

test('timing test, variant #2', function (t) {
  // Causes a test error
  assertTime(
    fut,
    timeout,
    function onSlow(duration) {
      t.fail('Test timed out after ' + duration + 'ms');
      t.end();
    },
    function onTime() {
      t.end();
    }
  );
});

test('timing test, variant #3', function (t) {
  // Causes the test to error and the test suite to STOP
  assertTime(fut, timeout);
  t.end();
});

About

Assert the duration of a (blocking or async) function under test

Resources

License

Security policy

Stars

Watchers

Forks

Contributors