vite+react项目使用jest测试自定义hook

768 阅读1分钟

vite+react项目使用jest测试自定义hook

前言

在react项目中,我们常会使用jest来进行,虽然create-react-app自带了很多jest,但是目前很多人习惯使用vite进行项目开发, 这需要自己手动引入jest,可能会遇到一些问题,特别是配合TypeScript使用时,这里分享一个模板,应该能解决绝大部分问题。

安装依赖

npm install jest @types/jest identity-obj-proxy 
npm install @babel/preset-env @babel/preset-react @babel/preset-typescript 
npm install @testing-library/jest-dom @testing-library/react
pnpm install jest @types/jest identity-obj-proxy 
pnpm install @babel/preset-env @babel/preset-react @babel/preset-typescript 
pnpm install @testing-library/jest-dom @testing-library/react
yarn install jest @types/jest identity-obj-proxy 
yarn install @babel/preset-env @babel/preset-react @babel/preset-typescript 
yarn install @testing-library/jest-dom @testing-library/react

配置文件

package.json

"scripts": {
   .......
  "test": "jest",
  "test:coverage": "jest --coverage"
},

babel.config.js

项目根目录下新建babel.config.js文件。

module.exports = {
    presets: ['@babel/preset-env', '@babel/preset-react', '@babel/preset-typescript'],
};

jest.config.js

项目根目录下新建jest.config.js文件

const obj =  {
    clearMocks: true,
    collectCoverage: true,
    coverageDirectory: "coverage",
    coverageProvider: "v8",
    testEnvironment: "jsdom",
    moduleNameMapper: {
        '^.+\.(css|less)$': 'identity-obj-proxy',
    },
}

export default obj

编写单元测试

新建xxx.test.ts文件,在其中编写想要进行的单元测试即可。

运行

直接运行直接在package.json中注入的脚本。

test

解决报错

运行test脚本可能会遇到各种各样的报错,下面列举几个常见的报错和可能的解决办法。

报错一

The error below may be caused by using the wrong test environment, see https://jestjs.io/docs/configuration#testenvironment-string.
Consider using the "jsdom" test environment.

检查jest.config.js中是否有testEnvironment: "jsdom"

报错二

 Test suite failed to run

 You appear to be using a native ECMAScript module configuration file, which is only supported when running Babel asynchronously.
 at C:\Users\Administrator\Desktop\react_hooks\babel.config.js

babel.config.js文件名修改为babel.config.cjs

报错三

Test suite failed to run 

Jest encountered an unexpected token 
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax. 
Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration. 
By default "node_modules" folder is ignored by transformers. 

Here's what you can do: 
  • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
  • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config. 
  • If you need a custom transformation specify a "transform" option in your config. 
  • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

1)检查jest.config.js中是否有moduleNameMapper: { '^.+\.(css|less)$': 'identity-obj-proxy', },

2)检查babel.config.jspresets配置是否有遗漏。