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

Jest compatibility #5

Closed
t1bb4r opened this issue Jul 28, 2017 · 6 comments
Closed

Jest compatibility #5

t1bb4r opened this issue Jul 28, 2017 · 6 comments

Comments

@t1bb4r
Copy link

t1bb4r commented Jul 28, 2017

Jest is a great test runner and I'm trying to use it with request-promise in my current project. The problem is this module isn't working with jest.

Here is a minimal example:
reusable.js:

let id = Math.random();
module.exports = {id};

users.js:

let stealthyRequire = require('stealthy-require');
var reusableStealth = stealthyRequire(require.cache, function () {
    return require('./reusable');
},
function () {}, module);
var reusable = require("./reusable");
module.exports = {
  reusableStealth,
  reusable
}

__tests__/users.js:

let users = require("../users");
describe("", () => {
  it("", () => {
    expect(users.reusableStealth.id).not.toEqual(users.reusable.id);
  })
})

This actually works in node, when I require the users module I get two unique ids. Just not working with jest, so I can understand if you don't consider this a bug.

Side-note: I don't understand how I can be the only one using jest and request, each have over 10 000 stars on github.

@t1bb4r
Copy link
Author

t1bb4r commented Jul 28, 2017

Doesn't look like there is anything wrong with this module, it's more of a request-promise-core / jest issue.

EDIT: In jest require.cache doesn't actually control the cache, so request-promise-core and jest is incompatible)

@t1bb4r t1bb4r closed this as completed Jul 28, 2017
@analog-nico
Copy link
Owner

Hey @t1bb4r , I am curious nonetheless. Since the code is working in node without Jest I am wondering what difference Jest makes. In your Jest version do you still execute it within node or somewhere else like in the browser? Do you use transpilers like babel? And packagers like webpack or browserify?

@t1bb4r
Copy link
Author

t1bb4r commented Jul 28, 2017

I'm using jest --env=node which should execute the tests in a node environment. They have their own module cache / registry management, which is the problem (I might be completely wrong). I've been investigating this file, jest-runtime, for a while to try and understand whats going on.

Jest has a mocking feature where you can mock any import, this feature is probably the reason they keep their own module registry.

@analog-nico
Copy link
Owner

Alright, I see. They are passing their own require function to the modules and that function uses a cache that is not exposed. So yes, stealthy-require and jest are incompatible and there is no solution unless jest would expose their moduleRegistry.

But thinking outside the box: You want to test your request-promise code, right? It uses stealthy-require internally to allow projects that use request and request-promise at the same time. If your project always uses request-promise and never request by itself then you can just ignore the fact that stealthy-require is falling back to a standard require.

@t1bb4r
Copy link
Author

t1bb4r commented Jul 28, 2017

Its tough to say exactly what all the libraries I'm including uses internally and when they will conflict, but you are right, if we can manage to only use the core module once everything is ok.

The first time I started to run into this issue was when I installed a module which depends on "request-promise", while I'm using "request-promise-native". These imports, no matter how deep in your dependency tree, will break jest.

Either way, thanks a lot for the help and feedback. I'll manage to workaround the issue.

@analog-nico
Copy link
Owner

You are welcome buddy. One last tip: You can check with npm ls request if different libraries depend on request.

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

2 participants