jestjs / jest

Delightful JavaScript Testing.
https://jestjs.io
MIT License
44.28k stars 6.47k forks source link

Does not work with proxyquire #1937

Closed kentor closed 8 years ago

kentor commented 8 years ago

Do you want to request a feature or report a bug? bug

What is the current behavior? doesn't work with proxyquire

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal repository on GitHub that we can npm install and npm test. repro repo: https://github.com/kentor/proxyquire-jest

> npm test
 FAIL  __tests__/index-test.js
  ● index › test

    TypeError: Cannot read property 'bind' of undefined

      at Proxyquire.Object.<anonymous>.Proxyquire.load (node_modules/proxyquire/lib/proxyquire.js:136:79)
      at Object.it (__tests__/index-test.js:6:19)
      at process._tickCallback (internal/process/next_tick.js:103:7)

  index
    ✕ test (4ms)

works with mocha: npm run mocha.

What is the expected behavior? to work

Run Jest again with --debug and provide the full configuration it prints. Please mention your node and npm version and operating system. node: 6.8.0 npm: 3.10.8

cpojer commented 8 years ago

Proxyquire is not necessary with Jest. Jest brings its own require implementation and you can stub out modules using jest.mock and similar. See http://facebook.github.io/jest/docs/api.html

kentor commented 8 years ago

Thanks @cpojer. That's too bad. I was hoping to use jest as a drop in replacement for mocha because of its superior watching abilities and for snapshotting.

cpojer commented 8 years ago

Switching test frameworks is not a "drop-in" kind of thing. You should carefully evaluate what makes sense for you and make the necessary updates to your codebase to support your new choice.

theKashey commented 6 years ago

Thats was not a perfect answer. It is better to say - Jest sanboxing system does not allow any third-party mocking system to coexists. And it does include some sort of mocking, by the way.

https://github.com/theKashey/rewiremock does supports proxyquire-like syntax, and it could work inside Jest environment. But proxyquire and 99.9% other mocking libs cant bypass Jest's systems.

mrdimidium commented 6 years ago

@cpojer, but just.mock cannot create a stub for a non-existent file

thymikee commented 6 years ago

It can, you can pass a third parameter

jest.mock('module', () => {}, {virtual: true})
mrdimidium commented 6 years ago

@thymikee Where can I find this in the documentation?)

SimenB commented 6 years ago

In the docs for jest.mock: https://facebook.github.io/jest/docs/en/jest-object.html#jestmockmodulename-factory-options

mrdimidium commented 6 years ago

@SimenB thank!

HoraceShmorace commented 5 years ago

jest.mock cannot do dependency mocking in required modules, which is exactly what proxyquire does.

For instance if foo.js requires request, then foo.test.js, upon requiring foo.js, might want to mock (and spy on) the latter's request dependency.

// foo.test.js
const get = jest.spy();
const post = jest.spy();
proxyquire('./foo.js', { 'request': { get, post } });
// ... rest of test

To the best of my knowledge, Jest can't do this.

theKashey commented 5 years ago

You might use unhoisted jest.doMock.

The only thing, jest.mock cant - mock only required file dependencies, not project wise (global in proxyquite terms).

SimenB commented 5 years ago

jest.mock cannot do dependency mocking in required modules, which is exactly what proxyquire does.

Just to jest.mock('some-module') - it will be mocked no matter where it's required from. By default all of its exported functions will be jest mock functions (jest.fn()s).

The only thing, jest.mock cant - mock only required file dependencies, not project wise (global in proxyquite terms).

Not sure I follow? jest.mock only affects the test you are in, no other tests. If you want global mocking, add the mock to a __mocks__ directory

theKashey commented 5 years ago

Not sure I follow? jest.mock only affects the test you are in, no other tests. If you want global mocking, add the mock to a __mocks__ directory

You have a.js, requiring b.js, and c.js. Meanwhile c.js will also require b.js. And you are mocking b.js'. Soproxyquirewould mock only _the_b.js, required froma.js- dirrect first level child. Whilejest.mock,mockery, and others would mock _any_b.js`, required from any place.

That's not a bug, but a feature. Like shallow render. And does "fix" one of the mocking smells - mocking something you dont "own".

SimenB commented 5 years ago

Ah, right! Yeah, I agree that it's not a bug (or missing feature) from Jest's side 🙂 Thanks for specifying!

github-actions[bot] commented 3 years ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.