Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatic crypto engine detection fails in a vitest environment #414

Open
AKuederle opened this issue Aug 22, 2024 · 0 comments
Open

Automatic crypto engine detection fails in a vitest environment #414

AKuederle opened this issue Aug 22, 2024 · 0 comments

Comments

@AKuederle
Copy link

AKuederle commented Aug 22, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant