Closed kentor closed 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
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.
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.
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.
@cpojer, but just.mock cannot create a stub for a non-existent file
It can, you can pass a third parameter
jest.mock('module', () => {}, {virtual: true})
@thymikee Where can I find this in the documentation?)
In the docs for jest.mock
: https://facebook.github.io/jest/docs/en/jest-object.html#jestmockmodulename-factory-options
@SimenB thank!
jest.mock
cannot do dependency mocking in require
d 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.
You might use unhoisted jest.doMock.
The only thing, jest.mock
cant - mock only required file dependencies, not project wise (global in proxyquite terms).
jest.mock
cannot do dependency mocking inrequire
d 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
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'. So
proxyquirewould mock only _the_
b.js, required from
a.js- dirrect first level child. While
jest.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".
Ah, right! Yeah, I agree that it's not a bug (or missing feature) from Jest's side 🙂 Thanks for specifying!
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.
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
andnpm test
. repro repo: https://github.com/kentor/proxyquire-jestworks 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