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

aws4fetch unusable in testing #28

Closed
James-Reed-cognito opened this issue Nov 19, 2020 · 7 comments
Closed

aws4fetch unusable in testing #28

James-Reed-cognito opened this issue Nov 19, 2020 · 7 comments

Comments

@James-Reed-cognito
Copy link

Hey there,

I found this library recently and was quite excited to try it out - everything worked perfectly for my app. However, when I tried to run tests using it, it seems to error out - presumably because Jest runs in a node-like environment, and aws4fetch expects a browser-like environment. The first error I got was that 'TextEncoder' was missing, and then when I got past that, that 'Request' was missing, and then 'crypto'...and then I stopped because having to essentially rebuild dependencies of a library isn't what I want to spend my time doing, and means that my tests aren't true to what's actually going on anyway.

This code:

const Environment = require('jest-environment-jsdom');

module.exports = class CustomTestEnvironment extends Environment {
  async setup() {
    await super.setup();
    if (typeof this.global.TextEncoder === 'undefined') {
      const { TextEncoder } = require('util');
      this.global.TextEncoder = TextEncoder;
    }
    if (typeof this.global.Request === 'undefined') {
      const { request } = require('http');
      this.global.Request = request;
    }
  }
};

Was what I started with (which would go as a arg to a test script ie "test": "jest --env=./__tests__/setup/jsdomSetup.js"

I'm not sure how other users of this library are getting around this, but not being able to run tests whilst using this library means it's a no-go, for now. I hope you can find some way of making it possible!

@mhart
Copy link
Owner

mhart commented Nov 19, 2020

Hey @James-Reed-cognito ! I don't really appreciate the title you've given this issue. There are tests in https://github.com/mhart/aws4fetch/tree/master/test – I assure you they work just fine.

@mhart mhart closed this as completed Nov 19, 2020
@mhart
Copy link
Owner

mhart commented Nov 19, 2020

As the README says in the very first line, this library is:

for environments that support fetch and SubtleCrypto

If you need to get an environment like that working with jest, this looks like the right place to read up on:

https://jestjs.io/docs/en/puppeteer

Next time you're thinking of leaving an issue on a library that you want to use, especially if it's been created with people's free time and resources, just take a step back, take a breath, and have a think about whether it's really an issue with the library, or whether there are options you just haven't explored. Your open source experience will be all the better for it!

@JamesZoft
Copy link

JamesZoft commented Nov 19, 2020

Hey @mhart sorry for the prickly title - it was uncalled for. I'm used to a certain environment in which my unit tests for my webapps work, and in this environment, aws4fetch seemed to me to be unusable.

Perhaps you could add something in the README that addresses this difference in aws4fetch specifically for testing, and that points to puppeteer or similar?

Again, apologies for the curt tone in the title - please do feel free to change it (I looked but I can't seem to myself).

@mhart
Copy link
Owner

mhart commented Nov 19, 2020

The problem is you think it's an issue or difference with this library. It's not – this library is just using incredibly standard browser APIs. The issue is with the test runner you've chosen, and if you'd searched for how to get jest working with a browser you'd see it as the first result:

https://www.google.com/search?q=jest+browser

@JamesZoft
Copy link

The issue from my side was that I didn't know that all these errors were because of browser APIs - I've not seen TextEncoder before for example, so from my perspective it seemed like a load of strange errors. I did google around with searches like 'aws4fetch jest' and 'aws4fetch jest testing' and similar searches - I guess I didn't think to go a level up to the test runner.

@mhart
Copy link
Owner

mhart commented Nov 19, 2020

"TextEncoder jest" would've been a better search – you were on the right track with your "Jest runs in a node-like environment" hunch. Looks like there are issues with it: jestjs/jest#9983 and jsdom/jsdom#2524

In any case, all good. I'm not going to change the documentation – the first line makes it very clear what environment the library is intended for. It'd be like asking MDN to add documentation on how to get every browser API working with jest. That's just jest's responsibility (or jsdom's responsibility) to make sure they document what does and what doesn't work with their browser simulation. But I'm glad you're on the right track now 👍 Good luck!

@JamesZoft
Copy link

The first line does make it clear, but when I initially added this library to my project I wasn't thinking about testing, and my initial read of the README had gone out of my head - and when I tried to run my tests, getting what (to me) looked like esoteric errors lead me down wrong search paths and into confusion. A simple addition like for environments that support fetch and SubtleCrypto. This may mean you need extra config to make this work in a testing environment such as Jest.. I think the problem here is that I didn't jump from 'this library only works in browser only envs' and take a step back to 'gotta fix Jest', I jumped forwards to 'gotta fix the library'. Maybe this is on Jest for not making options like Puppeteer more 'in your face'...not really sure.

Apologies again for the initial curt tone - I do appreciate the work people like you do for OSS and it's very easy for people like me to come in guns blazing. :)

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

3 participants