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

Examples of requiring React once instead of in every test? #186

Closed
CodeOtter opened this issue Nov 27, 2014 · 7 comments
Closed

Examples of requiring React once instead of in every test? #186

CodeOtter opened this issue Nov 27, 2014 · 7 comments

Comments

@CodeOtter
Copy link

This is what I have to do in order to get tests to work:

describe(function() {
  describe(function() {
    it(function() {
        var React = require('react');
        // Change the state of a compoennt
    })
    it(function() {
        var React = require('react');
        // Change the state of a compoennt
    })
  })
})

When I try to do this:

describe(function() {
  var React = require('react');
  describe(function() {
    it(function() {
        // Change the state of a compoennt
    })
    it(function() {
        // Change the state of a compoennt
    })
  })
})

I get error: TypeError: Cannot read property 'firstChild' of undefined

@awei01
Copy link

awei01 commented Nov 27, 2014

Not really a Jest issue. Check out jasmine docs: http://jasmine.github.io/

You should do this:

describe(function() {
    var React;
    beforeEach(function() {
        React = require('react');
    });
    it('should have access to React var', function() {
        expect(React).not.toBeUndefined();
    });
});

@simenbrekken
Copy link

Even if it's not a Jest issue it looks messy and leads to bugs. Why doesn't e.g Mocha have this issue?

@SegFaultx64
Copy link
Contributor

This also makes the test runs very slow. Since HasteModuleLoader has to traverse the dependency tree for each test. I have a workaround for this but it breaks test "hygiene" by reusing mocks so I am hesitant to post it here.

@tchcxp
Copy link

tchcxp commented Feb 25, 2015

Anyone looks into this issue?

@ghost
Copy link

ghost commented Aug 5, 2015

Thank you for reporting this issue and appreciate your patience. We've notified the core team for an update on this issue. We're looking for a response within the next 30 days or the issue may be closed.

@cpojer
Copy link
Member

cpojer commented Feb 17, 2016

The performance problems were fixed with #599.

To answer your question (and sorry I never saw this issue before :) ), jest gives you test isolation. It resets the module registry on every call to it to make sure you get a fresh environment. You can require React either on the top level (once) or inside of your beforeEach calls (for every test).

@cpojer cpojer closed this as completed Feb 17, 2016
@github-actions
Copy link

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 14, 2021
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

6 participants