Coder Social home page Coder Social logo

Comments (12)

granmoe avatar granmoe commented on May 18, 2024 3

So the mistake I was making is obvious in hindsight but was hard to spot. I was mounting a subset of my app that is normally (outside of tests) wrapped in a parent route. So when I mapped through those components/routes in the test, they weren't wrapped in their parent route and so their routes never matched any href from any of the <Link>s. Once I realized that and mounted the top-level component with the parent routes (<Fragment>s), the rest of the process was easy. Turns out, I also needed to set the jest testURL for jsdom (otherwise it blows up when you try to push a route while on "about:blank").

Thank you for offering encouragement, by the way! I admire people who can behave that way in a context where it's far easier to be apathetic or annoyed 😄

from react-testing-library.

kentcdodds avatar kentcdodds commented on May 18, 2024 1

You can actually turn a codesandbox into a local project :)

screen shot 2018-04-10 at 8 25 02 am

from react-testing-library.

kentcdodds avatar kentcdodds commented on May 18, 2024 1

Works locally too :)

screen shot 2018-04-10 at 8 26 55 am

from react-testing-library.

kentcdodds avatar kentcdodds commented on May 18, 2024 1

Oh, yes, you definitely need jsdom for this library to work. If you're using Jest, you'll need to set the testEnvironment to jest-environment-jsdom.

I'm guessing that's the issue. Feel free to continue asking questions and folks can help when and where they can. But that's probably the problem. Good luck.

from react-testing-library.

granmoe avatar granmoe commented on May 18, 2024 1

Well, I'm at a loss. I updated the codesandbox to reflect this scenario and it works in the browser and locally. I'm going to try to strip down my failing example and discover what the problem is. Sorry for all the noise on this issue.

from react-testing-library.

kentcdodds avatar kentcdodds commented on May 18, 2024 1

I'm glad you worked it out! If you'd like to add an example to the tests that'd be welcome!

from react-testing-library.

kentcdodds avatar kentcdodds commented on May 18, 2024

Any chance you could try to reproduce this in a codesandbox like this one? Then we could probably help you better.

from react-testing-library.

granmoe avatar granmoe commented on May 18, 2024

No problem, I created one here. Sorry my initial issue was a little scant on details--I wasn't sure if this was the right repo to create it in.

I had been thinking that this may be due to running in node vs the browser before submitting the issue, and now I see that it works in codesandbox, which I think confirms those suspicions (it just bundles the code using its crazy magic and then executes that in the browser, right?). I'm going to make a standalone project out of that sandbox, and then I'll share it here.

from react-testing-library.

granmoe avatar granmoe commented on May 18, 2024

What! This makes no sense.

So now I'm wondering what the difference is between running tests via react-tests and running them in my project using jest.

I see this in the test script from the codesandbox project: "test": "react-scripts test --env=jsdom". I wonder if jsdom is what's making it work?

from react-testing-library.

granmoe avatar granmoe commented on May 18, 2024

On a sidenote, I'm once again blown away by how awesome codesandbox is 😄

from react-testing-library.

granmoe avatar granmoe commented on May 18, 2024

Thanks for your help so far! I tried your suggestion, and I still get issues, but I got closer to what the actual issue is. I'll need to revise my codesandbox and possibly re-open this if that's OK. The problem only happens if I return <Fragment>s from a function within the render of a component. Here's a snippet of the kind of code that fails:

      {pages.map(({ route, Component }) =>  (
          <Fragment key={route} forRoute={route}>
            <Component />
          </Fragment>
      ))}

Each item in pages is an object that just has a string ('route') and a component ('component'). Each one of those components is a connected component.

I'm going to make a new codesandbox and see if I can repro this now that I know more about the scenario that causes it.

from react-testing-library.

kentcdodds avatar kentcdodds commented on May 18, 2024

No worries. Keep at it. You can do it!

from react-testing-library.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.