Skip to content

Automatic crypto engine detection fails in a vitest environment #414

Open
@AKuederle

Description

@AKuederle

I am using vitest (node environment) for testing in an App that I am writing.
Whenever I was executing tests (node 22), I got the use setEngine before getEngine error.

After some digging, I found out that the check performed here fails when running code with vitest.
Specifically, the "webcrypto" in crypto check returns false.

I am not really sure why this happens. I tried to research potential differences between the way vitest handles these global modules, but did not find anything that would point towards differences in the global crypto module.

The current workaround is to use setEngine explicitly in a beforeAll block.

import { webcrypto } from "crypto";

...
  beforeAll(() => {
    // This is required for some reason, as the automatic check for the engine fails when running inside vitest
    const name = "NodeJS ^15";
    const nodeCrypto = webcrypto as Crypto;
    setEngine(name, new CryptoEngine({ name, crypto: nodeCrypto }));
  });

I am not sure if this is a vitest issue, or something that should be addressed in the env check performed by pki.js. But maybe this is something that can be documented somewhere

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