How can I turn React Testing Library "not wrapped in act" warnings into Jest unit tests errors - reactjs

We are unit testing our react app using Jest and React Testing Library. And we get some of this "classic" errors :
console.error
Warning: A suspended resource finished loading inside a test, but the event was not wrapped in act(...).
When testing, code that resolves suspended data should be wrapped into act(...):
act(() => {
/* finish loading suspended data */
});
/* assert on the output */
But some Some developers simply ignore the error of this type. So, as time passes, the tests output is bloated with warning like that. And our tests feel less confident.
Is there a config in Jest or in React Testing Library that turns this kind of warning in error and makes the unit tests fail ?
I search in Google and found a lot of (good) articles to solve the warning but not to turn it into real error.

You can use the [jest-fail-on-console]: https://www.npmjs.com/package/jest-fail-on-console npm package to make the tests fail on console.error, console.warning.

Related

Jest integration Tests break on Pipeline

Hello im trying to find to build a application on my pipeline using integrations tests with Jest version 27.5.1 as well as jest-cli 27.5.1 all the tests work on my localhost however when my code runs through the Pipeline it shows the following Error
FetchError: request to http://localhost/callback?usid= failed, reason: connect ECONNREFUSED 127.0.0.1:80
and if i add a skip to this test the next test breaks as well
I've tried reading the documentation of jest to see if the root of the problem could be syntax i changed the Url where im doing the request for the API and also cleared added on the function afterEach() server.resetHandlers() as well as LocalStorage.clear() to clear up anything that might be considered Old data

An update to MsalAuthenticationTemplate ran an effect , error when writing unit test with jest in reactjs

I am writing unit tests with jest, my unit tests are running but I am always getting below error.
Due to this error, it does not show coverage and test description which ran successfully
console.error
Warning: An update to MsalAuthenticationTemplate ran an effect, but was not wrapped in act(...).
When testing, code that causes React state updates should be wrapped into act(...):
act(() => {
/* fire events that update state */
});
/* assert on the output */
This ensures that you're testing the behavior the user would see in the browser. Learn more at https://reactjs.org/link/wrap-tests-with-act
at MsalProvider (C:\XXXXXX\node_modules\#azure\msal-react\src\MsalProvider.tsx:25:31)
at ThemeProvider (C:\XXXXXX\node_modules\#material-ui\core\node_modules\#material-ui\styles\ThemeProvider\ThemeProvider.js:48:24)
at Provider (C:\XXXXXX\node_modules\react-redux\lib\components\Provider.js:21:20)
anyone faced same issue and has the sample mocking of the MsalAuthenticationTemplate ?

React enzyme jest test cases are failing with antv g2plot

I'm using the React Enzyme Jest module to test my React app which contains the antv/g2plot module for charts. While I am using the module, the test case isn't running but after removing it, it's working perfectly fine. There is a problem with the model I think. Do you know any possible solution?
I'm trying to test the file which is using the antv/g2plot module for chart while testing. It's failing and giving a random error which is not relevant (the error variable not defined in my React app). If I try to run the test without this file it's running pretty well but with it it returns this error.
I'm the member of G2Plot dev-team. When you write unit test for chart with canvas, you should:
mock the canvas API, see https://github.com/hustcc/jest-canvas-mock
give the real document(include canvas) env, see https://github.com/hustcc/jest-electron
G2Plot use jest-electron for unit test and debugger.

React UnitTest with Jest and Enzyme

I was surprised that many blogs are writing unit tests for React Applications using both Jest and Enzyme.
Cant, we write unit tests for React applications by using the only jest or by the only Enzyme??
If yes, to get a component we use shallow() in enzyme but what we use in Jest?
I think you didn't fully understand the concepts of jest and enzyme.
Jest
TL;DR: Jest is just a test runner on steroids for JavaScript (not just react).
The main functionality of jest is to execute JavaScript tests. It gives you tools to organize your tests, e.g. using describe, it or test blocks. jest comes with a lot of build-in assertions (e.g. expect(actual).toBe(expected)) to help you identifying failing and succeeding tests. On top of that jest comes with additional features, for example it allows you to easily mock functions or even complete modules.
jest is not bound to react in general, but does quite well in combination with react, since it is maintained by facebook just as react itself. So it's basically almost everytime the right joice and also recommended by react.
Enzyme
TL;DR: Collection of utilities to simplify testing/rendering react components.
Technically you don't need Enzyme or any other framework to test react components (only jest). react already exposes react-dom/test-utils but it's really cumbersome to work with those without properly wrapping and simplyfing its API. And that's exactly what enzyme is doing. It basically puts a layer of abstraction over react-dom/test-utils and adds a bunch of utilities, so you don't need to worry about implementing them yourself.
I highly recommend you to look at react-testing-library, an alternative to enzyme, and read "Testing Implementation Details" blog post authored by its founder.
react-testing-library is, like jest, recommended by react.
Jest and Enzyme are not comparable.
Jest don't give you rich API/function to access dom elements like enzyme but it does other important tasks like when you hit command npm test or npm run test jest collect all the test file eg all files ending with .test.js run each test cases within these files and show the result in the console like below its jest responsibility.
Jest is a testing framework or test runner which runs all the test files in one go, enzyme is a utility or library consider this a smaller thing compare to jest it gives many functions to access dom easily like shallow, mount, find, children, etc...

How to debug failing react 16 components inside a jest snapshot test

Aloha,
I often get an error message when running a jest snapshot test where on sub component breaks. React itself writes the following warning, but keeps the thrown error message:
An error was thrown inside one of your components, but React doesn't know what it was. This is likely due to browser flakiness. React does its best to preserve the "Pause on exceptions" behavior of the DevTools, which requires
some DEV-mode only tricks. It's possible that these don't work in your browser. Try triggering the error in production mode, or switching to a modern browser. If you suspect that this is actually an issue with React, please file an issue.
That message is not really useful and I was wondering if there is a certain environment variable I could set that would cause react to just pass the thrown error on.

Resources