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

Documentation Needed: jest.mock Equivalent #126

Open
shellscape opened this issue Feb 19, 2021 · 1 comment
Open

Documentation Needed: jest.mock Equivalent #126

shellscape opened this issue Feb 19, 2021 · 1 comment

Comments

@shellscape
Copy link

As a fellow open source maintainer, thank you for spending your time on this project.

That said, I'm really struggling with the format and verbiage in the documentation. It's very hard to parse, and the punctuation and structure is hard to read. I'd highly recommend breaking it up into multiple, short, focused documents that are linked with a table of contents on a main README.

With regard to Jest, the README states: jest.mocks - jest is awesome. As well as rewiremock. and earlier versions of it stated jest.mocks - Jest is awesome. But rewiremock can do the same. Both are super confusing and vague. Does this work the same as all aspects of Jest, is it a 1:1 comparison in terms of functionality, or does rewiremock only support some of the same functionality? I don't think these statements are adding to your README and the developer experience. To the contrary, I think it's adding confusion to your documentation.

Going further into the README with the jest-like section (https:/theKashey/rewiremock#jest---like) it would appear that there's 1:1 functionality with jest.mock, which is awesome. Unfortunately, in my testing of this I found that relative dependencies required from other files don't seem to be mocked - this is something that jest.mock supports out of the box. Here's my test setup:

bar.js

const { action } = require('./foo');

const baz = () => action();

module.exports = { baz };

foo.js

const action = () => 42;

module.exports = { action };

rewire.js - this is executed with node rewrire.js

const rewiremock = require('rewiremock/node');

const { baz } = require('./bar');

rewiremock('./foo').with({ action: () => {} });

console.log(baz());

This results in printing 42 and not {} as I would expect. Further, if I move rewire.js up a directory in the tree, I'll get a MODULE_NOT_FOUND error (requiring me to change the target of the mock to ../foo.js). All of this behavior is counter to what I would get using jest.mock, and thus confusing. The documentation doesn't contain any direct instructions on how to make this very direct jump from jest.mock to rewiremock, and it's a darn shame.

@theKashey
Copy link
Owner

First of all - I could just agree that documentation is crap. It is. No surprise.


Secondly - from jest.mock point of view documentation points mostly to https://www.npmjs.com/package/babel-plugin-jest-hoist and in order to use the same "hoisting" you have to use rewiremock babel plugin as well.

Keep in mind - It will not work with require, only with ESM imports.


Speaking of ../foo.js case - as a TypeScript-first person I encourage you to use another syntax - rewiremock(() => require('./foo')).with({ action: () => {} });. That will just end all possible "MODULE_NOT_FOUND" stuff as well as enable type-safety for mocks.


👉 speaking about of jest.mock - only with babel plugin rewiremock can enable itself, mocks what is needed, import what is required, and then disable itself. It your case it's has not been activated.

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