jest icon indicating copy to clipboard operation
jest copied to clipboard

Slow React Tests

Open TYRONEMICHAEL opened this issue 12 years ago • 81 comments

Thanks for React and Jest. Loving the combo. Anyhow, I am used to running tests in livereload mode. So anytime a test file is saved, my tests are automatically run. I got this working correctly, but my tests are taking almost 3 seconds to run. That is just with one test file. I am running the files through the preprocessor, so I suspect that is where the problem lies. Do you have any suggestions on how to get the tests to run quickly or any advice on how to speed up the TDD / BDD workflow?

TYRONEMICHAEL avatar Aug 08 '14 11:08 TYRONEMICHAEL

Hi Tyron. Had this issue with 20+ seconds for one file :) The thing was that I've configured to just run 'jest' in the root directory. And there're LOTS of subdirs that Jest was checking for tests. Specifying a path for tests reduced this time more than 10 times now. And I've got a coffee preprocessor too. In package.json "scripts": { .... "test": "jest path/to/modules/to/test" }

BTW, are you preprocessing coffee or what? :)

gothy avatar Aug 08 '14 11:08 gothy

Thanks for getting back to me @gothy. I am using regular JS with JSX. I am just running my tests through the JSX preprocessor like the example (https://github.com/facebook/jest/tree/master/examples/react). The example is also taking about 2.5 - 3 seconds to run. The jQuery example takes less than a second. Is the JSX preprocessor meant to take so much time when processing your JSX files?

React Test

screen shot 2014-08-08 at 1 46 05 pm

jQuery Test

screen shot 2014-08-08 at 1 54 55 pm

TYRONEMICHAEL avatar Aug 08 '14 11:08 TYRONEMICHAEL

Ah, I didn't use it for JSX. Can't say anything on this processor. Maybe it's actually slow. I've found my issue with directories on a real project with loads of old stuff :)

gothy avatar Aug 08 '14 11:08 gothy

I experienced a similar issue with Coffeescript preprocessor. I think the problem here is that the preprocessor tries to process your dependencies as well. If you happen to require lots of things, it slows down.

jeffchan avatar Aug 08 '14 20:08 jeffchan

