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

Enable manual mocks with nested folders #2483

Merged
merged 4 commits into from
Jan 15, 2017

Conversation

MicheleBertoli
Copy link
Contributor

Summary

Following #2475 this PR enables manual mocks with nested folders.

You can now mock dependencies with nested folders like:
jest.mock('react-styleguidist/src/rsg-components/Playground/PlaygroundRenderer')

Where the manual mock directory structure is the following:
screen shot 2016-12-31 at 07 20 06

This PR exists mostly to get an early feedback from the team and it could be improved.
For example, I attached the getMockName function to the HasteMap class to make testing easier but we can find a better solution (transforming it to an instance method?).

Test plan

I tried it on a project where I actually need it, and it worked.

Before (Jest loads the actual package):
screen shot 2016-12-31 at 07 21 56

After (It works):
screen shot 2016-12-31 at 07 22 54

@MicheleBertoli MicheleBertoli force-pushed the manual-mocks-nested-folders branch from 49bdeca to 45dfbab Compare December 31, 2016 07:40
@MicheleBertoli MicheleBertoli force-pushed the manual-mocks-nested-folders branch 2 times, most recently from 45dfbab to 38994cf Compare December 31, 2016 22:15
@codecov-io
Copy link

codecov-io commented Dec 31, 2016

Current coverage is 87.74% (diff: 85.71%)

Merging #2483 into master will increase coverage by 0.21%

@@             master      #2483   diff @@
==========================================
  Files            40         40          
  Lines          1653       1657     +4   
  Methods           0          0          
  Messages          0          0          
  Branches          0          0          
==========================================
+ Hits           1447       1454     +7   
+ Misses          206        203     -3   
  Partials          0          0          

Powered by Codecov. Last update b9cb0ac...cca8cc8

@MicheleBertoli MicheleBertoli force-pushed the manual-mocks-nested-folders branch from 38994cf to cca8cc8 Compare December 31, 2016 22:26
@cpojer
Copy link
Member

cpojer commented Jan 8, 2017

This looks good. Thanks @MicheleBertoli.

Instead of exporting it on HasteMap, can we make it a separate module called getMockName? That makes it testable and it will still be kind of "hidden" from the exported API.

@MicheleBertoli MicheleBertoli force-pushed the manual-mocks-nested-folders branch from 6129227 to 1322f56 Compare January 10, 2017 22:28
@jest-bot
Copy link
Contributor

jest-bot commented Jan 11, 2017

@cpojer
Copy link
Member

cpojer commented Jan 11, 2017

Alright, would you mind adding the copyright header to all files and @flow to all non-test files? Ignore the ones in test_root. We will make danger ignore those.


const extractMockName = new RegExp(
`^\\${path.sep}?(.*?)(\\${path.sep}index)?\\${path.extname(modulePath)}$`
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm really worried about performance here. Right now, mocksPattern is a config option but it isn't really configurable and I'm ok making it non-configurable just like the snapshots folder because it causes trouble.

Can you change this either by:

  • Optimize this code for performance. Think running this on 50k files, it should be less than 100ms or at least not slower than what we do here currently.
  • Change the mocksPattern option to be hard-coded, so splitting at __mocks__ and looking at the right-hand side of this should work and be fast.

@MicheleBertoli
Copy link
Contributor Author

MicheleBertoli commented Jan 11, 2017

Thank you very much for your feedback, @cpojer.
I'll add the copyright headers, flow, and I'll do my best to avoid any performance issue.
Sorry if I'm not very reactive in the last few days, but I'm pretty busy with my new job :)))

@cpojer
Copy link
Member

cpojer commented Jan 15, 2017

This is great. We'll deprecate mocksPattern with Jest 19. #2600

@cpojer cpojer merged commit 5976d16 into jestjs:master Jan 15, 2017
skovhus pushed a commit to skovhus/jest that referenced this pull request Apr 29, 2017
* Enable manual mocks with nested folders

* extract getMockName into a separate module

* avoid using regex

* add file headers
tushardhole pushed a commit to tushardhole/jest that referenced this pull request Aug 21, 2017
* Enable manual mocks with nested folders

* extract getMockName into a separate module

* avoid using regex

* add file headers
@James-E-Adams
Copy link

Cool! Glad to see this is supported. 👍

@github-actions
Copy link

This pull request 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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants