parro-it/tape-async

Is this library still necessary?

RichardJECooke opened this issue · 7 comments

I've tried using promises (which compile to yield statements) with normal tape and they seem to work fine.

Does this library do something extra? Or will my current tests using normal tape break under certain circumstances?

Can you post some sample code of how are you using a public repo? I can better understand if this library would help you

Here for example, using normal tape and returning a promise. Works fine.

(async function runTests() {
  recreateDatabase();
  await test_getUsersRoles_userCanGetHerOwnRoles();
  await test_getUsersRoles_franchiseeManagerCanSeeRolesForUserAtHerFranchisee();

  ... lots more tests ...

  await startedServer.stop();
})();

function test_postUsersRoles_franchisorOwnerCanAlterHerOwnRoles() {
  return new Promise( (resolve, reject) => {
    try {
        tape(testeeName + `POST /users/id/roles returns success when franchisor owner deletes her own roles`, async (assert) => {

        const newToken = await helper.getTokenForUser(requesteeUserId);
        superagent
        .post(testData.url + `users/` + requesteeUserId + `/roles`)
        .set('token', newToken)
        .send({'franchiseeId': null, 'franchisorId': 'f1a', 'roles': []})
        .end( async (error: any , result: superagent.Response) => {
          assert.false(error);
          const json = JSON.parse(result.text);
          assert.equals(json.errorCode, ResponseErrorCode.noError);
          assert.false(json.data);
          const dbResult = await db.runQuery(`select * from dbo.users_roles where users_id = '` + requesteeUserId + `'`);
          assert.equals(dbResult.rows.length, 0);
          assert.end();
          resolve();
        });
      });
    }
    catch (error) {
      console.error(error);
      reject(error);
    }
  });
}

Does this library do something extra?

With tape async you can refactor your test with something similar.

import test from 'tape-async';

// use a library that return a promise to simplify test code
import superagentPromise from 'superagent-promise';

// ...other setup stuff

test(
  'use the first test in the file to setup the test suite',
  // here since `tape-async` manage the call to t.end for you,
  // you can simplify the test to a single function.
  recreateDatabase
);

test(`getUsersRoles: /users/id/roles returns success when franchisor owner deletes her own roles`, async (assert) => {

  const newToken = await helper.getTokenForUser(requesteeUserId);
  const result = await superagentPromise
    .post(testData.url + `users/` + requesteeUserId + `/roles`)
    .set('token', newToken)
    .send({'franchiseeId': null, 'franchisorId': 'f1a', 'roles': []})
    .end();

  const json = JSON.parse(result.text);
  assert.equals(json.errorCode, ResponseErrorCode.noError);
  assert.false(json.data);
  const dbResult = await db.runQuery(`select * from dbo.users_roles where users_id = '` + requesteeUserId + `'`);
  assert.equals(dbResult.rows.length, 0);
  // assert.end(); no need to end the test.

  // if somethink throws in this function, tape-async automatically catch the error.
});


// ... lots more tests ...

test(
  'use the last test in the file to tearDown the test suite',
  // here since `tape-async` just expect a promise as return value of your test method
  // you can simplify the test to a single expression.
  () => startedServer.stop()
);

Or will my current tests using normal tape break under certain circumstances?

It shouldn't. But note this open PR #2 in regard...

Thanks, this is cool, I'll use it.

But regarding my question as to the real difference between tape and tape-async

  • will tape work with promises without failing? Because that PR is for this library, not tape.
  • If tape works with promises, then are the only new things intape-async: calling end automatically, handling catch automatically ?

will tape work with promises without failing? Because that PR is for this library, not tape.

What do you mean "without failing"? maybe it's better if you ask @substack for this

If tape works with promises, then are the only new things in tape-async: calling end automatically, handling catch automatically ?

No, the main reason this module exists is to pass async functions directly to tape test method.
This could greatly simplify the test code.

In the example above, have you noticed I replaced your use of superagent with superagent-promise? I do it to be able to await the response and avoid the need to test for result errors.

One more thing about your example code above please.

You're going:

tape('test 1', ...);
tape('test 2', ....);
tape('test 3', ....);

But won't they then run in parallel since tape is not blocking? I.e. any database calls or other state changes will cause interference between the tests and unexpected results.

Does tape-async return a promise too? So I can do this instead?

(async function run() {
  await tape('test 1, ...);
  await tape('test 2, ....);
  await tape('test 3, ....);
)();

And sorry for noob, but what is substack? Is it like a forum? Google is of no help with this definition.

But won't they then run in parallel since tape is not blocking?

No, they are serially anyway. The code instead tests is non blocking, but you won't get any speed emprovemenr from parallelization. You may want to check AVA for this.

nd sorry for noob, but what is substack? Is it like a forum? Google is of no help with this definition.

No, it's the author of tape. If you click on it's name you'll see his profile 😄