feat: ship a Jest mock backed by node:crypto - #1076
Merged
Merged
Conversation
Jest runs in Node, where the native module doesn't exist, so importing the library in a test throws. Add react-native-quick-crypto/jest, which forwards to node:crypto so tests get real results without hand-written stubs. Includes a unit test, a 'Testing with Jest' guide, and an ESLint override for the CommonJS mock file.
|
@AttayR is attempting to deploy a commit to the Margelo Team on Vercel. A member of the Team first needs to authorize it. |
Collaborator
|
Thanks for this, @AttayR! Forwarding to I pushed a small follow-up commit (94d5a45) to your branch:
Jest passes 19/19, and ESLint and Prettier are clean. No need to backport to |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follows up on discussion #800, where adding mocks for Jest was suggested.
Problem
Jest runs in Node, where the native module doesn't exist, so any app test that imports
react-native-quick-crypto(directly or through its own code) throws. Today every app has to hand-write stubs for the functions it uses.Change
Ship a mock at
react-native-quick-crypto/jest. Node already implements thecryptoAPI this library brings to React Native, so the mock forwards tonode:cryptoinstead of stubbing functions. Tests get real hashes, ciphertexts and signatures, and app code runs unchanged.install()is a no-op (Node already hasglobalThis.cryptoandBuffer).undefined; the guide shows how to extend the mock for those.jestis added tofilesso it's published. It's plain CommonJS because Jest doesn't transformnode_modulesby default.Files
packages/react-native-quick-crypto/jest/index.js: the mockpackages/react-native-quick-crypto/test/jestMock.test.ts: default/named exports, SHA-256 vector, AES-256-GCM round trip,subtle.sign/verify(HMAC)docs/content/docs/guides/testing-with-jest.mdxandmeta.json: new guideeslint.config.mjs: lets ESLint parse the CommonJS mock (allowDefaultProjectplus a CommonJS override), since it sits outsidetsconfigTesting
jest: 19/19 (existing tests plus 4 new)eslint "**/*.{js,ts,tsx}",prettier --checkon the changed files,tsc --noEmit: cleansubtle.verifyresolving totrueandargon2being available (Node 24.7+).I targeted
mainonly. Happy to backport to0.xif you want it there too.