0

I am trying to add the jest-dom library to every test file. I installed that package and made the "jestSetup.js" file and add the package.json as below.

"jest":{
   setupFilesAfterEnv:["<rootDir>/jestSetup.js"]
}

and jestSetup.js is

import @testing-library/jest-dom;
console.log("jest-dom")

the test file is

import { render } from "@testing-library/svelte";
...
console.log("test")
expect(getByText("success notification")).toHaveClass("success");
...

But it says toHaveClass is not defined error And the result of console.log is "jest-dom, jest" I tested it with jest.config.js again but the result is the same. I am not sure why it happens.

mingxingwang
  • 169
  • 4
  • 17

2 Answers2

2

Have you tried the following?

"jest":{
     setupFilesAfterEnv: ['@testing-library/jest-dom/extend-expect']
}
David
  • 4,191
  • 2
  • 31
  • 40
1

Your node-module should have quotes.

Change this:
import @testing-library/jest-dom;

To this:
import '@testing-library/jest-dom';

Zach Jensz
  • 3,650
  • 5
  • 15
  • 30
Lin Chen
  • 13
  • 4