Jest detects open handles running scryptSync from crypto nodejs

Viewed 205

When running a unit test using the scryptSync function from the package crypto I'm getting error messages/warnings I don't know what to do about.

I'm for example running the following unit test in Jest:

it('Should match the passwords', () => {
const pass = 'welkom01';
const saltedPass = service.createHashedAndSaltedPassword(pass);

expect(service.areTheSamePassword(pass, saltedPass)).toBe(true);
});

=====================USING THESE FUNCTIONS==========================

createHash(input: CreateHashInput): Hash {
 const { salt, data, length, options, encoding } = input;
 if (!salt.salt_string)
   throw new BadRequestException(ErrorCodes.CryptoNoSaltStringProvided);
 const hash_buffer = scryptSync(data, salt.salt_string!, length, options);
 const hash_string = hash_buffer.toString(encoding);
 return { hash_buffer, hash_string, length, encoding };
}  

createHashAndSalt(
    createHashAndSaltInput: CreateHashAndSaltInput,
  ): hashAndSalt {
    let { data, hash, salt, options } = createHashAndSaltInput;
    salt =
      salt?.salt_string && salt?.salt_buffer ? salt : this.createSalt(salt);
    hash = this.createHash({
      data,
      salt,
      options,
      length: hash.length,
      encoding: hash.encoding,
    });

 return { hash, salt };
}

This will give the following error message when I run the test with the flags --detectOpenHandles --forceExit:

Jest has detected the following 8 open handles potentially keeping Jest from exiting:

ā— SCRYPTREQUEST

  27 |     if (!salt.salt_string)
  28 |       throw new BadRequestException(ErrorCodes.CryptoNoSaltStringProvided);
> 29 |     const hash_buffer = scryptSync(data, salt.salt_string!, length, options);
     |                                   ^
  30 |     const hash_string = hash_buffer.toString(encoding);
  31 |     return { hash_buffer, hash_string, length, encoding };
  32 |   }

  at CryptoService.createHash (src/services/shared/crypto/crypto.service.ts:29:35)
  at OfficePasswordsService.createHashedAndSaltedPassword (src/office/officePasswords/officepasswords.service.ts:31:37)
  at Object.<anonymous> (src/office/officePasswords/officepasswords.service.spec.ts:39:32)

Reading some other problems with Jest open handles I don't get why this is causing a problem. scryptSync should be a synchronous operation and there is no usage of databases anywhere in the unit tests.

Can anyone please tell me what I'm doing wrong?

1 Answers

I encountered potential open handles on both SCRYPTREQUEST and RANDOMBYTESREQUEST. It appears these were false positives reported by Jest, and fixed in v27.0.0

For more details, see https://github.com/facebook/jest/issues/11275

Related