I definitely also experience slow tests with jest :(

hojberg avatar Aug 08 '14 20:08 hojberg

I'm experiencing the same thing. I don't think it has to do with the preprocessing (JSX processing on this small file is fast). I commented out all of the code in the example test except one of the following require statements and the test still takes 4 seconds. As soon as I comment them out, the test takes 0.1s. I did a little digging and it looks like the HasteModuleLoader is having to process 490 required packages (_shouldMock()) and not mock them when you require the react/addons.

var React = require('react/addons');

or

var CheckboxWithLabel = require('../CheckboxWithLabel.js');

iamrandys avatar Aug 10 '14 14:08 iamrandys

I removed the following var React = require('react/addons'); and still ran the files through the preprocessor. I got maybe 0.2 second improvement. If I removed the preprocessor, I get the following results:

With the JSX preprocessor screen shot 2014-08-10 at 5 35 22 pm

Without the JSX preprocessor screen shot 2014-08-10 at 5 34 12 pm

TYRONEMICHAEL avatar Aug 10 '14 15:08 TYRONEMICHAEL

I prefer Mocha over Jasmine and decided to setup a gulpfile that would build the react component then run it through the mocha test suite (snippet below).

function buildScript(file, watch) {
  var bundler = browserify(file);
  var stream;
  bundler.transform(reactify);
  stream = bundler.bundle();
  return stream.on('error', handleErrors)
  .pipe(source(file))
  .pipe(gulp.dest(buildDir + '/'))
  .pipe(mocha({ reporter: 'list' }));
}

It still has to preprocess the JSX file using reactify, but rids the warning message for slow tests. So the runtime still takes just under 2 seconds, but the actual test is about 32ms. Still deciding if using JSX is worth it.

TYRONEMICHAEL avatar Aug 10 '14 16:08 TYRONEMICHAEL

Oh you're right. I tested the jest react example not using JSX and it went from close to 4 seconds down to 0.75 seconds. Makes me really think if it's worth it to use JSX. On a big project, it's going to get slow pretty quickly unless I have a lot of different packages. I wonder if the preprocessor is run on all 490 requires and not just that single file. There is no way it should take 3 seconds for that simple component.

Either way, I really really need my tests to be fast for my workflow. I still need to figure out how to at least run a single suite. In jasmine I could use "ddescribe" or "iit" instead of "describe" and "it" to run a single test or suite. Jest is SO nice, I just need a fast workflow now.

var React = require('react');

var CheckboxWithLabel = React.createClass({displayName: 'CheckboxWithLabel',
    getInitialState: function() {
        return { isChecked: false };
    },
    onChange: function() {
        this.setState({isChecked: !this.state.isChecked});
    },
    render: function() {
        return (
            React.DOM.label(null,
                React.DOM.input(
                    {type:"checkbox",
                        checked:this.state.isChecked,
                        onChange:this.onChange}
                ),
                this.state.isChecked ? this.props.labelOn : this.props.labelOff
            )
            );
    }
});

module.exports = CheckboxWithLabel; 

iamrandys avatar Aug 10 '14 20:08 iamrandys

@jeffchan was right. The all of the required code is run through the preprocessor, not just the JSX files.

Looks like the fastest solution will be to use gulp, watchify and react to only process the changed JSX files while your working. We will then be able to specify only the tests that I want to run as well. This way the JSX files only get processed once and I have control over which tests are run. Something like this would be really nice for a testing workflow.

gulp jest --tests "Checkbox*,*Form*"

Watchify will then watch for any changes that these tests depend on, and then only process the changes and then only run the tests I'm working with.

iamrandys avatar Aug 10 '14 21:08 iamrandys

@iamrandys I 100% agree with you. Jest and React are awesome, but compilation of JSX into JS is a big hinderance. Just curious how Gulp will solve the problem of having your required files (Non JSX) being run through the JSX preprocessor? Are you suggesting something like the following -http://blog.avisi.nl/2014/04/25/how-to-keep-a-fast-build-with-browserify-and-reactjs/ ?

TYRONEMICHAEL avatar Aug 11 '14 07:08 TYRONEMICHAEL

Yep, I'm thinking about some kind of caching layer with the gulp plugin in working on

On 11 August 2014 08:35, Tyrone Avnit [email protected] wrote:

@iamrandys https://github.com/iamrandys I 100% agree with you. Jest and React are awesome, but compilation of JSX into JS is a big hinderance. Just curious how Gulp will solve the problem of having your required files (Non JSX) being run through the JSX preprocessor? Are you suggesting something like the following? - http://blog.avisi.nl/2014/04/25/how-to-keep-a-fast-build-with-browserify-and-reactjs/ ?

— Reply to this email directly or view it on GitHub https://github.com/facebook/jest/issues/116#issuecomment-51749798.

Dakuan avatar Aug 11 '14 08:08 Dakuan

Exactly, using gulp and watchify is amazingly fast with react. Throw in gulp-livereload to refresh your browser after each change and you have an amazing development environment. You make any change, save and you almost instantly see the changes in all your open browsers and all devices. Now I just need the same thing for my TDD.

It's about like this, but use reactify instead of hbsfy. https://gist.github.com/benhowdle89/9533185

On Mon, Aug 11, 2014 at 2:35 AM, Tyrone Avnit [email protected] wrote:

@iamrandys https://github.com/iamrandys I 100% agree with you. Jest and React are awesome, but compilation of JSX into JS is a big hinderance. Just curious how Gulp will solve the problem of having your required files (Non JSX) being run through the JSX preprocessor? Are you suggesting something like the following? - http://blog.avisi.nl/2014/04/25/how-to-keep-a-fast-build-with-browserify-and-reactjs/ ?

— Reply to this email directly or view it on GitHub https://github.com/facebook/jest/issues/116#issuecomment-51749798.

iamrandys avatar Aug 11 '14 11:08 iamrandys

Thanks @iamrandys. Developed a quick react-component-boilerplate which uses Mocha and Chai (Jasmine can be easily substituted). Tests are extremely quick and you get the added benefit of Livereload. Use as you wish.

TYRONEMICHAEL avatar Aug 12 '14 15:08 TYRONEMICHAEL

Either way, I really really need my tests to be fast for my workflow. I still need to figure out how to at least run a single suite. In jasmine I could use "ddescribe" or "iit" instead of "describe" and "it" to run a single test or suite. Jest is SO nice, I just need a fast workflow now.

You can definitely write it.only and I believe you can do describe.only as well.

sophiebits avatar Aug 13 '14 00:08 sophiebits

Karma does exactly what you are wanting and all you have to do is add a karma.conf file to your project. I didn't realize karma supported reactify and browserify. Now you can test in all of your browsers at the same time. I created a PR for your boilerplate project.

https://github.com/iamrandys/react-component-boilerplate/tree/karma

Just run 'npm test' and karma will launch your browsers and will watch for changes.

On Tue, Aug 12, 2014 at 10:35 AM, Tyrone Avnit [email protected] wrote:

Thanks @iamrandys https://github.com/iamrandys. Developed a quick react-component-boilerplate https://github.com/TYRONEMICHAEL/react-component-boilerplate which uses Mocha and Chai (Jasmine can be easily substituted). Tests are extremely quick and you get the added benefit of Livereload. Use as you wish.

— Reply to this email directly or view it on GitHub https://github.com/facebook/jest/issues/116#issuecomment-51931532.

iamrandys avatar Aug 13 '14 00:08 iamrandys

Use the following preprocessor.js to avoid JSX-transforming non-JSX files. As is, it only processes .jsx files that contain the /** @jsx prefix. If you want to JSX-transform .js files, just remove the first part of the if-condition before the || (so that only the src.slice ... condition remains).

// from http://facebook.github.io/jest/docs/tutorial-react.html
var ReactTools = require('react-tools');
var MAGIC = "/** @jsx";
module.exports = {
  process: function(src, file) {
    if (!/\.jsx$/.test(file) || src.slice(0, MAGIC.length) != MAGIC) return src;
    return ReactTools.transform(src);
  }
};

Still kinda slow, though.

sqs avatar Aug 29 '14 05:08 sqs

Interesting snippet @sqs. Correct me If I am wrong, but would it still not have to look through the each file and check if it needs to convert it? I have had much success with the following - react-component-boilerplate. Tests actually run pretty quickly.

TYRONEMICHAEL avatar Aug 29 '14 06:08 TYRONEMICHAEL

Very nice. This reduced the time from 9.3s to 4.7s. This is for a single test. I'll still have to stay with Karma where it is much faster (100 tests take less than a second). Plus, Karma will watch for changes as you work and will test your code in multiple browsers, but I love Jest's automatic mocking though. Manually creating spies using rewireify is extra work, but you do have complete control.

iamrandys avatar Aug 29 '14 12:08 iamrandys

Yeah, I may be misunderstanding you, but that's what I meant about removing the check for .jsx if you have .js files with jsx and want to detect based on the pragma header.

Sent from my iPhone

On Aug 28, 2014, at 23:45, Tyrone Avnit [email protected] wrote:

Interesting snippet @sqs. Correct me If I am wrong, but would it still not have to look through the each file and check if it needs to convert it? I have had much success with the following - react-component-boilerplate. Tests actually run pretty quickly.

— Reply to this email directly or view it on GitHub.

sqs avatar Aug 29 '14 22:08 sqs