'Use both enzyme and react-testing-library

So, we started implementing testing in our app, I used enzyme and my coworkers used react-testing-library. Of course the setupTests.js file is different so either their tests broke or mine. Is there a way to use both or should I migrate the tests and use only one library?



Solution 1:[1]

You can use both of them together and incrementally migrate your enzyme tests to react-testing-library. Update your jest config with path to setupTests.js as:

// other config
setupFiles: ["react-app-polyfill/jsdom"],
setupFilesAfterEnv: ["<rootDir>/scripts/jest/setupTests.js"],

// rest of the config

And in the setupTests.js file, where you'd have configured Adapter for enzyme, add configuration for react-testing-library as:

import { configure } from "enzyme";
import Adapter from "enzyme-adapter-react-16";
import { configure as RTLConfigure } from "@testing-library/react";

const TEST_ID = "custom-test-id";
RTLConfigure({ testIdAttribute: TEST_ID }); // if you have a different test attribute for elements on dom

configure({ adapter: new Adapter() }); // enzyme configuration

You can find more info on setup here

Solution 2:[2]

Yes, on your setupTests.js just import import '@testing-library/jest-dom';

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 pritam
Solution 2 Drew Cordano