Description
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