Jest test Cannot read property 'Object.<anonymous>' of null - reactjs

/Users/***/Desktop/projects/***/node_modules/jest-runtime/build/index.js:517
const wrapper = this._environment.runScript(transformedFile.script)[
Sometimes if i'm run npm test jest given this error.

It can be caused by running an Animated animation in React Native. Not running that animation when jest is detected is the best solution I found for now. I tried not to run animation, and the error gone.

This happens to me when not shallow rendering a component in a test.
My guess is that it's caused by the rendering of the children components, so I mocked the parent component of the render function and the tests started running fine.
You have a great example of mocking children components here: https://jestjs.io/docs/en/es6-class-mocks

Related

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 ?

Snapshot tests className with emotion/styled

Using react, jest, react-testing-library, emotion
Everytime I update a component the className in my snapshot tests updates because of how emotion/styled uses hash for classNames.
This keeps breaking my snapshot tests. Is it possible to make the classNames more static for testing?
I found this https://emotion.sh/docs/testing but it kinda does the opposite of what I want with adding the css to the snapshot tests..

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 simulate a React component that uses the document object?

I am currently writing unit tests for a react component in my application. The react component utilize the DOM api such as document.getElementById().
When I use shallow() or mount() from enzyme, I get an error saying that the document object is null:
I thought that jest already comes with jsdom as a headless browser. How can I fixed this issue to access a fake dom object?
First of all, the component preferably shouldn't use DOM directly because this affects testability and SSR. Rven if it uses, it preferably should use relative queries, not document.
JSDOM is not headless browser but Node.js environment that emulates browser environment.
Jest uses JSDOM by default. document cannot be null, unless it was specifically assigned to it somewhere. If Jest were configured to not use JSDOM, it would throw document is not defined error.
The error states that it is document.getElementById() result that is null, not document.
document.getElementById is supposed to be mocked as any other function:
jest.spyOn(document, 'getElementById').mockReturnValueOnce({ value: '...' });
Document can be simulated as follows:
const sampleHtml = `<html><body><h1>My First Heading</h1><p>My first paragraph.</p></body></html>`;
document.body.innerHTML = sampleHtml;

Getting TypeError when using Jest to test React Components

I'm trying to wire up Jest testing with React and I've copied and pasted everything from the Using Jest with React Tutorial into a demo project and I'm getting a TypeError saying that some Object in node_modules/react/lib/ReactUpdates.js doesn't have the function getPooled(). As far as I can tell this is happening at var React = require('react/addons') inside the CheckboxWithLabel-test.js file. I feel like this is something really simple that I'm just overlooking but I can't find anything.

Resources