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

Slow tests when not mocked with factory method #7816

Closed
cpitt opened this issue Feb 6, 2019 · 3 comments
Closed

Slow tests when not mocked with factory method #7816

cpitt opened this issue Feb 6, 2019 · 3 comments

Comments

@cpitt
Copy link

cpitt commented Feb 6, 2019

🐛 Bug Report

We use Jest with an un-ejected CRA 2.0 configuration and have noticed extremely slow test speeds when component dependencies are not manually mocked with jest.mock using a factory method.

We added manual mocks with factory methods into a single domain of our code and saw drastic speed improvements. Attempts to use __mock__ directories made no impact.

Below are the results for only that test domain
Before (unmocked):

Test Suites: 74 passed, 74 total
Tests:       183 passed, 183 total
Snapshots:   0 total
Time:        105.903s
Ran all test suites matching /Notifications/i.
✨  Done in 107.97s.

After (mocked with factory methods):

Test Suites: 74 passed, 74 total
Tests:       183 passed, 183 total
Snapshots:   0 total
Time:        14.547s
Ran all test suites matching /Notifications/i.
✨  Done in 17.18s.                                                                                                                                                                         

Our current hypothesis is this is due to slow module resolution as tests for components with deeper dependency trees see a more drastic improvement when mocked with a factory method, whereas components with shallower dependencies see only marginal improvements.

To Reproduce

  1. Create a component that uses several subcomponents (we're using material-ui, but we've seen similar results without libraries)
  2. Run test and record speed
  3. Add jest.mock with factory function
    Example:
jest.mock('@material-ui/core', () => ({
  ListItem: 'ListItem',
  ListItemSecondaryAction: 'ListItemSecondaryAction',
  ListItemText: 'ListItemText',
  withStyles: () => component => component,
}));
  1. Run tests again and record speed.

Expected behavior

While I would expect some overhead for module resolution. I would not expect such a drastic difference in speeds. If this is expected is it documented somewhere?

Link to repl or repo (highly encouraged)

I've tried to reproduce this in repl.it with no success. Unfortunately the project I'm experiencing this on is private.

I will try to create a new CRA 2.0 project and see if I can reproduce and publish here when I get a chance.

Run npx envinfo --preset jest

Paste the results here:

  System:
    OS: macOS 10.14.2
    CPU: (8) x64 Intel(R) Core(TM) i7-6820HQ CPU @ 2.70GHz
  Binaries:
    Node: 8.11.3 - ~/.nvm/versions/node/v8.11.3/bin/node
    Yarn: 1.12.3 - /usr/local/bin/yarn
    npm: 6.4.1 - ~/.nvm/versions/node/v8.11.3/bin/npm
@cpitt cpitt added the 🐛 Bug label Feb 6, 2019
@github-actions
Copy link

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 14 days.

@github-actions github-actions bot added the Stale label Feb 25, 2022
@github-actions
Copy link

This issue was closed because it has been stalled for 7 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

@github-actions
Copy link

github-actions bot commented May 2, 2022

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.

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

No branches or pull requests

1 participant