How to test async componentDidMount()
See original GitHub issueDo 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:
- Created 5 years ago
- Comments:6
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I have a question. When do you use async
componentDidMount
?Thanks.