Skip to content

Promise support #262

@kl0tl

Description

@kl0tl

Would be great to support returning a promise to end the test when the promise is resolved.

  • It would play nice with future extensions of the language, allowing one to call tape with Async Functions to transparently return a promise.
  • It would eliminate some otherwise unfixable ordering issues with nested tests.
  • Rewriting
var test = require('tape');

test('first', function (t) {

  setTimeout(function () {
    t.ok(1, 'first test');
    t.end();
  }, 200);

  t.test('second', function (t) {
    t.ok(1, 'second test');
    t.end();
  });
});

test('third', function (t) {
  setTimeout(function () {
    t.ok(1, 'third test');
    t.end();
  }, 100);
});

to (assuming a function delay returning a Promise resolved after t milliseconds)

var test = require('tape');

test('first', async function (t) {

  await delay(200);

  t.ok(1, 'first test');

  t.test('second', async function (t) {
    t.ok(1, 'second test');
  });
});

test('third', async function (t) {
  await delay(100);
  t.ok(1, 'third test');
});

will preserve the expected order (see #222).


Looking at blue-tape it should be doable with something along the lines of

var ret = this._cb(this);
var self = this;
if (ret && typeof ret.then === 'function') {
  ret.then(function () {
    self.end();
  }, function (err) {
    nextTick(function () {
      throw err;
    });
  });
}

Note that it would not require a Promise library nor any configuration. Users wanting to take advantage of this feature would need to provide their own Promise implementation if necessary anyway.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions