threepointone/react-act-examples

Disabling the warning in 16.8

aravindet opened this issue ยท 7 comments

Hi Sunil! ๐Ÿ˜„

I know this is a somewhat annoying ask, but is there a good way to suppress the act() warning until 16.9 is released?

(The code I'm testing ends up creating a chain of promises, which Jest's fake timers won't run synchronously until this is merged.)

Why don't you use 16.9 alpha version? The async act should be able to take care of chain of promises.

I definitely agree that 16.9 would have been ideal. There's of course some circumstances specific to my project (although it can't be that rare) that factors in:

  • This needs to go into production, and the app in question has a lot of paying users so using the alpha is not really feasible.
  • Our CI is set up to fail if build/test produces warnings to console. I can't merge my otherwise working feature unless I can suppress this warning. ๐Ÿ˜ข
  • The thing I'm working on involves some third-party libraries that schedule asynchronous tasks in a way that cannot really be trapped by Jest timer mocks.

This needs to go into production, and the app in question has a lot of paying users so using the alpha is not really feasible.

I would say this is more of the scarecrow that a real issue. React is very stable and "alpha" doesn't mean "it's broken", but "there are some new things we want users to check out before rolling out" instead. I would even say it's intentional to test these releases in production. Imagine there is a bug that gets revealed only in your code. The "stable" 16.9 comes out, you will use it and bam, bug hits you anyway.

Furthermore, I believe that particular release is focused on act only, so it should be pretty safe regarding the actual production code. Well, if you are unwilling to put forward some trust, I am afraid you have to wait.

Alternatively, you can try jest --silent which should hide pretty much anything including act warnings. Or figure out some other way on suppressing console output in tests, it's not that hard really, eg. jest-mock-console.

So is there no way to just disable the "You've caused a rerender outside of act()" warning with no other effects?

AFAIK there is no such thing as it would be too tempting to just disable instead of fixing the situation :)

@aravindet does jest-mock-console not unblock you? you can use it in your warning tests, and assert that only the act warning fires (and then remove it once 16.9 is released)

@threepointone that might work, thanks!

They are already hijacking the console to add the "fail CI on output" behaviour, but I think I can double-hijack it during my tests ๐Ÿ‘