Search code examples
node.jsunit-testingtap

Tap test times out


I try to perform a Diffie Hellman Key agreement:

Test.test('testDh', (t) => {
  const k1 = createSelfKey();
  const k2 = createSelfKey(k1.dh.getPrime(), k1.dh.getGenerator());

  const k1Secret = k1.dh.computeSecret(k2.keys);
  const k2Secret = k2.dh.computeSecret(k1.keys);

  t.equal(k1Secret.toString('hex'), k2Secret.toString('hex'));
  t.end();
});

Using the following function:

const createSelfKey = (p, g) => {
  let returnVal = null;
  if (_.isNumber(p) && _.isNumber(g)) {
    returnVal = { dh: crypto.createDiffieHellman(p, g) };
  } else {
    returnVal = { dh: crypto.createDiffieHellman(2048) };
  }
  returnVal.keys = returnVal.dh.generateKeys();
  return returnVal;
};

But for some reason the test times out. Do you know how I can raise the limit for timeout?

test/keygenTests.js ................................... 1/2 31s
  not ok timeout!
    expired: test/keygenTests.js

test/participantTest.js ............................... 1/1
total ................................................. 2/3


  2 passing (31s)
  1 failing

npm ERR! Test failed.  See above for more details.

Solution

  • You can make the tests not to timeout by modifying your package.json script like that:

     "scripts": {
        //Other scripts do there
        "test": "tap --no-timeout test/*.js",
      },
    

    As you can see you can pass the parameter --no-timeout in order to your test not times out at all, usefull in testing computation-heavy code.