0

Our project has both enzyme and testing-library. My goal is to overwrite getByTestId to my custom. But when I'm adding the second configuration to my setup.test.js - some tests become failing with Found multiple elements with the text.

setup.test.js:

const { configure } = require("enzyme");
const Adapter = require("enzyme-adapter-react-16");
import { configure as conf } from "@testing-library/react";

window.__config = {};
conf({ testIdAttribute: "data-my-test-id" });
configure({ adapter: new Adapter() });

My versions:

"enzyme": "^3.11.0",

"@testing-library/react": "^11.0.4",

Dima Dorogonov
  • 2,297
  • 1
  • 20
  • 23
  • The error message indicates that you're using `getByText` somewhere which will throw an error if it finds multiple elements in the DOM matching the provided text. Are you sure this is related to your config setup? Doesn't seem like it to me at first glance – Carle B. Navy Oct 09 '20 at 11:16
  • Yes, tests code is the same. Errors appear after this line `import { configure as conf } from "@testing-library/react";` – Dima Dorogonov Oct 09 '20 at 13:50
  • Can you share a little bit more of your code to have a reproducible example? – Carle B. Navy Oct 10 '20 at 14:35
  • Sure, so from my package.json I execute `jest --config myConf.js` which has `setupFiles: ''setup.test.js"`. Setup, for now, use only enzyme configure function and all 2000+ pass fine. After I'm just adding there 1 line like `import { configure as conf } from "@testing-library/react"`- near 20 tests fail with "Found multiple elements with the text". Vesrions: "react": "^16.13.1", "enzyme": "^3.11.0" and "@testing-library/react": "^9.4.1". Anything else? The Code of tests was not changed. – Dima Dorogonov Oct 13 '20 at 13:27

1 Answers1

0

This is happening because expected cleanup doesn't work. Move below configuration to a new file, let's say "env-setup.js"

import { configure as conf } from "@testing-library/react";
conf({ testIdAttribute: "data-my-test-id" });

Now, in jest.config.js:

{
  setupFilesAfterEnv: ['<existing_setup_file_path>', '<path>/env-setup.js']
}
ruhi
  • 1
  • 1