How to add @testing-library/jest-dom to every testing file in svelte?

1k views Asked by At

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.

2

There are 2 answers

0
Lin Chen On

Your node-module should have quotes.

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

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

0
David On

Have you tried the following?

"jest":{
     setupFilesAfterEnv: ['@testing-library/jest-dom/extend-expect']
}