run following command:
yarn add -D jest ts-jest ts-node @types/jest @testing-library/jest-dom @testing-library/react jest-environment-jsdom
jest
:test(), describe(), expect(), expect(value).toBe(4)
@testing-library/jest-dom
:expect(element).toBeInTheDocument()
add custom matchers related to the dom@testing-library/react
:render(), screen()
- query single element:
- getBy
- queryBy
- findBy
- query multiple elements:
- getAllBy
- queryAllBy
- findAllBy
- query single element:
create a file setup-jest.ts
, put the following to it:
import "@testing-library/jest-dom"
create a file jest.config.ts
, put the following to it:
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: "ts-jest",
testEnvironment: "jsdom,
setupFilesAfterEnv: ["<rootDir>/setup-jest.ts"],
};
add following script to package.json:
"test": "jest"
add following types to tsconfig.ts
{
"types": ["node", "jest", "@testing-library/jest-dom"]
}