question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to test async componentDidMount()

See original GitHub issue

Do you want to request a feature or report a bug?

Question.

What is the current behavior?

I’m trying to test a statefull component (Applications) that has async componentDidMount();

import React from 'react';
import { create } from 'react-test-renderer';
import Applications from './Applications';

describe("Applications", () => {
    const renderComponent = (props = {}) => {
        const component = create(<Applications {...props} />);
        return component.getInstance();
    } 
      
    it("renders applications", () => {
        const instance = renderComponent();
        // Here i get "Invariant Violation: The `document` global was defined when React was initialized, but is not defined anymore. ..." error
    });
});

I tried to provide a callback (done()) to the test and use setImmediate(() => { done(); }, but I still get the error. If I don’t call setImmediate() but use the callback variant of the test, I get “Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.” error.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
hg-pyuncommented, Jan 24, 2019

I have a question. When do you use async componentDidMount?

0reactions
stonemonkeycommented, Jan 25, 2019

Thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing with React's Jest and Enzyme when async ...
Solution: 1: use the async/await syntax. 2: Use mount (no shallow). 3: await async componentLifecycle. For ex: test(' ',async () => { const ......
Read more >
How to Test Asynchronous Methods with React Testing Library
The question is about methods called in componentDidMount but the testing strategy is the same if you use Hooks;; If you are uncertain...
Read more >
Jest|Enzyme|React testing with Async ComponentDidMount()
flushPromises() solves the bug or feature request depending how you look at it. Background: I was building a feature out requiring asynchronous fetch()...
Read more >
Testing component with async componentDidMount #1587
When I have to wait for some data in componentDidMount , I can't test that after data is fetched component rendered with expected...
Read more >
Testing Async componentDidMount - CodeSandbox
React from "react"; ; App extends React.Component { ; }; ; componentDidMount() { ; console.log("calling fetch");.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found