mingxingwang
mingxingwang

Reputation: 339

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

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.

Upvotes: 0

Views: 1317

Answers (2)

Lin Chen
Lin Chen

Reputation: 13

Your node-module should have quotes.

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

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

Upvotes: 1

David
David

Reputation: 4857

Have you tried the following?

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

Upvotes: 2

Related Questions