udacity / cloudflare-typescript-workers

Types and mocks for building a tested Typescript Cloudflare Worker, generates three NPM packages
Apache License 2.0
139 stars 12 forks source link

Add support for mocking the Web Crypto API #17

Open wuservices opened 4 years ago

wuservices commented 4 years ago

I'm trying to test code that does something like crypto.subtle.digest('MD5', body). However, when I run the test, I get the error ReferenceError: crypto is not defined.

Do the mocks need to be augmented to add support for the crypto API?

Related:

13rac1 commented 4 years ago

Can you add/setup https://www.npmjs.com/package/get-random-values ?

wuservices commented 4 years ago

@13rac1 are you thinking that in my runtime code I'd just use that instead of using crypto directly?

If so, I think that could be an option, although it would bloat my production deployment with extra code. I think that particular package doesn't look like it'd work to calculate an MD5 with crypto.subtle.digest, but maybe there's something else similar.

Is there an option to make this work in Jest only since Cloudflare provides it already? Sorry if that's obvious, this is my first Jest rodeo.

13rac1 commented 4 years ago

Oh, no, not replacing anything provided by Cloudflare. We need to mock with get-random-values in Node, then provide Types so it can be used in the worker Typescript. Should be a small addition in https://github.com/udacity/cloudflare-typescript-workers/blob/master/packages/types-cloudflare-worker/src/global.ts plus configuration in https://github.com/udacity/cloudflare-typescript-workers/blob/master/test/setupJest.ts to setup get-random-values.

This should be enough to get you started if you can PR, otherwise I can probably handle it next week.

wuservices commented 4 years ago

Thank you for explaining that. It'd be really great if you could help add this capability. We've been loving this project and it's helping us run all our production traffic now. It's also my first TypeScript project, so if you'd be able to handle this next week, I think it'd help me understand how to approach such a change and contribute back in the future. 🙇

One thing I noticed was that get-random-values seems to be a different part of the crypto API than the part I need. It seems ideal to have that too since Cloudflare supports it, but my immediate use case was the SubtleCrypto API, which is accessible from crypto.subtle. Another small quirk is that Cloudflare supports MD5 with crypto.subtle.*, but the WebCrypto standard does not.

13rac1 commented 4 years ago

Can you locate a NodeJS replacement for these features?

wuservices commented 4 years ago

If we were adding fakes for all the crypto that Cloudflare supports, We could use get-random-values that you referenced, node-webcrypto-ossl, and if that doesn't work with MD5, node-md5.

ryan0x44 commented 2 years ago

If you're using Jest, Miniflare 2.0 now has a solution which uses a custom Jest test environment as per https://miniflare.dev/testing/jest:

yarn add --dev jest-environment-miniflare

enabled via Jest config e.g. in package.json:

"jest": {
  "testEnvironment": "miniflare"
}

If you use this, your tests should no longer show the ReferenceError: crypto is not defined error :)