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

[Q] How to speed up Jest? #4453

Closed
CharlieIGG opened this issue May 13, 2018 · 11 comments
Closed

[Q] How to speed up Jest? #4453

CharlieIGG opened this issue May 13, 2018 · 11 comments

Comments

@CharlieIGG
Copy link

CharlieIGG commented May 13, 2018

Is this a bug report?

No, it's a question.

The Question: How to speed up Jest?

Tests are running at a very slow pace

I currently have only three tests: Two tests that validate the initial state of my app, plus the default 'renders without crashing' test that comes with CRA.

My other tests look like so:

import { AppState } from '../State/AppState';

describe("store", () => {
    const store = new AppState();
    it("initializes with default variableX", () => {
        expect(store.quote.variableX).toBe("SomeService");
    })
    it("cannot have variableZ without a defined variableX and variableY", () => {
        // (...)
    })
})

It takes 11.30 seconds (not ms) to run these tests. If I just test with the default CRA test it takes around 8 - 9 seconds.

What gives? How can I make tests run faster?

@vidarc
Copy link

vidarc commented May 18, 2018

I've been getting this same issue lately. Very simple tests are taking 6 seconds to run. If I'm in watch mode and add a file regex to run one, they run fast as expected. Running them together causes a slow down either in the test or the post/pre test.

@Timer
Copy link
Contributor

Timer commented Jun 1, 2018

This question may be better suited for the Jest repo, but my best advice would probably be to disable the jsdom environment if you don't need it.

You can also try the newer version of react-scripts@next to see if an upgraded version of Jest helps anything.

If this occurred as a result of an update on our end (and not just an increase in app size), let us know!

@Timer Timer closed this as completed Jun 1, 2018
@CharlieIGG
Copy link
Author

Thanks @Timer I'll find out how to disable jsdom.

@Timer
Copy link
Contributor

Timer commented Jun 2, 2018

@CharlieIGG you can remove the --env=jsdom from your package.json file's test command.

@JeremyBradshaw7
Copy link

JeremyBradshaw7 commented Aug 2, 2018

I'd be happy with 6s - I'm taking minutes to run one test!

Test Suites: 1 passed, 1 total
Tests:       1 skipped, 1 passed, 2 total
Snapshots:   0 total
Time:        232.852s

@bugzpodder
Copy link

@JeremyBradshaw7 mind creating a reproducible demo?

@petermikitsh
Copy link

Adding the following to my package.json speeds up my jest runs considerably:

  jest: {
    "testEnvironment": "node"
  }

WITHOUT testEnvironment set:

    console.time src/server/services/authentication/__tests__/authentication.test.js:87
      foo: 4383ms
    console.time src/server/services/authentication/__tests__/authentication.test.js:103
      bar: 4321ms

WITH testEnvironment set:

    console.time src/server/services/authentication/__tests__/authentication.test.js:87
      foo: 666ms
    console.time src/server/services/authentication/__tests__/authentication.test.js:103
      bar: 635ms

@JeremyBradshaw7
Copy link

@bugzpodder can't really do that without exposing the company's code, I'd have to reproduce the whole environment on a separate codebase with dummy code. Typescript transpilation is involved though - that may have something to do with it. The multi-minute delay is only first time I run the tests (in a session), run them again it takes a few seconds.

@parkerault
Copy link

Sorry to comment in a closed thread, but I just upgraded react-scripts from 1.4 to 2.0.4 and my test suite that was previously taking ~200ms to run now takes ~5000ms. Watching the output, I can see that it hangs on Tests: 0 passed, 0 total for most of that 5 seconds, so it would appear that it's not the tests, but the startup time that is causing the slowdown. Is there any reason that v2 would be doing significantly more test setup than v1?

@gaearon
Copy link
Contributor

gaearon commented Oct 4, 2018

@parkerault

File a new issue please. Ideally reproducing project would also be helpful.

@facebook facebook locked as resolved and limited conversation to collaborators Oct 4, 2018
@gaearon
Copy link
Contributor

gaearon commented Oct 4, 2018

I'll lock this one because we don't track closed stale issues, so if they are used for new problems then we'll never solve those problems.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants