Skip to content
This repository has been archived by the owner on Mar 15, 2021. It is now read-only.

Not mocking package under Jest #32

Closed
franciscop opened this issue Mar 6, 2018 · 7 comments
Closed

Not mocking package under Jest #32

franciscop opened this issue Mar 6, 2018 · 7 comments

Comments

@franciscop
Copy link

Hi and thanks for this package, it seems really useful. Specially since there are some packages that cannot be mocked easily otherwise.

I am trying an example very similar to the one stated in the documentation:

var mock = require('mock-require');

mock('pkg-dir', { sync: function () {
  console.log('pkg.sync() called');
}});

const pkg = require('pkg-dir');
console.log(pkg.sync());

When I try it with Node.js with node plugins/index.test.js (in my case) it works flawlessly printing pkg.sync() called. However, with Jest (with slight modifications since at least one test is required) it does not seem to work:

var mock = require('mock-require');

mock('pkg-dir', { sync: function () {
  console.log('pkg.sync() called');
}});

it('is synced', () => {
  const pkg = require('pkg-dir');
  console.log(pkg.sync());
});

It prints the directory where the package.json is hosted, as the pkg-dir library specifies (so it runs the original code). I have no idea how to even debug this since it seems like it's an issue with Jest + mock-require. I do not know the differences at runtime of Node.js and Jest, I thought Jest just loaded a couple of functions like it() and expect() but otherwise used a stock Node.js runtime.

@franciscop
Copy link
Author

franciscop commented Mar 6, 2018

Seems like Jest has a .mock() method so I will be using that. Sorry for bothering you :)

@boblauer
Copy link
Owner

boblauer commented Mar 6, 2018

Not a bother at all. For anyone else who sees this, I'm guessing the issue here is that Jest is probably using its own custom require method, which is how it handles things like auto-mocking. But as you discovered, you can just use Jest's mocking functionality and bypass mock-require altogether.

@JasoonS
Copy link

JasoonS commented Mar 6, 2019

@boblauer I really enjoy using your library. I use it in a bff (backend for frontend) that I have for a UI I am building and it works great.

I usually run the bff in 'mocked' mode independently and that works perfectly, but I would like to run it in mocked mode for my jest tests. This causes problems, and I can't seem to find a way to get jest to mock modules that are deeper in my file hierarchy (like your library does beautifully).

I tried mocking the 'mock-require' function with the jest.mock (or jest.doMock or jest.setMock) but it doesn't seem to work when it is an external file.

Maybe you have some suggestions? Otherwise I'll just run the bff in a separate thread when I run my tests.

@boblauer
Copy link
Owner

boblauer commented Mar 6, 2019

Unfortunately I don't have any experience with using this library in parallel with Jest, so I'm afraid I don't have any suggestions on how to make it work. Sorry ☹️

@JasoonS
Copy link

JasoonS commented Mar 7, 2019

Thanks for the reply. :) I'll have to make do!

@NateZimmer
Copy link

Your mocking lib was beautifully intuitive and I was using it w/ mocha. Unfortunately I hate mocha so I was trying to move onto Jest. Unfortunately it didn't work there and Jest's mock lib for modules is not nearly as intuitive or elegant as yours. So, nice work on the lib 👍

@boblauer
Copy link
Owner

Thank you for the kind words @NateZimmer . It's a bummer it doesn't work with Jest.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants