0

Started a new create-react-app which comes with all the necessary react testing lib stuff pre installed:

"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",

i have tried to write basic test for one of my components:

import "@testing-library/jest-dom";
import React from "react";
import { render } from "@testing-library/react";
import SomeComponent from "./SomeComponent";

describe("helllo", () => {
  it("renders SomeComponent correctley", () => {
    const { queryAllByPlaceholderText } = render(<SomeComponent />);

    console.log(test);
    expect(test).toBeTruthy();
  });
});

But i am getting this error message in the console:

Test suite failed to run

    Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

    Here's what you can do:
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/en/configuration.html

    Details:


       6 | describe("helllo", () => {
       7 |   it("renders SomeComponent correctley", () => {
    >  8 |     const { queryAllByPlaceholderText } = render(<SomeComponent />);
         |                                                  ^

My react version is:

 "react": "^17.0.2",

I have researched this issue and keep on bumping into bable stuff but i though the react-testing-lib should be working out of box...?

John
  • 1,595
  • 4
  • 21
  • 44
  • Does this answer your question? [Jest encountered an unexpected token](https://stackoverflow.com/questions/51994111/jest-encountered-an-unexpected-token) – Code-Apprentice Jun 23 '21 at 23:17
  • @Code-Apprentice i tried some of the solutions on that post but they all intreduce new issue: `Test suite failed to run Cannot find module './SomeComponent' from 'src/components/TopFactsContainer/TopFactsContainer.test.js'` `> 4 | import SomeComponent from "./SomeComponent";` – John Jun 23 '21 at 23:30
  • This is very common in programming. Solving one error leads to another error. You keep following these until you get to a working solution. If you still need help, post a new question about that new error. – Code-Apprentice Jun 24 '21 at 15:00

0 Answers0