Mar*_*cos 12 typescript jestjs monorepo react-testing-library
我正在建立一个 monorepo,在其中构建一个反应应用程序,我将在其中使用打字稿。我们想使用 jest 来测试后端功能,使用 React-Testing-Library 来测试前端功能。我应该安装 jest 并在根目录中添加配置文件还是直接在“后端”包中添加配置文件?
与另一种相比,这样做的优点/缺点是什么?
感谢您的帮助。
只需在根目录中安装 jest 包即可。然后,添加项目 [array<string | ProjectConfig>]\n文件中的配置jest.config.js
:
\n\nJest 将同时在所有指定项目中运行测试。这对于单一存储库或同时处理多个项目时非常有用。
\n
我的项目使用lerna来管理 mono 存储库。
\n文件夹结构:
\n\xe2\x9a\xa1 tree -L 2 -I \'node_modules|examples\' \n.\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 LICENSE\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 coverage\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 clover.xml\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 coverage-final.json\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 lcov-report\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 lcov.info\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 jest.config.js\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 jest.setup.js\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 lerna.json\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 package-lock.json\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 package.json\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 packages\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 redux-saga-examples\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 redux-toolkit-example\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 tsconfig.json\n\n5 directories, 10 files\n
Run Code Online (Sandbox Code Playgroud)\n有两个包:redux-saga-examples
和redux-toolkit-example
。这些包中有很多测试文件。
在package.json
根中:
{\n "name": "root",\n "private": true,\n "scripts": {\n "bootstrap": "lerna bootstrap",\n "clean": "lerna clean",\n "test": "jest"\n },\n "devDependencies": {\n "@types/jest": "^26.0.24",\n "lerna": "^4.0.0",\n "jest": "^27.0.6",\n "ts-jest": "^27.0.4",\n "ts-node": "^9.1.1",\n "typescript": "^4.3.5",\n "prettier": "^2.3.1"\n },\n "dependencies": {\n "axios": "^0.21.4"\n }\n}\n
Run Code Online (Sandbox Code Playgroud)\njest.config.js
:
const reduxSagaExamplesPkg = require(\'./packages/redux-saga-examples/package.json\');\nconst RTKExamplesPkg = require(\'./packages/redux-toolkit-example/package.json\');\n\nmodule.exports = {\n verbose: true,\n projects: [\n {\n preset: \'ts-jest/presets/js-with-ts\',\n testEnvironment: \'jsdom\',\n setupFilesAfterEnv: [\'./jest.setup.js\'],\n displayName: reduxSagaExamplesPkg.name,\n testMatch: [\'<rootDir>/packages/redux-saga-examples/**/?(*.)+(spec|test).[jt]s?(x)\'],\n },\n {\n preset: \'ts-jest/presets/js-with-ts\',\n testEnvironment: \'jsdom\',\n setupFilesAfterEnv: [\'./jest.setup.js\'],\n displayName: RTKExamplesPkg.name,\n testMatch: [\'<rootDir>/packages/redux-toolkit-example/**/?(*.)+(spec|test).[jt]s?(x)\'],\n },\n ],\n};\n
Run Code Online (Sandbox Code Playgroud)\n现在,您可以npm t
在项目的根目录中运行 npm 脚本来运行所有测试。
归档时间: |
|
查看次数: |
15591 次 |
最近记录: |