Jest tests leaking due to improper teardown

As suggested in the warning, add –detectOpenHandles option to jest’s script in package.json file: “scripts”: { “test”: “jest –watchAll –detectOpenHandles” } Dont forget to stop then start the server ! This solution can work whatever your problem. But, according to your case, your problem is coming from the redis connection. You need to close redis … Read more

Mocking react-router-dom hooks using jest is not working

This works for me to mock useParams and change values for each unit test within the same file: import React from “react”; import { render } from “@testing-library/react”; import Router from “react-router-dom”; import Component from “./Component”; jest.mock(“react-router-dom”, () => ({ …jest.requireActual(“react-router-dom”), useParams: jest.fn(), })); const createWrapper = () => { return render(<Cases />); }; describe(“Component … Read more

Testing react component enclosed in withRouter (preferably using jest/enzyme)

Normally if we try to test such components we won’t be able to render it as it is wrapped within WithRouter (WithRouter is a wrapper over a component which provides Router props like match, route and history to be directly used within the component). module.exports = withRouter(ManageProfilePage); To render such components, we have to explicitly … Read more

Using Jest property matchers on arrays of objects

Version Info As is noted in the question, property matchers were introduced in Jest 23.0.0. Note that apps bootstrapped with create-react-app as of today (Aug 5, 2018) are still < 23. OBJECT Here is an example using a property matcher for a single object: test(‘sportsBallPerson’, () => { expect(sportsBallPeople[0]).toMatchSnapshot({ createdAt: expect.any(Date) }) }); The snapshot … Read more

How to restore a mock created with jest.mock()?

mockFn.mockRestore only works for a mock function created with jest.spyOn: const obj = { func: () => ‘original’ } test(‘func’, () => { const mock = jest.spyOn(obj, ‘func’); mock.mockReturnValue(‘mocked’); expect(obj.func()).toBe(‘mocked’); // Success! mock.mockRestore(); expect(obj.func()).toBe(‘original’); // Success! }) jest.spyOn wraps the original function and provides mockRestore as a way to restore the original function. jest.mock calls … Read more

How to assert function invocation order in jest

The solution by clemenspeters (where he wanted to make sure logout is called before login) works for me: const logoutSpy = jest.spyOn(client, ‘logout’); const loginSpy = jest.spyOn(client, ‘login’); // Run actual function to test await client.refreshToken(); const logoutOrder = logoutSpy.mock.invocationCallOrder[0]; const loginOrder = loginSpy.mock.invocationCallOrder[0]; expect(logoutOrder).toBeLessThan(loginOrder)

How should I test React Hook “useEffect” making an api call with Typescript?

There are two issues at play here Asynchronous call to setData setData gets called in a Promise callback. As soon as a Promise resolves, any callbacks waiting for it get queued in the PromiseJobs queue. Any pending jobs in the PromiseJobs queue run after the current message has completed and before the next one begins. … Read more

React testing library: Test attribute / prop

jest-dom toHaveAttribute assertion asserts item attribute while the test tries to test item prop. item prop won’t necessarily result in item attribute, and since it’s non-standard attribute it most probably won’t. react-testing-library propagates functional testing and asserts resulting DOM, this requires to be aware of how components work. As can be seen here, item props … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)