Jest for react-notification - reactjs

I updated my React project to react-notifactions(https://www.npmjs.com/package/react-notifications)
While writing jest cases, for alert("Login Failed"), I mocked using window.alert = jest.fn().
Now as I updated to react-notifaction like below
NotificationManager.error(Login failed!) how do I mock it

You might want to check if your Jest configuration is aware that it's testing for browsers vs Node.
https://facebook.github.io/jest/docs/en/configuration.html#browser-boolean

Related

How can I use both jest-dom matchers and enzyme matchers in the same repo?

We have been using Enzyme for a long time to test our react components but have started the migration to React Testing Library step by step.
When trying to use the toHaveValue matcher in a RTL test it failes since the enzyme matcher with the same name is called instead. Is there a way I can "tell" a specific test file to use the matchers from jest-dom instead?
I have added both the enzyme and jest-dom matchers to my repository according to the installation docs.
The toHaveValue() assertion is provided by react-testing-library's jest-dom (https://github.com/testing-library/jest-dom#tohavevalue).
You should be able to force the jest-dom matchers (assertions) "before" whichever ones you have loaded previously (enzyme's, for example) by putting
import "#testing-library/jest-dom/extend-expect";
at the top of your test.
See also:
https://github.com/testing-library/react-testing-library/issues/379 and
https://github.com/facebook/jest/issues/6243
A similar issue:
https://github.com/testing-library/jest-dom/issues/208

Making assertions with mocha and chai when using react-testing-library?

I would like to add React Testing Library to a project that uses mocha and chai for unit tests. I am a bit stumped when it comes to writing assertions, most examples make use of jest and jest-dom. I am really keen to use assertions like toBeInTheDocument or toHaveTextContent
I am wondering if there is a chai plugin that is a suitable replacement for jest-dom?
or what alternative solutions you have found for using React Testing Library with mocha and chai?
chai-dom is an alternative. It's a plugin that provides a subset of assertions on the dom. Unfortunately, the assertions are not as declarative or extensive as jest-dom.
Using chai-dom, we'd use exist in place of toBeInTheDocument and text in place of toHaveTextContent.

How can I get the real DOM in complete browser environment by using Jest?

I want to test something like layout using Jest and Enzyme. But Jest uses JSDOM to simulate the real browser environment.
I can only use Jest in this old project. How can I do it?
Here are some descriptions in JSDOM: https://github.com/jsdom/jsdom#unimplemented-parts-of-the-web-platform

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;

Resources