Skip to content

set up Jest at Vite project

leetrue edited this page Nov 28, 2023 · 1 revision

Jest

package install

yarn add -D jest @types/jest ts-node ts-node ts-jest @testing-library/react identity-obj-proxy jest-environment-jsdom @testing-library/jest-dom jest-svg-transformer

edit package.json

"scripts":{
    "test": "jest"
 }

add jest config file

  • create jest.config.ts at root
export default {
  testEnvironment: "jsdom",
  transform: {
    "^.+\\.tsx?$": "ts-jest",
  },
  moduleNameMapper: {
    "^.+\\.svg$": "jest-svg-transformer",
    "\\.(css|less|sass|scss)$": "identity-obj-proxy",
  },
  setupFilesAfterEnv: ["<rootDir>/jest.setup.ts"],
};

add jest setup file

  • create jest.setup.ts at root
import '@testing-library/jest-dom';
  • if it doesn't work, try
import "@testing-library/jest-dom/extend-expect";
  • if lint error occurs, try this.
// .eslintrc.cjs
module.exports = {
  env: {
    ...,
  	module: "node",
  }
}

edit tsconfig

{
	"compilerOptions": {
            "esModuleInterop": true      
    }
}