Skip to content

Instantly share code, notes, and snippets.

@luistak
Last active December 9, 2023 19:20
Show Gist options
  • Save luistak/f344b013047d1bb8afcbee9af40d9b21 to your computer and use it in GitHub Desktop.
Save luistak/f344b013047d1bb8afcbee9af40d9b21 to your computer and use it in GitHub Desktop.
Testing-library Setup to a react tsdx library
  1. Install Testing library packages and types
yarn add --dev @testing-library/jest-dom @types/testing-library__jest-dom @testing-library/react @testing-library/user-event
  1. Create setupTests.ts file at /src with this content:
import '@testing-library/jest-dom/extend-expect';
  1. Add jest dom sixteen
yarn add --dev jest-environment-jsdom-sixteen
  1. Add this section in your package.json
  "jest": {
    "collectCoverageFrom": [
      "src/**/*.{ts,tsx}"
    ],
    "setupFilesAfterEnv": [
      "<rootDir>/src/setupTests.ts",
    ],
    "modulePaths": [
      "<rootDir>/src"
    ],
    "testEnvironment": "jest-environment-jsdom-sixteen"
  }
  1. Add helpfull eslint plugins
yarn add --dev eslint-plugin-jest-dom eslint-plugin-react-hooks eslint-plugin-testing-library
  1. Add this configs to your eslint.json
...
  "extends": [
    ...
    "plugin:jest-dom/recommended",
    "plugin:react-hooks/recommended",
    "plugin:testing-library/recommended",
    "plugin:testing-library/react",
    ...
  ],
  ...
  "plugins": [
    ...
    "react-hooks",
    "jest-dom",
    "testing-library",
    ...
  ],
  ...
  "overrides": [
    {
      ...
      "env": {
        "node": true,
        "commonjs": true,
        "browser": true,
        "jest": true
      }
    }
  ]
@Dhaiwat10
Copy link

Thank you. This helped!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment