@qlover/create-app 1.1.0 → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +32 -0
- package/dist/index.cjs +9 -9
- package/dist/index.js +9 -9
- package/package.json +5 -4
- package/dist/configs/_common/.editorconfig +0 -23
- package/dist/configs/_common/.env.template +0 -13
- package/dist/configs/_common/.gitattributes +0 -2
- package/dist/configs/_common/.github/workflows/general-check.yml +0 -41
- package/dist/configs/_common/.github/workflows/release.yml +0 -81
- package/dist/configs/_common/.gitignore.template +0 -64
- package/dist/configs/_common/.husky/commit-msg +0 -3
- package/dist/configs/_common/.husky/pre-commit +0 -3
- package/dist/configs/_common/.prettierignore +0 -17
- package/dist/configs/_common/.prettierrc.js +0 -7
- package/dist/configs/_common/.vscode/extensions.json +0 -9
- package/dist/configs/_common/.vscode/react.code-snippets +0 -19
- package/dist/configs/_common/.vscode/settings.json +0 -16
- package/dist/configs/_common/commitlint.config.js +0 -10
- package/dist/configs/_common/package.json.template +0 -75
- package/dist/configs/node-lib/eslint.config.js +0 -50
- package/dist/templates/next-app/.env.template +0 -25
- package/dist/templates/next-app/.prettierignore +0 -58
- package/dist/templates/next-app/README.en.md +0 -130
- package/dist/templates/next-app/README.md +0 -130
- package/dist/templates/next-app/config/IOCIdentifier.ts +0 -74
- package/dist/templates/next-app/config/Identifier/api.ts +0 -41
- package/dist/templates/next-app/config/Identifier/common/admint.table.ts +0 -69
- package/dist/templates/next-app/config/Identifier/common/common.ts +0 -90
- package/dist/templates/next-app/config/Identifier/common/index.ts +0 -3
- package/dist/templates/next-app/config/Identifier/common/validators.ts +0 -34
- package/dist/templates/next-app/config/Identifier/index.ts +0 -3
- package/dist/templates/next-app/config/Identifier/pages/index.ts +0 -7
- package/dist/templates/next-app/config/Identifier/pages/page.about.ts +0 -20
- package/dist/templates/next-app/config/Identifier/pages/page.admin.home.ts +0 -27
- package/dist/templates/next-app/config/Identifier/pages/page.admin.locales.ts +0 -266
- package/dist/templates/next-app/config/Identifier/pages/page.admin.user.ts +0 -293
- package/dist/templates/next-app/config/Identifier/pages/page.home.ts +0 -56
- package/dist/templates/next-app/config/Identifier/pages/page.login.ts +0 -159
- package/dist/templates/next-app/config/Identifier/pages/page.register.ts +0 -177
- package/dist/templates/next-app/config/adminNavs.ts +0 -19
- package/dist/templates/next-app/config/common.ts +0 -43
- package/dist/templates/next-app/config/cookies.ts +0 -23
- package/dist/templates/next-app/config/i18n/AboutI18n.ts +0 -14
- package/dist/templates/next-app/config/i18n/HomeI18n.ts +0 -24
- package/dist/templates/next-app/config/i18n/PageI18nInterface.ts +0 -51
- package/dist/templates/next-app/config/i18n/admin18n.ts +0 -75
- package/dist/templates/next-app/config/i18n/i18nConfig.ts +0 -16
- package/dist/templates/next-app/config/i18n/i18nKeyScheam.ts +0 -36
- package/dist/templates/next-app/config/i18n/index.ts +0 -7
- package/dist/templates/next-app/config/i18n/loginI18n.ts +0 -50
- package/dist/templates/next-app/config/i18n/register18n.ts +0 -44
- package/dist/templates/next-app/config/route.ts +0 -9
- package/dist/templates/next-app/config/theme.ts +0 -28
- package/dist/templates/next-app/docs/en/api.md +0 -387
- package/dist/templates/next-app/docs/en/component.md +0 -544
- package/dist/templates/next-app/docs/en/database.md +0 -496
- package/dist/templates/next-app/docs/en/development-guide.md +0 -727
- package/dist/templates/next-app/docs/en/env.md +0 -563
- package/dist/templates/next-app/docs/en/i18n.md +0 -287
- package/dist/templates/next-app/docs/en/index.md +0 -165
- package/dist/templates/next-app/docs/en/page.md +0 -457
- package/dist/templates/next-app/docs/en/project-structure.md +0 -176
- package/dist/templates/next-app/docs/en/router.md +0 -427
- package/dist/templates/next-app/docs/en/theme.md +0 -532
- package/dist/templates/next-app/docs/en/validator.md +0 -478
- package/dist/templates/next-app/docs/zh/api.md +0 -387
- package/dist/templates/next-app/docs/zh/component.md +0 -544
- package/dist/templates/next-app/docs/zh/database.md +0 -496
- package/dist/templates/next-app/docs/zh/development-guide.md +0 -727
- package/dist/templates/next-app/docs/zh/env.md +0 -563
- package/dist/templates/next-app/docs/zh/i18n.md +0 -287
- package/dist/templates/next-app/docs/zh/index.md +0 -165
- package/dist/templates/next-app/docs/zh/page.md +0 -457
- package/dist/templates/next-app/docs/zh/project-structure.md +0 -176
- package/dist/templates/next-app/docs/zh/router.md +0 -427
- package/dist/templates/next-app/docs/zh/theme.md +0 -532
- package/dist/templates/next-app/docs/zh/validator.md +0 -476
- package/dist/templates/next-app/eslint.config.mjs +0 -285
- package/dist/templates/next-app/make/generateLocales.ts +0 -32
- package/dist/templates/next-app/migrations/schema/LocalesSchema.ts +0 -15
- package/dist/templates/next-app/migrations/schema/UserSchema.ts +0 -38
- package/dist/templates/next-app/migrations/sql/1694244000000.sql +0 -21
- package/dist/templates/next-app/next.config.ts +0 -25
- package/dist/templates/next-app/package.json +0 -87
- package/dist/templates/next-app/postcss.config.mjs +0 -5
- package/dist/templates/next-app/public/favicon.ico +0 -0
- package/dist/templates/next-app/public/file.svg +0 -1
- package/dist/templates/next-app/public/globe.svg +0 -1
- package/dist/templates/next-app/public/locales/en.json +0 -182
- package/dist/templates/next-app/public/locales/zh.json +0 -182
- package/dist/templates/next-app/public/next.svg +0 -1
- package/dist/templates/next-app/public/vercel.svg +0 -1
- package/dist/templates/next-app/public/window.svg +0 -1
- package/dist/templates/next-app/src/app/[locale]/admin/AdminI18nProvider.tsx +0 -37
- package/dist/templates/next-app/src/app/[locale]/admin/layout.tsx +0 -42
- package/dist/templates/next-app/src/app/[locale]/admin/locales/page.tsx +0 -153
- package/dist/templates/next-app/src/app/[locale]/admin/page.tsx +0 -20
- package/dist/templates/next-app/src/app/[locale]/admin/users/page.tsx +0 -67
- package/dist/templates/next-app/src/app/[locale]/auth/layout.tsx +0 -18
- package/dist/templates/next-app/src/app/[locale]/auth/login/LoginForm.tsx +0 -126
- package/dist/templates/next-app/src/app/[locale]/auth/login/page.tsx +0 -90
- package/dist/templates/next-app/src/app/[locale]/auth/page.tsx +0 -8
- package/dist/templates/next-app/src/app/[locale]/auth/register/RegisterForm.tsx +0 -197
- package/dist/templates/next-app/src/app/[locale]/auth/register/page.tsx +0 -90
- package/dist/templates/next-app/src/app/[locale]/layout.tsx +0 -63
- package/dist/templates/next-app/src/app/[locale]/not-found.tsx +0 -24
- package/dist/templates/next-app/src/app/[locale]/page.tsx +0 -98
- package/dist/templates/next-app/src/app/api/admin/locales/create/route.ts +0 -13
- package/dist/templates/next-app/src/app/api/admin/locales/import/route.ts +0 -13
- package/dist/templates/next-app/src/app/api/admin/locales/route.ts +0 -20
- package/dist/templates/next-app/src/app/api/admin/locales/update/route.ts +0 -13
- package/dist/templates/next-app/src/app/api/admin/users/route.ts +0 -20
- package/dist/templates/next-app/src/app/api/ai/completions/route.ts +0 -32
- package/dist/templates/next-app/src/app/api/auth/callback/route.ts +0 -11
- package/dist/templates/next-app/src/app/api/callback/route.ts +0 -49
- package/dist/templates/next-app/src/app/api/locales/json/route.ts +0 -33
- package/dist/templates/next-app/src/app/api/user/login/route.ts +0 -10
- package/dist/templates/next-app/src/app/api/user/logout/route.ts +0 -8
- package/dist/templates/next-app/src/app/api/user/register/route.ts +0 -11
- package/dist/templates/next-app/src/app/manifest.ts +0 -16
- package/dist/templates/next-app/src/app/robots.txt +0 -2
- package/dist/templates/next-app/src/base/cases/AdminPageManager.ts +0 -28
- package/dist/templates/next-app/src/base/cases/AppConfig.ts +0 -40
- package/dist/templates/next-app/src/base/cases/Datetime.ts +0 -18
- package/dist/templates/next-app/src/base/cases/DialogErrorPlugin.ts +0 -57
- package/dist/templates/next-app/src/base/cases/DialogHandler.ts +0 -116
- package/dist/templates/next-app/src/base/cases/InversifyContainer.ts +0 -45
- package/dist/templates/next-app/src/base/cases/NavigateBridge.ts +0 -32
- package/dist/templates/next-app/src/base/cases/RequestEncryptPlugin.ts +0 -77
- package/dist/templates/next-app/src/base/cases/ResourceState.ts +0 -17
- package/dist/templates/next-app/src/base/cases/RouterService.ts +0 -52
- package/dist/templates/next-app/src/base/cases/StringEncryptor.ts +0 -73
- package/dist/templates/next-app/src/base/cases/TranslateI18nUtil.ts +0 -53
- package/dist/templates/next-app/src/base/cases/ZodColumnBuilder.ts +0 -212
- package/dist/templates/next-app/src/base/port/AdminLayoutInterface.ts +0 -26
- package/dist/templates/next-app/src/base/port/AppApiInterface.ts +0 -36
- package/dist/templates/next-app/src/base/port/AppUserApiInterface.ts +0 -27
- package/dist/templates/next-app/src/base/port/I18nServiceInterface.ts +0 -25
- package/dist/templates/next-app/src/base/port/IOCInterface.ts +0 -33
- package/dist/templates/next-app/src/base/port/RouterInterface.ts +0 -11
- package/dist/templates/next-app/src/base/port/UserServiceInterface.ts +0 -25
- package/dist/templates/next-app/src/base/port/ZodBuilderInterface.ts +0 -8
- package/dist/templates/next-app/src/base/services/AdminPageEvent.ts +0 -26
- package/dist/templates/next-app/src/base/services/AdminPageScheduler.ts +0 -42
- package/dist/templates/next-app/src/base/services/AppApiRequester.ts +0 -67
- package/dist/templates/next-app/src/base/services/AppUserGateway.ts +0 -110
- package/dist/templates/next-app/src/base/services/I18nService.ts +0 -87
- package/dist/templates/next-app/src/base/services/ResourceService.ts +0 -139
- package/dist/templates/next-app/src/base/services/UserService.ts +0 -68
- package/dist/templates/next-app/src/base/services/adminApi/AdminLocalesApi.ts +0 -106
- package/dist/templates/next-app/src/base/services/adminApi/AdminUserApi.ts +0 -87
- package/dist/templates/next-app/src/base/services/appApi/AppApiPlugin.ts +0 -110
- package/dist/templates/next-app/src/base/services/appApi/AppUserApiBootstrap.ts +0 -52
- package/dist/templates/next-app/src/base/types/AppPageRouter.ts +0 -12
- package/dist/templates/next-app/src/base/types/PagesRouter.ts +0 -9
- package/dist/templates/next-app/src/core/bootstraps/BootstrapClient.ts +0 -76
- package/dist/templates/next-app/src/core/bootstraps/BootstrapServer.ts +0 -125
- package/dist/templates/next-app/src/core/bootstraps/BootstrapsRegistry.ts +0 -50
- package/dist/templates/next-app/src/core/bootstraps/IocIdentifierTest.ts +0 -26
- package/dist/templates/next-app/src/core/bootstraps/PrintBootstrap.ts +0 -18
- package/dist/templates/next-app/src/core/clientIoc/ClientIOC.ts +0 -68
- package/dist/templates/next-app/src/core/clientIoc/ClientIOCRegister.ts +0 -100
- package/dist/templates/next-app/src/core/globals.ts +0 -28
- package/dist/templates/next-app/src/core/serverIoc/ServerIOC.ts +0 -80
- package/dist/templates/next-app/src/core/serverIoc/ServerIOCRegister.ts +0 -66
- package/dist/templates/next-app/src/i18n/loadMessages.ts +0 -103
- package/dist/templates/next-app/src/i18n/request.ts +0 -31
- package/dist/templates/next-app/src/i18n/routing.ts +0 -35
- package/dist/templates/next-app/src/lib/supabase/client.ts +0 -8
- package/dist/templates/next-app/src/lib/supabase/conts.ts +0 -2
- package/dist/templates/next-app/src/lib/supabase/proxy.ts +0 -84
- package/dist/templates/next-app/src/lib/supabase/server.ts +0 -38
- package/dist/templates/next-app/src/pages/[locale]/about.tsx +0 -61
- package/dist/templates/next-app/src/pages/_app.tsx +0 -50
- package/dist/templates/next-app/src/pages/_document.tsx +0 -13
- package/dist/templates/next-app/src/proxy.ts +0 -33
- package/dist/templates/next-app/src/server/AppErrorApi.ts +0 -10
- package/dist/templates/next-app/src/server/AppPageRouteParams.ts +0 -110
- package/dist/templates/next-app/src/server/AppSuccessApi.ts +0 -7
- package/dist/templates/next-app/src/server/NextApiServer.ts +0 -61
- package/dist/templates/next-app/src/server/PagesRouteParams.ts +0 -145
- package/dist/templates/next-app/src/server/PasswordEncrypt.ts +0 -18
- package/dist/templates/next-app/src/server/ServerAuth.ts +0 -81
- package/dist/templates/next-app/src/server/SupabaseBridge.ts +0 -262
- package/dist/templates/next-app/src/server/UserCredentialToken.ts +0 -53
- package/dist/templates/next-app/src/server/controllers/AdminLocalesController.ts +0 -86
- package/dist/templates/next-app/src/server/controllers/AdminUserController.ts +0 -42
- package/dist/templates/next-app/src/server/controllers/LocalesController.ts +0 -36
- package/dist/templates/next-app/src/server/controllers/UserController.ts +0 -91
- package/dist/templates/next-app/src/server/port/AIControllerInterface.ts +0 -8
- package/dist/templates/next-app/src/server/port/AdminLocalesControllerInterface.ts +0 -21
- package/dist/templates/next-app/src/server/port/AdminUserControllerInterface.ts +0 -11
- package/dist/templates/next-app/src/server/port/CrentialTokenInterface.ts +0 -5
- package/dist/templates/next-app/src/server/port/DBBridgeInterface.ts +0 -36
- package/dist/templates/next-app/src/server/port/DBTableInterface.ts +0 -12
- package/dist/templates/next-app/src/server/port/LocalesControllerInterface.ts +0 -10
- package/dist/templates/next-app/src/server/port/LocalesRepositoryInterface.ts +0 -43
- package/dist/templates/next-app/src/server/port/PaginationInterface.ts +0 -6
- package/dist/templates/next-app/src/server/port/RouteParamsnHandlerInterface.ts +0 -18
- package/dist/templates/next-app/src/server/port/ServerAuthInterface.ts +0 -15
- package/dist/templates/next-app/src/server/port/ServerInterface.ts +0 -23
- package/dist/templates/next-app/src/server/port/UserRepositoryInterface.ts +0 -15
- package/dist/templates/next-app/src/server/port/UserServiceInterface.ts +0 -14
- package/dist/templates/next-app/src/server/port/ValidatorInterface.ts +0 -23
- package/dist/templates/next-app/src/server/repositorys/LocalesRepository.ts +0 -216
- package/dist/templates/next-app/src/server/repositorys/UserRepository.ts +0 -102
- package/dist/templates/next-app/src/server/services/AIService.ts +0 -45
- package/dist/templates/next-app/src/server/services/AdminAuthPlugin.ts +0 -21
- package/dist/templates/next-app/src/server/services/AdminLocalesService.ts +0 -20
- package/dist/templates/next-app/src/server/services/ApiLocaleService.ts +0 -137
- package/dist/templates/next-app/src/server/services/ApiUserService.ts +0 -29
- package/dist/templates/next-app/src/server/services/UserService.ts +0 -134
- package/dist/templates/next-app/src/server/validators/ExtendedExecutorError.ts +0 -6
- package/dist/templates/next-app/src/server/validators/LocalesValidator.ts +0 -145
- package/dist/templates/next-app/src/server/validators/LoginValidator.ts +0 -82
- package/dist/templates/next-app/src/server/validators/PaginationValidator.ts +0 -70
- package/dist/templates/next-app/src/server/validators/SignupVerifyValidator.ts +0 -68
- package/dist/templates/next-app/src/styles/css/antd-themes/_common/_default.css +0 -280
- package/dist/templates/next-app/src/styles/css/antd-themes/_common/dark.css +0 -233
- package/dist/templates/next-app/src/styles/css/antd-themes/_common/index.css +0 -3
- package/dist/templates/next-app/src/styles/css/antd-themes/_common/pink.css +0 -246
- package/dist/templates/next-app/src/styles/css/antd-themes/index.css +0 -4
- package/dist/templates/next-app/src/styles/css/antd-themes/menu/_default.css +0 -108
- package/dist/templates/next-app/src/styles/css/antd-themes/menu/dark.css +0 -67
- package/dist/templates/next-app/src/styles/css/antd-themes/menu/index.css +0 -3
- package/dist/templates/next-app/src/styles/css/antd-themes/menu/pink.css +0 -67
- package/dist/templates/next-app/src/styles/css/antd-themes/no-context.css +0 -34
- package/dist/templates/next-app/src/styles/css/antd-themes/pagination/_default.css +0 -34
- package/dist/templates/next-app/src/styles/css/antd-themes/pagination/dark.css +0 -31
- package/dist/templates/next-app/src/styles/css/antd-themes/pagination/index.css +0 -3
- package/dist/templates/next-app/src/styles/css/antd-themes/pagination/pink.css +0 -36
- package/dist/templates/next-app/src/styles/css/antd-themes/table/_default.css +0 -44
- package/dist/templates/next-app/src/styles/css/antd-themes/table/dark.css +0 -43
- package/dist/templates/next-app/src/styles/css/antd-themes/table/index.css +0 -3
- package/dist/templates/next-app/src/styles/css/antd-themes/table/pink.css +0 -43
- package/dist/templates/next-app/src/styles/css/index.css +0 -6
- package/dist/templates/next-app/src/styles/css/page.css +0 -20
- package/dist/templates/next-app/src/styles/css/scrollbar.css +0 -34
- package/dist/templates/next-app/src/styles/css/tailwind.css +0 -5
- package/dist/templates/next-app/src/styles/css/themes/_default.css +0 -30
- package/dist/templates/next-app/src/styles/css/themes/dark.css +0 -30
- package/dist/templates/next-app/src/styles/css/themes/index.css +0 -3
- package/dist/templates/next-app/src/styles/css/themes/pink.css +0 -30
- package/dist/templates/next-app/src/styles/css/zIndex.css +0 -9
- package/dist/templates/next-app/src/uikit/components/AdminLayout.tsx +0 -150
- package/dist/templates/next-app/src/uikit/components/BootstrapsProvider.tsx +0 -39
- package/dist/templates/next-app/src/uikit/components/ClientRootProvider.tsx +0 -64
- package/dist/templates/next-app/src/uikit/components/ClientSeo.tsx +0 -36
- package/dist/templates/next-app/src/uikit/components/ClinetRenderProvider.tsx +0 -42
- package/dist/templates/next-app/src/uikit/components/EditableCell.tsx +0 -118
- package/dist/templates/next-app/src/uikit/components/FeatureItem.tsx +0 -13
- package/dist/templates/next-app/src/uikit/components/IOCProvider.tsx +0 -34
- package/dist/templates/next-app/src/uikit/components/LocaleLink.tsx +0 -50
- package/dist/templates/next-app/src/uikit/components/With.tsx +0 -17
- package/dist/templates/next-app/src/uikit/components/localesImportButton/LocalesImportButton.tsx +0 -62
- package/dist/templates/next-app/src/uikit/components/localesImportButton/LocalesImportEvent.ts +0 -28
- package/dist/templates/next-app/src/uikit/components/localesImportButton/import.module.css +0 -6
- package/dist/templates/next-app/src/uikit/components-app/AdminButton.tsx +0 -29
- package/dist/templates/next-app/src/uikit/components-app/AppBridge.tsx +0 -17
- package/dist/templates/next-app/src/uikit/components-app/AppRoutePage.tsx +0 -105
- package/dist/templates/next-app/src/uikit/components-app/AuthButton.tsx +0 -20
- package/dist/templates/next-app/src/uikit/components-app/LanguageSwitcher.tsx +0 -75
- package/dist/templates/next-app/src/uikit/components-app/LogoutButton.tsx +0 -45
- package/dist/templates/next-app/src/uikit/components-app/ThemeSwitcher.tsx +0 -144
- package/dist/templates/next-app/src/uikit/components-pages/LanguageSwitcher.tsx +0 -98
- package/dist/templates/next-app/src/uikit/components-pages/PagesRoutePage.tsx +0 -93
- package/dist/templates/next-app/src/uikit/context/IOCContext.ts +0 -25
- package/dist/templates/next-app/src/uikit/hook/useI18nInterface.ts +0 -22
- package/dist/templates/next-app/src/uikit/hook/useIOC.ts +0 -37
- package/dist/templates/next-app/src/uikit/hook/useStrictEffect.ts +0 -32
- package/dist/templates/next-app/src/uikit/hook/useWarnTranslations.ts +0 -11
- package/dist/templates/next-app/src/uikit/utils/getHashParams.ts +0 -8
- package/dist/templates/next-app/src/uikit/utils/getHashVerifyEmailParams.ts +0 -42
- package/dist/templates/next-app/tailwind.config.ts +0 -8
- package/dist/templates/next-app/tsconfig.json +0 -39
- package/dist/templates/node-lib/__tests__/readJson.test.ts +0 -26
- package/dist/templates/node-lib/bin/test.js +0 -30
- package/dist/templates/node-lib/package.json +0 -66
- package/dist/templates/node-lib/rollup.config.js +0 -79
- package/dist/templates/node-lib/src/index.ts +0 -7
- package/dist/templates/node-lib/src/readJson.ts +0 -12
- package/dist/templates/node-lib/tsconfig.json +0 -23
- package/dist/templates/pack-app/README.md +0 -108
- package/dist/templates/pack-app/eslint.config.js +0 -97
- package/dist/templates/pack-app/fe-config.json +0 -35
- package/dist/templates/pack-app/package.json +0 -86
- package/dist/templates/pack-app/pnpm-workspace.yaml +0 -2
- package/dist/templates/pack-app/tsconfig.json +0 -9
- package/dist/templates/pack-app/tsconfig.test.json +0 -10
- package/dist/templates/pack-app/vite.config.ts +0 -14
- package/dist/templates/react-app/.env.template +0 -22
- package/dist/templates/react-app/.prettierignore +0 -17
- package/dist/templates/react-app/README.en.md +0 -274
- package/dist/templates/react-app/README.md +0 -273
- package/dist/templates/react-app/__tests__/__mocks__/BootstrapTest.ts +0 -16
- package/dist/templates/react-app/__tests__/__mocks__/MockAppConfig.ts +0 -48
- package/dist/templates/react-app/__tests__/__mocks__/MockDialogHandler.ts +0 -17
- package/dist/templates/react-app/__tests__/__mocks__/MockLogger.ts +0 -14
- package/dist/templates/react-app/__tests__/__mocks__/components/TestApp.tsx +0 -38
- package/dist/templates/react-app/__tests__/__mocks__/components/TestBootstrapsProvider.tsx +0 -53
- package/dist/templates/react-app/__tests__/__mocks__/components/TestRouter.tsx +0 -46
- package/dist/templates/react-app/__tests__/__mocks__/components/index.ts +0 -12
- package/dist/templates/react-app/__tests__/__mocks__/createMockGlobals.ts +0 -96
- package/dist/templates/react-app/__tests__/__mocks__/i18nextHttpBackend.ts +0 -110
- package/dist/templates/react-app/__tests__/__mocks__/testIOC/TestIOC.ts +0 -55
- package/dist/templates/react-app/__tests__/__mocks__/testIOC/TestIOCRegister.ts +0 -74
- package/dist/templates/react-app/__tests__/setup/index.ts +0 -1
- package/dist/templates/react-app/__tests__/setup/setupGlobal.ts +0 -64
- package/dist/templates/react-app/__tests__/src/App.structure.test.tsx +0 -115
- package/dist/templates/react-app/__tests__/src/base/cases/AppConfig.test.ts +0 -288
- package/dist/templates/react-app/__tests__/src/base/cases/DialogHandler.test.ts +0 -226
- package/dist/templates/react-app/__tests__/src/base/cases/I18nKeyErrorPlugin.test.ts +0 -178
- package/dist/templates/react-app/__tests__/src/base/cases/InversifyContainer.test.ts +0 -181
- package/dist/templates/react-app/__tests__/src/base/cases/PublicAssetsPath.test.ts +0 -61
- package/dist/templates/react-app/__tests__/src/base/cases/RequestLogger.test.ts +0 -177
- package/dist/templates/react-app/__tests__/src/base/cases/RequestStatusCatcher.test.ts +0 -191
- package/dist/templates/react-app/__tests__/src/base/cases/RouterLoader.test.ts +0 -245
- package/dist/templates/react-app/__tests__/src/base/services/I18nService.test.ts +0 -240
- package/dist/templates/react-app/__tests__/src/core/IOC.test.ts +0 -242
- package/dist/templates/react-app/__tests__/src/core/bootstraps/BootstrapClient.test.ts +0 -135
- package/dist/templates/react-app/__tests__/src/core/bootstraps/BootstrapsApp.test.ts +0 -74
- package/dist/templates/react-app/__tests__/src/main.test.tsx +0 -46
- package/dist/templates/react-app/__tests__/src/uikit/components/BaseHeader.test.tsx +0 -97
- package/dist/templates/react-app/__tests__/src/uikit/components/chatMessage/ChatRoot.test.tsx +0 -274
- package/dist/templates/react-app/config/IOCIdentifier.ts +0 -91
- package/dist/templates/react-app/config/Identifier/common/common.error.ts +0 -34
- package/dist/templates/react-app/config/Identifier/common/common.ts +0 -62
- package/dist/templates/react-app/config/Identifier/common/index.ts +0 -2
- package/dist/templates/react-app/config/Identifier/components/component.chatMessage.ts +0 -56
- package/dist/templates/react-app/config/Identifier/components/component.messageBaseList.ts +0 -103
- package/dist/templates/react-app/config/Identifier/index.ts +0 -2
- package/dist/templates/react-app/config/Identifier/pages/index.ts +0 -9
- package/dist/templates/react-app/config/Identifier/pages/page.about.ts +0 -189
- package/dist/templates/react-app/config/Identifier/pages/page.executor.ts +0 -275
- package/dist/templates/react-app/config/Identifier/pages/page.home.ts +0 -71
- package/dist/templates/react-app/config/Identifier/pages/page.identifiter.ts +0 -102
- package/dist/templates/react-app/config/Identifier/pages/page.jsonStorage.ts +0 -77
- package/dist/templates/react-app/config/Identifier/pages/page.login.ts +0 -162
- package/dist/templates/react-app/config/Identifier/pages/page.message.ts +0 -20
- package/dist/templates/react-app/config/Identifier/pages/page.register.ts +0 -159
- package/dist/templates/react-app/config/Identifier/pages/page.request.ts +0 -169
- package/dist/templates/react-app/config/app.router.ts +0 -338
- package/dist/templates/react-app/config/common.ts +0 -85
- package/dist/templates/react-app/config/feapi.mock.json +0 -34
- package/dist/templates/react-app/config/i18n/PageI18nInterface.ts +0 -53
- package/dist/templates/react-app/config/i18n/aboutI18n.ts +0 -42
- package/dist/templates/react-app/config/i18n/chatMessageI18n.ts +0 -17
- package/dist/templates/react-app/config/i18n/executorI18n.ts +0 -51
- package/dist/templates/react-app/config/i18n/homeI18n.ts +0 -24
- package/dist/templates/react-app/config/i18n/i18nConfig.ts +0 -30
- package/dist/templates/react-app/config/i18n/identifiter18n.ts +0 -30
- package/dist/templates/react-app/config/i18n/jsonStorage18n.ts +0 -27
- package/dist/templates/react-app/config/i18n/login18n.ts +0 -42
- package/dist/templates/react-app/config/i18n/messageBaseListI18n.ts +0 -22
- package/dist/templates/react-app/config/i18n/messageI18n.ts +0 -14
- package/dist/templates/react-app/config/i18n/notFoundI18n.ts +0 -34
- package/dist/templates/react-app/config/i18n/register18n.ts +0 -40
- package/dist/templates/react-app/config/i18n/request18n.ts +0 -41
- package/dist/templates/react-app/config/theme.ts +0 -21
- package/dist/templates/react-app/docs/en/bootstrap.md +0 -1891
- package/dist/templates/react-app/docs/en/components/chat-message-component.md +0 -320
- package/dist/templates/react-app/docs/en/components/chat-message-refactor.md +0 -283
- package/dist/templates/react-app/docs/en/components/message-base-list-component.md +0 -171
- package/dist/templates/react-app/docs/en/development-guide.md +0 -1199
- package/dist/templates/react-app/docs/en/env.md +0 -1336
- package/dist/templates/react-app/docs/en/global.md +0 -509
- package/dist/templates/react-app/docs/en/i18n.md +0 -979
- package/dist/templates/react-app/docs/en/index.md +0 -802
- package/dist/templates/react-app/docs/en/ioc.md +0 -1365
- package/dist/templates/react-app/docs/en/playwright/e2e-tests.md +0 -321
- package/dist/templates/react-app/docs/en/playwright/index.md +0 -19
- package/dist/templates/react-app/docs/en/playwright/installation-summary.md +0 -332
- package/dist/templates/react-app/docs/en/playwright/overview.md +0 -222
- package/dist/templates/react-app/docs/en/playwright/quickstart.md +0 -325
- package/dist/templates/react-app/docs/en/playwright/reorganization-notes.md +0 -340
- package/dist/templates/react-app/docs/en/playwright/setup-complete.md +0 -290
- package/dist/templates/react-app/docs/en/playwright/testing-guide.md +0 -565
- package/dist/templates/react-app/docs/en/request.md +0 -423
- package/dist/templates/react-app/docs/en/router.md +0 -404
- package/dist/templates/react-app/docs/en/store.md +0 -1331
- package/dist/templates/react-app/docs/en/test-guide.md +0 -976
- package/dist/templates/react-app/docs/en/theme.md +0 -424
- package/dist/templates/react-app/docs/en/typescript-guide.md +0 -473
- package/dist/templates/react-app/docs/en/why-no-globals.md +0 -797
- package/dist/templates/react-app/docs/zh/bootstrap.md +0 -1891
- package/dist/templates/react-app/docs/zh/components/chat-message-component.md +0 -320
- package/dist/templates/react-app/docs/zh/components/chat-message-refactor.md +0 -283
- package/dist/templates/react-app/docs/zh/components/message-base-list-component.md +0 -171
- package/dist/templates/react-app/docs/zh/development-guide.md +0 -1199
- package/dist/templates/react-app/docs/zh/env.md +0 -1336
- package/dist/templates/react-app/docs/zh/global.md +0 -511
- package/dist/templates/react-app/docs/zh/i18n.md +0 -979
- package/dist/templates/react-app/docs/zh/index.md +0 -786
- package/dist/templates/react-app/docs/zh/ioc.md +0 -1364
- package/dist/templates/react-app/docs/zh/playwright/e2e-tests.md +0 -321
- package/dist/templates/react-app/docs/zh/playwright/index.md +0 -19
- package/dist/templates/react-app/docs/zh/playwright/installation-summary.md +0 -332
- package/dist/templates/react-app/docs/zh/playwright/overview.md +0 -222
- package/dist/templates/react-app/docs/zh/playwright/quickstart.md +0 -325
- package/dist/templates/react-app/docs/zh/playwright/reorganization-notes.md +0 -340
- package/dist/templates/react-app/docs/zh/playwright/setup-complete.md +0 -290
- package/dist/templates/react-app/docs/zh/playwright/testing-guide.md +0 -565
- package/dist/templates/react-app/docs/zh/request.md +0 -427
- package/dist/templates/react-app/docs/zh/router.md +0 -408
- package/dist/templates/react-app/docs/zh/store.md +0 -1329
- package/dist/templates/react-app/docs/zh/test-guide.md +0 -976
- package/dist/templates/react-app/docs/zh/theme.md +0 -424
- package/dist/templates/react-app/docs/zh/typescript-guide.md +0 -473
- package/dist/templates/react-app/docs/zh/why-no-globals.md +0 -797
- package/dist/templates/react-app/e2e/App.spec.ts +0 -319
- package/dist/templates/react-app/e2e/fixtures/base.fixture.ts +0 -40
- package/dist/templates/react-app/e2e/main.spec.ts +0 -20
- package/dist/templates/react-app/e2e/utils/test-helpers.ts +0 -19
- package/dist/templates/react-app/eslint.config.mjs +0 -325
- package/dist/templates/react-app/index.html +0 -13
- package/dist/templates/react-app/makes/generateTs2LocalesOptions.ts +0 -26
- package/dist/templates/react-app/package.json +0 -125
- package/dist/templates/react-app/playwright.config.ts +0 -79
- package/dist/templates/react-app/postcss.config.js +0 -5
- package/dist/templates/react-app/public/locales/en/common.json +0 -235
- package/dist/templates/react-app/public/locales/zh/common.json +0 -235
- package/dist/templates/react-app/public/logo.svg +0 -1
- package/dist/templates/react-app/public/router-root/logo.svg +0 -1
- package/dist/templates/react-app/src/App.tsx +0 -35
- package/dist/templates/react-app/src/assets/react.svg +0 -1
- package/dist/templates/react-app/src/base/apis/AiApi.ts +0 -68
- package/dist/templates/react-app/src/base/apis/feApi/FeApi.ts +0 -29
- package/dist/templates/react-app/src/base/apis/feApi/FeApiAdapter.ts +0 -14
- package/dist/templates/react-app/src/base/apis/feApi/FeApiBootstarp.ts +0 -86
- package/dist/templates/react-app/src/base/apis/feApi/FeApiType.ts +0 -21
- package/dist/templates/react-app/src/base/apis/userApi/UserApi.ts +0 -142
- package/dist/templates/react-app/src/base/apis/userApi/UserApiAdapter.ts +0 -14
- package/dist/templates/react-app/src/base/apis/userApi/UserApiBootstarp.ts +0 -86
- package/dist/templates/react-app/src/base/apis/userApi/UserApiType.ts +0 -70
- package/dist/templates/react-app/src/base/cases/AppConfig.ts +0 -123
- package/dist/templates/react-app/src/base/cases/DialogHandler.ts +0 -115
- package/dist/templates/react-app/src/base/cases/I18nKeyErrorPlugin.ts +0 -64
- package/dist/templates/react-app/src/base/cases/InversifyContainer.ts +0 -45
- package/dist/templates/react-app/src/base/cases/PublicAssetsPath.ts +0 -23
- package/dist/templates/react-app/src/base/cases/RequestLanguages.ts +0 -55
- package/dist/templates/react-app/src/base/cases/RequestLogger.ts +0 -80
- package/dist/templates/react-app/src/base/cases/RequestStatusCatcher.ts +0 -40
- package/dist/templates/react-app/src/base/cases/ResourceState.ts +0 -23
- package/dist/templates/react-app/src/base/cases/RouterLoader.ts +0 -173
- package/dist/templates/react-app/src/base/cases/TranslateI18nInterface.ts +0 -26
- package/dist/templates/react-app/src/base/port/ExecutorPageBridgeInterface.ts +0 -23
- package/dist/templates/react-app/src/base/port/I18nServiceInterface.ts +0 -10
- package/dist/templates/react-app/src/base/port/IOCInterface.ts +0 -38
- package/dist/templates/react-app/src/base/port/JSONStoragePageBridgeInterface.ts +0 -21
- package/dist/templates/react-app/src/base/port/RequestPageBridgeInterface.ts +0 -23
- package/dist/templates/react-app/src/base/port/RequestStatusInterface.ts +0 -5
- package/dist/templates/react-app/src/base/port/RouteServiceInterface.ts +0 -29
- package/dist/templates/react-app/src/base/port/UserServiceInterface.ts +0 -20
- package/dist/templates/react-app/src/base/services/BaseLayoutService.ts +0 -61
- package/dist/templates/react-app/src/base/services/I18nService.ts +0 -146
- package/dist/templates/react-app/src/base/services/IdentifierService.ts +0 -162
- package/dist/templates/react-app/src/base/services/RouteService.ts +0 -115
- package/dist/templates/react-app/src/base/services/UserBootstrap.ts +0 -45
- package/dist/templates/react-app/src/base/services/UserService.ts +0 -88
- package/dist/templates/react-app/src/base/types/Page.ts +0 -47
- package/dist/templates/react-app/src/base/types/deprecated-antd.d.ts +0 -60
- package/dist/templates/react-app/src/base/types/global.d.ts +0 -8
- package/dist/templates/react-app/src/core/IOC.ts +0 -28
- package/dist/templates/react-app/src/core/bootstraps/BootstrapClient.ts +0 -108
- package/dist/templates/react-app/src/core/bootstraps/BootstrapsRegistry.ts +0 -54
- package/dist/templates/react-app/src/core/bootstraps/IocIdentifierTest.ts +0 -26
- package/dist/templates/react-app/src/core/bootstraps/PrintBootstrap.ts +0 -14
- package/dist/templates/react-app/src/core/bootstraps/SaveAppInfo.ts +0 -28
- package/dist/templates/react-app/src/core/clientIoc/ClientIOC.ts +0 -47
- package/dist/templates/react-app/src/core/clientIoc/ClientIOCRegister.ts +0 -142
- package/dist/templates/react-app/src/core/globals.ts +0 -47
- package/dist/templates/react-app/src/main.tsx +0 -19
- package/dist/templates/react-app/src/pages/404.tsx +0 -19
- package/dist/templates/react-app/src/pages/500.tsx +0 -18
- package/dist/templates/react-app/src/pages/NoRouteFound.tsx +0 -5
- package/dist/templates/react-app/src/pages/auth/Layout.tsx +0 -27
- package/dist/templates/react-app/src/pages/auth/LoginPage.tsx +0 -166
- package/dist/templates/react-app/src/pages/auth/RegisterPage.tsx +0 -226
- package/dist/templates/react-app/src/pages/base/AboutPage.tsx +0 -123
- package/dist/templates/react-app/src/pages/base/ExecutorPage.tsx +0 -467
- package/dist/templates/react-app/src/pages/base/HomePage.tsx +0 -81
- package/dist/templates/react-app/src/pages/base/IdentifierPage.tsx +0 -117
- package/dist/templates/react-app/src/pages/base/JSONStoragePage.tsx +0 -132
- package/dist/templates/react-app/src/pages/base/Layout.tsx +0 -20
- package/dist/templates/react-app/src/pages/base/MessagePage.tsx +0 -71
- package/dist/templates/react-app/src/pages/base/RedirectPathname.tsx +0 -18
- package/dist/templates/react-app/src/pages/base/RequestPage.tsx +0 -193
- package/dist/templates/react-app/src/styles/css/antd-themes/_common/_default.css +0 -280
- package/dist/templates/react-app/src/styles/css/antd-themes/_common/dark.css +0 -233
- package/dist/templates/react-app/src/styles/css/antd-themes/_common/index.css +0 -3
- package/dist/templates/react-app/src/styles/css/antd-themes/_common/pink.css +0 -246
- package/dist/templates/react-app/src/styles/css/antd-themes/index.css +0 -4
- package/dist/templates/react-app/src/styles/css/antd-themes/menu/_default.css +0 -108
- package/dist/templates/react-app/src/styles/css/antd-themes/menu/dark.css +0 -67
- package/dist/templates/react-app/src/styles/css/antd-themes/menu/index.css +0 -3
- package/dist/templates/react-app/src/styles/css/antd-themes/menu/pink.css +0 -67
- package/dist/templates/react-app/src/styles/css/antd-themes/no-context.css +0 -34
- package/dist/templates/react-app/src/styles/css/antd-themes/pagination/_default.css +0 -34
- package/dist/templates/react-app/src/styles/css/antd-themes/pagination/dark.css +0 -31
- package/dist/templates/react-app/src/styles/css/antd-themes/pagination/index.css +0 -3
- package/dist/templates/react-app/src/styles/css/antd-themes/pagination/pink.css +0 -36
- package/dist/templates/react-app/src/styles/css/antd-themes/table/_default.css +0 -44
- package/dist/templates/react-app/src/styles/css/antd-themes/table/dark.css +0 -43
- package/dist/templates/react-app/src/styles/css/antd-themes/table/index.css +0 -3
- package/dist/templates/react-app/src/styles/css/antd-themes/table/pink.css +0 -43
- package/dist/templates/react-app/src/styles/css/index.css +0 -6
- package/dist/templates/react-app/src/styles/css/page.css +0 -20
- package/dist/templates/react-app/src/styles/css/scrollbar.css +0 -34
- package/dist/templates/react-app/src/styles/css/tailwind.css +0 -5
- package/dist/templates/react-app/src/styles/css/themes/_default.css +0 -30
- package/dist/templates/react-app/src/styles/css/themes/dark.css +0 -30
- package/dist/templates/react-app/src/styles/css/themes/index.css +0 -3
- package/dist/templates/react-app/src/styles/css/themes/pink.css +0 -30
- package/dist/templates/react-app/src/styles/css/zIndex.css +0 -9
- package/dist/templates/react-app/src/uikit/bridges/ExecutorPageBridge.ts +0 -72
- package/dist/templates/react-app/src/uikit/bridges/JSONStoragePageBridge.ts +0 -41
- package/dist/templates/react-app/src/uikit/bridges/NavigateBridge.ts +0 -22
- package/dist/templates/react-app/src/uikit/bridges/RequestPageBridge.ts +0 -136
- package/dist/templates/react-app/src/uikit/components/AppRouterProvider.tsx +0 -35
- package/dist/templates/react-app/src/uikit/components/BaseHeader.tsx +0 -51
- package/dist/templates/react-app/src/uikit/components/BaseLayoutProvider.tsx +0 -44
- package/dist/templates/react-app/src/uikit/components/BaseRouteProvider.tsx +0 -21
- package/dist/templates/react-app/src/uikit/components/BaseRouteSeo.tsx +0 -18
- package/dist/templates/react-app/src/uikit/components/BootstrapsProvider.tsx +0 -11
- package/dist/templates/react-app/src/uikit/components/ClientSeo.tsx +0 -62
- package/dist/templates/react-app/src/uikit/components/ComboProvider.tsx +0 -38
- package/dist/templates/react-app/src/uikit/components/LanguageSwitcher.tsx +0 -78
- package/dist/templates/react-app/src/uikit/components/Loading.tsx +0 -49
- package/dist/templates/react-app/src/uikit/components/LocaleLink.tsx +0 -43
- package/dist/templates/react-app/src/uikit/components/LogoutButton.tsx +0 -58
- package/dist/templates/react-app/src/uikit/components/MessageBaseList.tsx +0 -258
- package/dist/templates/react-app/src/uikit/components/RouterRenderComponent.tsx +0 -19
- package/dist/templates/react-app/src/uikit/components/ThemeSwitcher.tsx +0 -137
- package/dist/templates/react-app/src/uikit/components/With.tsx +0 -17
- package/dist/templates/react-app/src/uikit/components/chatMessage/ChatMessageBridge.ts +0 -205
- package/dist/templates/react-app/src/uikit/components/chatMessage/ChatRoot.tsx +0 -21
- package/dist/templates/react-app/src/uikit/components/chatMessage/FocusBar.tsx +0 -108
- package/dist/templates/react-app/src/uikit/components/chatMessage/MessageApi.ts +0 -282
- package/dist/templates/react-app/src/uikit/components/chatMessage/MessageItem.tsx +0 -102
- package/dist/templates/react-app/src/uikit/components/chatMessage/MessagesList.tsx +0 -86
- package/dist/templates/react-app/src/uikit/contexts/BaseRouteContext.ts +0 -42
- package/dist/templates/react-app/src/uikit/contexts/IOCContext.ts +0 -13
- package/dist/templates/react-app/src/uikit/hooks/useAppTranslation.ts +0 -26
- package/dist/templates/react-app/src/uikit/hooks/useI18nInterface.ts +0 -25
- package/dist/templates/react-app/src/uikit/hooks/useIOC.ts +0 -35
- package/dist/templates/react-app/src/uikit/hooks/useNavigateBridge.ts +0 -21
- package/dist/templates/react-app/src/uikit/hooks/useRouterI18nGuard.ts +0 -25
- package/dist/templates/react-app/src/uikit/hooks/useStrictEffect.ts +0 -31
- package/dist/templates/react-app/src/vite-env.d.ts +0 -1
- package/dist/templates/react-app/tailwind.config.js +0 -4
- package/dist/templates/react-app/tsconfig.app.json +0 -36
- package/dist/templates/react-app/tsconfig.e2e.json +0 -24
- package/dist/templates/react-app/tsconfig.json +0 -22
- package/dist/templates/react-app/tsconfig.node.json +0 -27
- package/dist/templates/react-app/tsconfig.test.json +0 -18
- package/dist/templates/react-app/vite.config.ts +0 -144
|
@@ -1,976 +0,0 @@
|
|
|
1
|
-
# 测试指南
|
|
2
|
-
|
|
3
|
-
> 本文档简要介绍 **fe-base** 项目在 monorepo 场景下的测试策略与最佳实践,使用 [Vitest](https://vitest.dev/) 作为统一测试框架。
|
|
4
|
-
|
|
5
|
-
---
|
|
6
|
-
|
|
7
|
-
## 为何选择 Vitest
|
|
8
|
-
|
|
9
|
-
1. **与 Vite 生态完美集成**:共享 Vite 配置,TypeScript & ESM 开箱即用。
|
|
10
|
-
2. **现代特性**:并行执行、HMR、内置覆盖率统计。
|
|
11
|
-
3. **Jest 兼容 API**:`describe / it / expect` 等 API 无学习成本。
|
|
12
|
-
4. **Monorepo 友好**:可按 workspace 过滤执行,易于在 CI 中并行跑包级测试。
|
|
13
|
-
|
|
14
|
-
---
|
|
15
|
-
|
|
16
|
-
## 测试类型
|
|
17
|
-
|
|
18
|
-
- **单元测试 (Unit)**:验证函数、类或组件的最小行为。
|
|
19
|
-
- **集成测试 (Integration)**:验证多个模块的协作与边界。
|
|
20
|
-
- **端到端 (E2E,视需要引入 Playwright/Cypress)**:验证完整用户流程。
|
|
21
|
-
|
|
22
|
-
> ⚡️ 在绝大多数情况下,优先编写单元测试;仅在跨模块交互复杂时再补充集成测试。
|
|
23
|
-
|
|
24
|
-
---
|
|
25
|
-
|
|
26
|
-
## 测试文件组织规范
|
|
27
|
-
|
|
28
|
-
### 文件命名与位置
|
|
29
|
-
|
|
30
|
-
```
|
|
31
|
-
packages/
|
|
32
|
-
├── package-name/
|
|
33
|
-
│ ├── __tests__/ # 测试文件目录
|
|
34
|
-
│ │ ├── Class.test.ts # 类测试
|
|
35
|
-
│ │ ├── utils/ # 工具函数测试
|
|
36
|
-
│ │ │ └── helper.test.ts
|
|
37
|
-
│ │ └── integration/ # 集成测试
|
|
38
|
-
│ ├── __mocks__/ # Mock 文件目录
|
|
39
|
-
│ │ └── index.ts
|
|
40
|
-
│ └── src/ # 源代码目录
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
### 测试文件结构
|
|
44
|
-
|
|
45
|
-
```typescript
|
|
46
|
-
// 标准测试文件头部注释
|
|
47
|
-
/**
|
|
48
|
-
* ClassName test-suite
|
|
49
|
-
*
|
|
50
|
-
* Coverage:
|
|
51
|
-
* 1. constructor – 构造函数测试
|
|
52
|
-
* 2. methodName – 方法功能测试
|
|
53
|
-
* 3. edge cases – 边界情况测试
|
|
54
|
-
* 4. error handling – 错误处理测试
|
|
55
|
-
*/
|
|
56
|
-
|
|
57
|
-
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
|
58
|
-
import { ClassName } from '../src/ClassName';
|
|
59
|
-
|
|
60
|
-
describe('ClassName', () => {
|
|
61
|
-
// 测试数据和Mock对象
|
|
62
|
-
let instance: ClassName;
|
|
63
|
-
let mockDependency: MockType;
|
|
64
|
-
|
|
65
|
-
// 设置和清理
|
|
66
|
-
beforeEach(() => {
|
|
67
|
-
// 初始化测试环境
|
|
68
|
-
mockDependency = createMockDependency();
|
|
69
|
-
instance = new ClassName(mockDependency);
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
afterEach(() => {
|
|
73
|
-
// 清理测试环境
|
|
74
|
-
vi.clearAllMocks();
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
// 构造函数测试
|
|
78
|
-
describe('constructor', () => {
|
|
79
|
-
it('should create instance with valid parameters', () => {
|
|
80
|
-
expect(instance).toBeInstanceOf(ClassName);
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
it('should throw error with invalid parameters', () => {
|
|
84
|
-
expect(() => new ClassName(null)).toThrow();
|
|
85
|
-
});
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
// 方法测试分组
|
|
89
|
-
describe('methodName', () => {
|
|
90
|
-
it('should handle normal case', () => {
|
|
91
|
-
// 测试正常情况
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
it('should handle edge cases', () => {
|
|
95
|
-
// 测试边界情况
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
it('should handle error cases', () => {
|
|
99
|
-
// 测试错误情况
|
|
100
|
-
});
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
// 集成测试
|
|
104
|
-
describe('integration tests', () => {
|
|
105
|
-
it('should work with dependent modules', () => {
|
|
106
|
-
// 测试模块间协作
|
|
107
|
-
});
|
|
108
|
-
});
|
|
109
|
-
});
|
|
110
|
-
```
|
|
111
|
-
|
|
112
|
-
---
|
|
113
|
-
|
|
114
|
-
## Vitest 全局配置示例
|
|
115
|
-
|
|
116
|
-
```typescript
|
|
117
|
-
// vitest.config.ts
|
|
118
|
-
import { defineConfig } from 'vitest/config';
|
|
119
|
-
import { resolve } from 'path';
|
|
120
|
-
|
|
121
|
-
export default defineConfig({
|
|
122
|
-
test: {
|
|
123
|
-
globals: true,
|
|
124
|
-
environment: 'jsdom',
|
|
125
|
-
setupFiles: ['./test/setup.ts'],
|
|
126
|
-
alias: {
|
|
127
|
-
// 在测试环境下自动 Mock 某些包,指向 __mocks__ 目录
|
|
128
|
-
'@qlover/fe-corekit': resolve(__dirname, 'packages/fe-corekit/__mocks__'),
|
|
129
|
-
'@qlover/logger': resolve(__dirname, 'packages/logger/__mocks__')
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
});
|
|
133
|
-
```
|
|
134
|
-
|
|
135
|
-
### 包级脚本
|
|
136
|
-
|
|
137
|
-
```jsonc
|
|
138
|
-
// packages/xxx/package.json (示例)
|
|
139
|
-
{
|
|
140
|
-
"scripts": {
|
|
141
|
-
"test": "vitest run",
|
|
142
|
-
"test:watch": "vitest",
|
|
143
|
-
"test:coverage": "vitest run --coverage"
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
```
|
|
147
|
-
|
|
148
|
-
---
|
|
149
|
-
|
|
150
|
-
## 测试策略
|
|
151
|
-
|
|
152
|
-
### 测试分组
|
|
153
|
-
|
|
154
|
-
整个文件为一个测试文件, 测试内容以分组为单位,比如describe为一组测试,一般一个测试文件根只有一个describe
|
|
155
|
-
|
|
156
|
-
内容按照从"小到大测试", 比如: 源文件中是 class 那么从构造器传递参数,构造器,每个成员方法为分组进行测试
|
|
157
|
-
|
|
158
|
-
- 小到每个方法传递各种参数类型的覆盖,大到调用方法时影响的整体流程
|
|
159
|
-
- 以及整体的流程测试,边界测试
|
|
160
|
-
|
|
161
|
-
源文件(TestClass.ts):
|
|
162
|
-
|
|
163
|
-
```ts
|
|
164
|
-
type TestClassOptions = {
|
|
165
|
-
name: string;
|
|
166
|
-
};
|
|
167
|
-
|
|
168
|
-
class TestClass {
|
|
169
|
-
constructor(options: TestClassOptions) {}
|
|
170
|
-
|
|
171
|
-
getName(): string {
|
|
172
|
-
return this.options.name;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
setName(name: string): void {
|
|
176
|
-
this.options.name = name;
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
```
|
|
180
|
-
|
|
181
|
-
测试文件(TestClass.test.ts):
|
|
182
|
-
|
|
183
|
-
```ts
|
|
184
|
-
describe('TestClass', () => {
|
|
185
|
-
describe('TestClass.constructor', () => {
|
|
186
|
-
// ...
|
|
187
|
-
});
|
|
188
|
-
describe('TestClass.getName', () => {
|
|
189
|
-
// ...
|
|
190
|
-
});
|
|
191
|
-
|
|
192
|
-
describe('整体流程或边界测试', () => {
|
|
193
|
-
it('应该在修改name后,getName保持一致', () => {
|
|
194
|
-
const testClass = new TestClass({ name: 'test' });
|
|
195
|
-
testClass.setName('test2');
|
|
196
|
-
expect(testClass.getName()).toBe('test2');
|
|
197
|
-
});
|
|
198
|
-
});
|
|
199
|
-
});
|
|
200
|
-
```
|
|
201
|
-
|
|
202
|
-
### 测试用例命名规范
|
|
203
|
-
|
|
204
|
-
```typescript
|
|
205
|
-
describe('ClassName', () => {
|
|
206
|
-
describe('methodName', () => {
|
|
207
|
-
// 正面测试用例
|
|
208
|
-
it('should return expected result when given valid input', () => {});
|
|
209
|
-
it('should handle multiple parameters correctly', () => {});
|
|
210
|
-
|
|
211
|
-
// 边界测试用例
|
|
212
|
-
it('should handle empty input', () => {});
|
|
213
|
-
it('should handle null/undefined input', () => {});
|
|
214
|
-
it('should handle maximum/minimum values', () => {});
|
|
215
|
-
|
|
216
|
-
// 错误测试用例
|
|
217
|
-
it('should throw error when given invalid input', () => {});
|
|
218
|
-
it('should handle network failure gracefully', () => {});
|
|
219
|
-
|
|
220
|
-
// 行为测试用例
|
|
221
|
-
it('should call dependency method with correct parameters', () => {});
|
|
222
|
-
it('should not call dependency when condition is false', () => {});
|
|
223
|
-
});
|
|
224
|
-
});
|
|
225
|
-
```
|
|
226
|
-
|
|
227
|
-
### 函数的测试
|
|
228
|
-
|
|
229
|
-
函数测试应该覆盖以下几个方面:
|
|
230
|
-
|
|
231
|
-
1. **参数组合测试**
|
|
232
|
-
|
|
233
|
-
```typescript
|
|
234
|
-
interface TestParams {
|
|
235
|
-
key1?: string;
|
|
236
|
-
key2?: number;
|
|
237
|
-
key3?: boolean;
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
function testFunction({ key1, key2, key3 }: TestParams): string {
|
|
241
|
-
// 实现...
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
describe('testFunction', () => {
|
|
245
|
-
describe('参数组合测试', () => {
|
|
246
|
-
it('应该处理所有参数都存在的情况', () => {
|
|
247
|
-
expect(testFunction({ key1: 'test', key2: 1, key3: true })).toBe(
|
|
248
|
-
'expected result'
|
|
249
|
-
);
|
|
250
|
-
});
|
|
251
|
-
|
|
252
|
-
it('应该处理只有 key1, key2 的情况', () => {
|
|
253
|
-
expect(testFunction({ key1: 'test', key2: 1 })).toBe('expected result');
|
|
254
|
-
});
|
|
255
|
-
|
|
256
|
-
it('应该处理只有 key1, key3 的情况', () => {
|
|
257
|
-
expect(testFunction({ key1: 'test', key3: true })).toBe(
|
|
258
|
-
'expected result'
|
|
259
|
-
);
|
|
260
|
-
});
|
|
261
|
-
|
|
262
|
-
it('应该处理只有 key2, key3 的情况', () => {
|
|
263
|
-
expect(testFunction({ key2: 1, key3: true })).toBe('expected result');
|
|
264
|
-
});
|
|
265
|
-
|
|
266
|
-
it('应该处理只有 key1 的情况', () => {
|
|
267
|
-
expect(testFunction({ key1: 'test' })).toBe('expected result');
|
|
268
|
-
});
|
|
269
|
-
|
|
270
|
-
it('应该处理只有 key2 的情况', () => {
|
|
271
|
-
expect(testFunction({ key2: 1 })).toBe('expected result');
|
|
272
|
-
});
|
|
273
|
-
|
|
274
|
-
it('应该处理只有 key3 的情况', () => {
|
|
275
|
-
expect(testFunction({ key3: true })).toBe('expected result');
|
|
276
|
-
});
|
|
277
|
-
|
|
278
|
-
it('应该处理空对象的情况', () => {
|
|
279
|
-
expect(testFunction({})).toBe('expected result');
|
|
280
|
-
});
|
|
281
|
-
});
|
|
282
|
-
|
|
283
|
-
describe('边界值测试', () => {
|
|
284
|
-
it('应该处理极限值情况', () => {
|
|
285
|
-
expect(
|
|
286
|
-
testFunction({
|
|
287
|
-
key1: '', // 空字符串
|
|
288
|
-
key2: Number.MAX_SAFE_INTEGER, // 最大安全整数
|
|
289
|
-
key3: false
|
|
290
|
-
})
|
|
291
|
-
).toBe('expected result');
|
|
292
|
-
|
|
293
|
-
expect(
|
|
294
|
-
testFunction({
|
|
295
|
-
key1: 'a'.repeat(1000), // 超长字符串
|
|
296
|
-
key2: Number.MIN_SAFE_INTEGER, // 最小安全整数
|
|
297
|
-
key3: true
|
|
298
|
-
})
|
|
299
|
-
).toBe('expected result');
|
|
300
|
-
});
|
|
301
|
-
|
|
302
|
-
it('应该处理特殊值情况', () => {
|
|
303
|
-
expect(
|
|
304
|
-
testFunction({
|
|
305
|
-
key1: ' ', // 全空格字符串
|
|
306
|
-
key2: 0, // 零值
|
|
307
|
-
key3: false
|
|
308
|
-
})
|
|
309
|
-
).toBe('expected result');
|
|
310
|
-
|
|
311
|
-
expect(
|
|
312
|
-
testFunction({
|
|
313
|
-
key1: null as any, // null 值
|
|
314
|
-
key2: NaN, // NaN 值
|
|
315
|
-
key3: undefined as any // undefined 值
|
|
316
|
-
})
|
|
317
|
-
).toBe('expected result');
|
|
318
|
-
});
|
|
319
|
-
|
|
320
|
-
it('应该处理无效值情况', () => {
|
|
321
|
-
expect(() =>
|
|
322
|
-
testFunction({
|
|
323
|
-
key1: Symbol() as any, // 无效类型
|
|
324
|
-
key2: {} as any, // 无效类型
|
|
325
|
-
key3: 42 as any // 无效类型
|
|
326
|
-
})
|
|
327
|
-
).toThrow();
|
|
328
|
-
});
|
|
329
|
-
});
|
|
330
|
-
});
|
|
331
|
-
```
|
|
332
|
-
|
|
333
|
-
这个测试套件展示了:
|
|
334
|
-
|
|
335
|
-
1. **完整的参数组合覆盖**:
|
|
336
|
-
- 测试所有可能的参数组合(2^n 种组合,n 为参数个数)
|
|
337
|
-
- 包括全部参数存在、部分参数存在和空对象的情况
|
|
338
|
-
|
|
339
|
-
2. **边界值测试**:
|
|
340
|
-
- 测试参数的极限值(最大值、最小值)
|
|
341
|
-
- 测试特殊值(空字符串、null、undefined、NaN)
|
|
342
|
-
- 测试无效值(类型错误)
|
|
343
|
-
|
|
344
|
-
3. **测试用例组织**:
|
|
345
|
-
- 使用嵌套 describe 清晰组织测试场景
|
|
346
|
-
- 每个测试用例都有明确的描述
|
|
347
|
-
- 相关的测试用例组织在一起
|
|
348
|
-
|
|
349
|
-
### 测试数据管理
|
|
350
|
-
|
|
351
|
-
```typescript
|
|
352
|
-
describe('DataProcessor', () => {
|
|
353
|
-
// 测试数据常量
|
|
354
|
-
const VALID_DATA = {
|
|
355
|
-
id: 1,
|
|
356
|
-
name: 'test',
|
|
357
|
-
items: ['a', 'b', 'c']
|
|
358
|
-
};
|
|
359
|
-
|
|
360
|
-
const INVALID_DATA = {
|
|
361
|
-
id: null,
|
|
362
|
-
name: '',
|
|
363
|
-
items: []
|
|
364
|
-
};
|
|
365
|
-
|
|
366
|
-
// 测试数据工厂函数
|
|
367
|
-
const createTestUser = (overrides = {}) => ({
|
|
368
|
-
id: 1,
|
|
369
|
-
name: 'Test User',
|
|
370
|
-
email: 'test@example.com',
|
|
371
|
-
...overrides
|
|
372
|
-
});
|
|
373
|
-
|
|
374
|
-
// 复杂数据结构
|
|
375
|
-
const createComplexTestData = () => ({
|
|
376
|
-
metadata: {
|
|
377
|
-
version: '1.0.0',
|
|
378
|
-
created: Date.now(),
|
|
379
|
-
tags: ['test', 'data']
|
|
380
|
-
},
|
|
381
|
-
users: [
|
|
382
|
-
createTestUser({ id: 1 }),
|
|
383
|
-
createTestUser({ id: 2, name: 'Another User' })
|
|
384
|
-
]
|
|
385
|
-
});
|
|
386
|
-
});
|
|
387
|
-
```
|
|
388
|
-
|
|
389
|
-
---
|
|
390
|
-
|
|
391
|
-
## Mock 策略
|
|
392
|
-
|
|
393
|
-
### 1. 全局 Mock 目录
|
|
394
|
-
|
|
395
|
-
每个包可暴露同名子目录,提供持久 Mock,供其他包在测试时自动使用。
|
|
396
|
-
|
|
397
|
-
```typescript
|
|
398
|
-
// packages/fe-corekit/__mocks__/index.ts
|
|
399
|
-
import { vi } from 'vitest';
|
|
400
|
-
|
|
401
|
-
export const MyUtility = {
|
|
402
|
-
doSomething: vi.fn(() => 'mocked'),
|
|
403
|
-
processData: vi.fn((input: string) => `processed-${input}`)
|
|
404
|
-
};
|
|
405
|
-
export default MyUtility;
|
|
406
|
-
```
|
|
407
|
-
|
|
408
|
-
### 2. 文件级 Mock
|
|
409
|
-
|
|
410
|
-
```typescript
|
|
411
|
-
// 在测试文件顶部
|
|
412
|
-
vi.mock('../src/Util', () => ({
|
|
413
|
-
Util: {
|
|
414
|
-
ensureDir: vi.fn(),
|
|
415
|
-
readFile: vi.fn()
|
|
416
|
-
}
|
|
417
|
-
}));
|
|
418
|
-
|
|
419
|
-
vi.mock('js-cookie', () => {
|
|
420
|
-
let store: Record<string, string> = {};
|
|
421
|
-
|
|
422
|
-
const get = vi.fn((key?: string) => {
|
|
423
|
-
if (typeof key === 'string') return store[key];
|
|
424
|
-
return { ...store };
|
|
425
|
-
});
|
|
426
|
-
|
|
427
|
-
const set = vi.fn((key: string, value: string) => {
|
|
428
|
-
store[key] = value;
|
|
429
|
-
});
|
|
430
|
-
|
|
431
|
-
const remove = vi.fn((key: string) => {
|
|
432
|
-
delete store[key];
|
|
433
|
-
});
|
|
434
|
-
|
|
435
|
-
const __resetStore = () => {
|
|
436
|
-
store = {};
|
|
437
|
-
};
|
|
438
|
-
|
|
439
|
-
return {
|
|
440
|
-
default: { get, set, remove, __resetStore }
|
|
441
|
-
};
|
|
442
|
-
});
|
|
443
|
-
```
|
|
444
|
-
|
|
445
|
-
### 3. 动态 Mock
|
|
446
|
-
|
|
447
|
-
```typescript
|
|
448
|
-
describe('ServiceClass', () => {
|
|
449
|
-
it('should handle API failure', async () => {
|
|
450
|
-
// 临时 Mock API 调用失败
|
|
451
|
-
vi.spyOn(apiClient, 'request').mockRejectedValue(
|
|
452
|
-
new Error('Network error')
|
|
453
|
-
);
|
|
454
|
-
|
|
455
|
-
await expect(service.fetchData()).rejects.toThrow('Network error');
|
|
456
|
-
|
|
457
|
-
// 恢复原始实现
|
|
458
|
-
vi.restoreAllMocks();
|
|
459
|
-
});
|
|
460
|
-
});
|
|
461
|
-
```
|
|
462
|
-
|
|
463
|
-
### 4. Mock 类实例
|
|
464
|
-
|
|
465
|
-
```typescript
|
|
466
|
-
class MockStorage<Key = string> implements SyncStorageInterface<Key> {
|
|
467
|
-
public data = new Map<string, string>();
|
|
468
|
-
public calls: {
|
|
469
|
-
setItem: Array<{ key: Key; value: unknown; options?: unknown }>;
|
|
470
|
-
getItem: Array<{ key: Key; defaultValue?: unknown; options?: unknown }>;
|
|
471
|
-
removeItem: Array<{ key: Key; options?: unknown }>;
|
|
472
|
-
clear: number;
|
|
473
|
-
} = {
|
|
474
|
-
setItem: [],
|
|
475
|
-
getItem: [],
|
|
476
|
-
removeItem: [],
|
|
477
|
-
clear: 0
|
|
478
|
-
};
|
|
479
|
-
|
|
480
|
-
setItem<T>(key: Key, value: T, options?: unknown): void {
|
|
481
|
-
this.calls.setItem.push({ key, value, options });
|
|
482
|
-
this.data.set(String(key), String(value));
|
|
483
|
-
}
|
|
484
|
-
|
|
485
|
-
getItem<T>(key: Key, defaultValue?: T, options?: unknown): T | null {
|
|
486
|
-
this.calls.getItem.push({ key, defaultValue, options });
|
|
487
|
-
const value = this.data.get(String(key));
|
|
488
|
-
return (value ?? defaultValue ?? null) as T | null;
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
reset(): void {
|
|
492
|
-
this.data.clear();
|
|
493
|
-
this.calls = {
|
|
494
|
-
setItem: [],
|
|
495
|
-
getItem: [],
|
|
496
|
-
removeItem: [],
|
|
497
|
-
clear: 0
|
|
498
|
-
};
|
|
499
|
-
}
|
|
500
|
-
}
|
|
501
|
-
```
|
|
502
|
-
|
|
503
|
-
---
|
|
504
|
-
|
|
505
|
-
## 测试环境管理
|
|
506
|
-
|
|
507
|
-
### 生命周期钩子
|
|
508
|
-
|
|
509
|
-
```typescript
|
|
510
|
-
describe('ComponentTest', () => {
|
|
511
|
-
let component: Component;
|
|
512
|
-
let mockDependency: MockDependency;
|
|
513
|
-
|
|
514
|
-
beforeAll(() => {
|
|
515
|
-
// 整个测试套件运行前的一次性设置
|
|
516
|
-
setupGlobalTestEnvironment();
|
|
517
|
-
});
|
|
518
|
-
|
|
519
|
-
afterAll(() => {
|
|
520
|
-
// 整个测试套件运行后的一次性清理
|
|
521
|
-
cleanupGlobalTestEnvironment();
|
|
522
|
-
});
|
|
523
|
-
|
|
524
|
-
beforeEach(() => {
|
|
525
|
-
// 每个测试用例前的设置
|
|
526
|
-
vi.useFakeTimers();
|
|
527
|
-
mockDependency = new MockDependency();
|
|
528
|
-
component = new Component(mockDependency);
|
|
529
|
-
});
|
|
530
|
-
|
|
531
|
-
afterEach(() => {
|
|
532
|
-
// 每个测试用例后的清理
|
|
533
|
-
vi.useRealTimers();
|
|
534
|
-
vi.clearAllMocks();
|
|
535
|
-
mockDependency.reset();
|
|
536
|
-
});
|
|
537
|
-
});
|
|
538
|
-
```
|
|
539
|
-
|
|
540
|
-
### 文件系统测试
|
|
541
|
-
|
|
542
|
-
```typescript
|
|
543
|
-
describe('FileProcessor', () => {
|
|
544
|
-
const testDir = './test-files';
|
|
545
|
-
const testFilePath = path.join(testDir, 'test.json');
|
|
546
|
-
|
|
547
|
-
beforeAll(() => {
|
|
548
|
-
// 创建测试目录和文件
|
|
549
|
-
if (!fs.existsSync(testDir)) {
|
|
550
|
-
fs.mkdirSync(testDir, { recursive: true });
|
|
551
|
-
}
|
|
552
|
-
fs.writeFileSync(testFilePath, JSON.stringify({ test: 'data' }));
|
|
553
|
-
});
|
|
554
|
-
|
|
555
|
-
afterAll(() => {
|
|
556
|
-
// 清理测试文件
|
|
557
|
-
if (fs.existsSync(testDir)) {
|
|
558
|
-
fs.rmSync(testDir, { recursive: true, force: true });
|
|
559
|
-
}
|
|
560
|
-
});
|
|
561
|
-
|
|
562
|
-
it('should process file correctly', () => {
|
|
563
|
-
const processor = new FileProcessor();
|
|
564
|
-
const result = processor.processFile(testFilePath);
|
|
565
|
-
expect(result).toEqual({ test: 'data' });
|
|
566
|
-
});
|
|
567
|
-
});
|
|
568
|
-
```
|
|
569
|
-
|
|
570
|
-
---
|
|
571
|
-
|
|
572
|
-
## 边界测试与错误处理
|
|
573
|
-
|
|
574
|
-
### 边界值测试
|
|
575
|
-
|
|
576
|
-
```typescript
|
|
577
|
-
describe('ValidationUtils', () => {
|
|
578
|
-
describe('validateAge', () => {
|
|
579
|
-
it('should handle boundary values', () => {
|
|
580
|
-
// 边界值测试
|
|
581
|
-
expect(validateAge(0)).toBe(true); // 最小值
|
|
582
|
-
expect(validateAge(150)).toBe(true); // 最大值
|
|
583
|
-
expect(validateAge(-1)).toBe(false); // 小于最小值
|
|
584
|
-
expect(validateAge(151)).toBe(false); // 大于最大值
|
|
585
|
-
});
|
|
586
|
-
|
|
587
|
-
it('should handle edge cases', () => {
|
|
588
|
-
// 边界情况测试
|
|
589
|
-
expect(validateAge(null)).toBe(false);
|
|
590
|
-
expect(validateAge(undefined)).toBe(false);
|
|
591
|
-
expect(validateAge(NaN)).toBe(false);
|
|
592
|
-
expect(validateAge(Infinity)).toBe(false);
|
|
593
|
-
});
|
|
594
|
-
});
|
|
595
|
-
});
|
|
596
|
-
```
|
|
597
|
-
|
|
598
|
-
### 异步操作测试
|
|
599
|
-
|
|
600
|
-
```typescript
|
|
601
|
-
describe('AsyncService', () => {
|
|
602
|
-
it('should handle successful async operation', async () => {
|
|
603
|
-
const service = new AsyncService();
|
|
604
|
-
const result = await service.fetchData();
|
|
605
|
-
expect(result).toBeDefined();
|
|
606
|
-
});
|
|
607
|
-
|
|
608
|
-
it('should handle async operation failure', async () => {
|
|
609
|
-
const service = new AsyncService();
|
|
610
|
-
vi.spyOn(service, 'apiCall').mockRejectedValue(new Error('API Error'));
|
|
611
|
-
|
|
612
|
-
await expect(service.fetchData()).rejects.toThrow('API Error');
|
|
613
|
-
});
|
|
614
|
-
|
|
615
|
-
it('should handle timeout', async () => {
|
|
616
|
-
vi.useFakeTimers();
|
|
617
|
-
const service = new AsyncService();
|
|
618
|
-
|
|
619
|
-
const promise = service.fetchDataWithTimeout(1000);
|
|
620
|
-
vi.advanceTimersByTime(1001);
|
|
621
|
-
|
|
622
|
-
await expect(promise).rejects.toThrow('Timeout');
|
|
623
|
-
vi.useRealTimers();
|
|
624
|
-
});
|
|
625
|
-
});
|
|
626
|
-
```
|
|
627
|
-
|
|
628
|
-
### 类型安全测试
|
|
629
|
-
|
|
630
|
-
TypeScript 项目的测试不仅要验证运行时行为,还应该确保类型系统的正确性。Vitest 提供了 `expectTypeOf` 工具来进行编译时类型检查。
|
|
631
|
-
|
|
632
|
-
#### 为什么需要类型测试?
|
|
633
|
-
|
|
634
|
-
1. **类型推断验证**:确保 TypeScript 能正确推断复杂类型
|
|
635
|
-
2. **泛型约束检查**:验证泛型参数的约束条件
|
|
636
|
-
3. **类型兼容性**:确保类型定义与实际使用匹配
|
|
637
|
-
4. **API 契约保证**:防止类型定义的破坏性变更
|
|
638
|
-
|
|
639
|
-
#### 基础类型测试
|
|
640
|
-
|
|
641
|
-
```typescript
|
|
642
|
-
import { describe, it, expectTypeOf } from 'vitest';
|
|
643
|
-
|
|
644
|
-
describe('TypeSafetyTests', () => {
|
|
645
|
-
it('should maintain type safety', () => {
|
|
646
|
-
const processor = new DataProcessor<User>();
|
|
647
|
-
|
|
648
|
-
// 使用 expectTypeOf 进行类型检查
|
|
649
|
-
expectTypeOf(processor.process).parameter(0).toEqualTypeOf<User>();
|
|
650
|
-
expectTypeOf(processor.process).returns.toEqualTypeOf<ProcessedUser>();
|
|
651
|
-
});
|
|
652
|
-
|
|
653
|
-
it('should infer correct return types', () => {
|
|
654
|
-
const result = getData();
|
|
655
|
-
|
|
656
|
-
// 验证返回值类型
|
|
657
|
-
expectTypeOf(result).toEqualTypeOf<{ id: number; name: string }>();
|
|
658
|
-
expectTypeOf(result).not.toEqualTypeOf<{ id: string; name: string }>();
|
|
659
|
-
});
|
|
660
|
-
|
|
661
|
-
it('should validate parameter types', () => {
|
|
662
|
-
function processUser(user: User): void {
|
|
663
|
-
// implementation
|
|
664
|
-
}
|
|
665
|
-
|
|
666
|
-
// 验证参数类型
|
|
667
|
-
expectTypeOf(processUser).parameter(0).toMatchTypeOf<{ id: number }>();
|
|
668
|
-
expectTypeOf(processUser).parameter(0).toHaveProperty('id');
|
|
669
|
-
});
|
|
670
|
-
});
|
|
671
|
-
```
|
|
672
|
-
|
|
673
|
-
#### 泛型类型测试
|
|
674
|
-
|
|
675
|
-
```typescript
|
|
676
|
-
describe('Generic Type Tests', () => {
|
|
677
|
-
it('should work with generic constraints', () => {
|
|
678
|
-
class Storage<T extends { id: number }> {
|
|
679
|
-
store(item: T): T {
|
|
680
|
-
return item;
|
|
681
|
-
}
|
|
682
|
-
}
|
|
683
|
-
|
|
684
|
-
const storage = new Storage<User>();
|
|
685
|
-
|
|
686
|
-
// 验证泛型类型
|
|
687
|
-
expectTypeOf(storage.store).parameter(0).toMatchTypeOf<User>();
|
|
688
|
-
expectTypeOf(storage.store).returns.toMatchTypeOf<User>();
|
|
689
|
-
});
|
|
690
|
-
|
|
691
|
-
it('should validate complex generic types', () => {
|
|
692
|
-
type ApiResponse<T> = {
|
|
693
|
-
data: T;
|
|
694
|
-
status: number;
|
|
695
|
-
message?: string;
|
|
696
|
-
};
|
|
697
|
-
|
|
698
|
-
const response: ApiResponse<User[]> = {
|
|
699
|
-
data: [],
|
|
700
|
-
status: 200
|
|
701
|
-
};
|
|
702
|
-
|
|
703
|
-
// 验证嵌套泛型类型
|
|
704
|
-
expectTypeOf(response).toMatchTypeOf<ApiResponse<User[]>>();
|
|
705
|
-
expectTypeOf(response.data).toEqualTypeOf<User[]>();
|
|
706
|
-
});
|
|
707
|
-
});
|
|
708
|
-
```
|
|
709
|
-
|
|
710
|
-
#### 联合类型与交叉类型测试
|
|
711
|
-
|
|
712
|
-
```typescript
|
|
713
|
-
describe('Union and Intersection Types', () => {
|
|
714
|
-
it('should handle union types correctly', () => {
|
|
715
|
-
type Result = Success | Error;
|
|
716
|
-
type Success = { status: 'success'; data: string };
|
|
717
|
-
type Error = { status: 'error'; message: string };
|
|
718
|
-
|
|
719
|
-
function handleResult(result: Result): void {
|
|
720
|
-
// implementation
|
|
721
|
-
}
|
|
722
|
-
|
|
723
|
-
// 验证联合类型
|
|
724
|
-
expectTypeOf(handleResult).parameter(0).toMatchTypeOf<Success>();
|
|
725
|
-
expectTypeOf(handleResult).parameter(0).toMatchTypeOf<Error>();
|
|
726
|
-
});
|
|
727
|
-
|
|
728
|
-
it('should handle intersection types correctly', () => {
|
|
729
|
-
type Timestamped = { createdAt: Date; updatedAt: Date };
|
|
730
|
-
type UserWithTimestamp = User & Timestamped;
|
|
731
|
-
|
|
732
|
-
const user: UserWithTimestamp = {
|
|
733
|
-
id: 1,
|
|
734
|
-
name: 'John',
|
|
735
|
-
createdAt: new Date(),
|
|
736
|
-
updatedAt: new Date()
|
|
737
|
-
};
|
|
738
|
-
|
|
739
|
-
// 验证交叉类型包含所有属性
|
|
740
|
-
expectTypeOf(user).toHaveProperty('id');
|
|
741
|
-
expectTypeOf(user).toHaveProperty('name');
|
|
742
|
-
expectTypeOf(user).toHaveProperty('createdAt');
|
|
743
|
-
expectTypeOf(user).toHaveProperty('updatedAt');
|
|
744
|
-
});
|
|
745
|
-
});
|
|
746
|
-
```
|
|
747
|
-
|
|
748
|
-
#### 类型窄化测试
|
|
749
|
-
|
|
750
|
-
```typescript
|
|
751
|
-
describe('Type Narrowing Tests', () => {
|
|
752
|
-
it('should validate type guards', () => {
|
|
753
|
-
function isString(value: unknown): value is string {
|
|
754
|
-
return typeof value === 'string';
|
|
755
|
-
}
|
|
756
|
-
|
|
757
|
-
const value: unknown = 'test';
|
|
758
|
-
|
|
759
|
-
if (isString(value)) {
|
|
760
|
-
// 在此作用域内,value 应该被窄化为 string 类型
|
|
761
|
-
expectTypeOf(value).toEqualTypeOf<string>();
|
|
762
|
-
}
|
|
763
|
-
});
|
|
764
|
-
|
|
765
|
-
it('should validate discriminated unions', () => {
|
|
766
|
-
type Shape =
|
|
767
|
-
| { kind: 'circle'; radius: number }
|
|
768
|
-
| { kind: 'rectangle'; width: number; height: number };
|
|
769
|
-
|
|
770
|
-
function getArea(shape: Shape): number {
|
|
771
|
-
if (shape.kind === 'circle') {
|
|
772
|
-
// 在此分支,shape 应该被窄化为 circle 类型
|
|
773
|
-
expectTypeOf(shape).toHaveProperty('radius');
|
|
774
|
-
expectTypeOf(shape).not.toHaveProperty('width');
|
|
775
|
-
return Math.PI * shape.radius ** 2;
|
|
776
|
-
} else {
|
|
777
|
-
// 在此分支,shape 应该被窄化为 rectangle 类型
|
|
778
|
-
expectTypeOf(shape).toHaveProperty('width');
|
|
779
|
-
expectTypeOf(shape).toHaveProperty('height');
|
|
780
|
-
return shape.width * shape.height;
|
|
781
|
-
}
|
|
782
|
-
}
|
|
783
|
-
});
|
|
784
|
-
});
|
|
785
|
-
```
|
|
786
|
-
|
|
787
|
-
#### 实用建议
|
|
788
|
-
|
|
789
|
-
1. **结合运行时测试**:类型测试应该与运行时测试相辅相成
|
|
790
|
-
|
|
791
|
-
```typescript
|
|
792
|
-
describe('Combined Runtime and Type Tests', () => {
|
|
793
|
-
it('should validate both runtime behavior and types', () => {
|
|
794
|
-
function add(a: number, b: number): number {
|
|
795
|
-
return a + b;
|
|
796
|
-
}
|
|
797
|
-
|
|
798
|
-
// 类型测试
|
|
799
|
-
expectTypeOf(add).parameter(0).toEqualTypeOf<number>();
|
|
800
|
-
expectTypeOf(add).returns.toEqualTypeOf<number>();
|
|
801
|
-
|
|
802
|
-
// 运行时测试
|
|
803
|
-
expect(add(1, 2)).toBe(3);
|
|
804
|
-
expect(add(-1, 1)).toBe(0);
|
|
805
|
-
});
|
|
806
|
-
});
|
|
807
|
-
```
|
|
808
|
-
|
|
809
|
-
2. **测试类型推断**:确保 TypeScript 能正确推断类型,避免过度使用 `any`
|
|
810
|
-
|
|
811
|
-
```typescript
|
|
812
|
-
describe('Type Inference Tests', () => {
|
|
813
|
-
it('should infer types correctly', () => {
|
|
814
|
-
const data = { id: 1, name: 'John' };
|
|
815
|
-
|
|
816
|
-
// 验证推断的类型
|
|
817
|
-
expectTypeOf(data).toEqualTypeOf<{ id: number; name: string }>();
|
|
818
|
-
expectTypeOf(data.id).toEqualTypeOf<number>();
|
|
819
|
-
expectTypeOf(data.name).toEqualTypeOf<string>();
|
|
820
|
-
});
|
|
821
|
-
});
|
|
822
|
-
```
|
|
823
|
-
|
|
824
|
-
3. **使用 TypeScript 编译器检查**:在 CI 中运行 `tsc --noEmit` 确保没有类型错误
|
|
825
|
-
|
|
826
|
-
```bash
|
|
827
|
-
# 在 package.json 中添加脚本
|
|
828
|
-
{
|
|
829
|
-
"scripts": {
|
|
830
|
-
"type-check": "tsc --noEmit",
|
|
831
|
-
"test": "pnpm type-check && vitest run"
|
|
832
|
-
}
|
|
833
|
-
}
|
|
834
|
-
```
|
|
835
|
-
|
|
836
|
-
---
|
|
837
|
-
|
|
838
|
-
## 性能测试
|
|
839
|
-
|
|
840
|
-
```typescript
|
|
841
|
-
describe('PerformanceTests', () => {
|
|
842
|
-
it('should complete operation within time limit', async () => {
|
|
843
|
-
const startTime = Date.now();
|
|
844
|
-
const processor = new DataProcessor();
|
|
845
|
-
|
|
846
|
-
await processor.processLargeDataset(largeDataset);
|
|
847
|
-
|
|
848
|
-
const endTime = Date.now();
|
|
849
|
-
const duration = endTime - startTime;
|
|
850
|
-
|
|
851
|
-
expect(duration).toBeLessThan(1000); // 应在1秒内完成
|
|
852
|
-
});
|
|
853
|
-
});
|
|
854
|
-
```
|
|
855
|
-
|
|
856
|
-
---
|
|
857
|
-
|
|
858
|
-
## 运行测试
|
|
859
|
-
|
|
860
|
-
```bash
|
|
861
|
-
# 运行所有包测试
|
|
862
|
-
pnpm test
|
|
863
|
-
|
|
864
|
-
# 仅运行指定包
|
|
865
|
-
pnpm --filter @qlover/fe-corekit test
|
|
866
|
-
|
|
867
|
-
# 监听模式
|
|
868
|
-
pnpm test:watch
|
|
869
|
-
|
|
870
|
-
# 生成覆盖率报告
|
|
871
|
-
pnpm test:coverage
|
|
872
|
-
```
|
|
873
|
-
|
|
874
|
-
在 CI 中,可借助 GitHub Actions:
|
|
875
|
-
|
|
876
|
-
```yaml
|
|
877
|
-
# .github/workflows/test.yml (截断)
|
|
878
|
-
- run: pnpm install
|
|
879
|
-
- run: pnpm test:coverage
|
|
880
|
-
- uses: codecov/codecov-action@v3
|
|
881
|
-
```
|
|
882
|
-
|
|
883
|
-
---
|
|
884
|
-
|
|
885
|
-
## 覆盖率目标
|
|
886
|
-
|
|
887
|
-
| 指标 | 目标 |
|
|
888
|
-
| ---- | ----- |
|
|
889
|
-
| 语句 | ≥ 80% |
|
|
890
|
-
| 分支 | ≥ 75% |
|
|
891
|
-
| 函数 | ≥ 85% |
|
|
892
|
-
| 行数 | ≥ 80% |
|
|
893
|
-
|
|
894
|
-
覆盖率报告默认输出至 `coverage/` 目录,`index.html` 可本地浏览。
|
|
895
|
-
|
|
896
|
-
---
|
|
897
|
-
|
|
898
|
-
## 调试
|
|
899
|
-
|
|
900
|
-
### VS Code Launch 配置
|
|
901
|
-
|
|
902
|
-
```jsonc
|
|
903
|
-
{
|
|
904
|
-
"version": "0.2.0",
|
|
905
|
-
"configurations": [
|
|
906
|
-
{
|
|
907
|
-
"name": "Debug Vitest",
|
|
908
|
-
"type": "node",
|
|
909
|
-
"request": "launch",
|
|
910
|
-
"program": "${workspaceFolder}/node_modules/vitest/vitest.mjs",
|
|
911
|
-
"args": ["run", "--reporter=verbose"],
|
|
912
|
-
"console": "integratedTerminal",
|
|
913
|
-
"internalConsoleOptions": "neverOpen"
|
|
914
|
-
}
|
|
915
|
-
]
|
|
916
|
-
}
|
|
917
|
-
```
|
|
918
|
-
|
|
919
|
-
> 在测试代码中可使用 `console.log` / `debugger` 辅助排查。
|
|
920
|
-
|
|
921
|
-
---
|
|
922
|
-
|
|
923
|
-
## 常见问题解答 (FAQ)
|
|
924
|
-
|
|
925
|
-
### Q1 : 如何 Mock 浏览器 API?
|
|
926
|
-
|
|
927
|
-
使用 `vi.mock()` 或在 `setupFiles` 中全局覆写,例如:
|
|
928
|
-
|
|
929
|
-
```typescript
|
|
930
|
-
globalThis.requestAnimationFrame = (cb) => setTimeout(cb, 16);
|
|
931
|
-
```
|
|
932
|
-
|
|
933
|
-
### Q2 : 测试过慢怎么办?
|
|
934
|
-
|
|
935
|
-
- 使用 `vi.useFakeTimers()` 加速时间相关逻辑。
|
|
936
|
-
- 拆分长时间集成流程为独立单元测试。
|
|
937
|
-
|
|
938
|
-
### Q3 : 如何测试 TypeScript 类型?
|
|
939
|
-
|
|
940
|
-
利用 `expectTypeOf`:
|
|
941
|
-
|
|
942
|
-
```typescript
|
|
943
|
-
import { expectTypeOf } from 'vitest';
|
|
944
|
-
|
|
945
|
-
expectTypeOf(MyUtility.doSomething).returns.toEqualTypeOf<string>();
|
|
946
|
-
```
|
|
947
|
-
|
|
948
|
-
### Q4 : 如何测试私有方法?
|
|
949
|
-
|
|
950
|
-
```typescript
|
|
951
|
-
// 通过类型断言访问私有方法
|
|
952
|
-
it('should test private method', () => {
|
|
953
|
-
const instance = new MyClass();
|
|
954
|
-
const result = (instance as any).privateMethod();
|
|
955
|
-
expect(result).toBe('expected');
|
|
956
|
-
});
|
|
957
|
-
```
|
|
958
|
-
|
|
959
|
-
### Q5 : 如何处理依赖注入的测试?
|
|
960
|
-
|
|
961
|
-
```typescript
|
|
962
|
-
describe('ServiceWithDependencies', () => {
|
|
963
|
-
let mockRepository: MockRepository;
|
|
964
|
-
let service: UserService;
|
|
965
|
-
|
|
966
|
-
beforeEach(() => {
|
|
967
|
-
mockRepository = new MockRepository();
|
|
968
|
-
service = new UserService(mockRepository);
|
|
969
|
-
});
|
|
970
|
-
|
|
971
|
-
it('should use injected dependency', () => {
|
|
972
|
-
service.getUser(1);
|
|
973
|
-
expect(mockRepository.findById).toHaveBeenCalledWith(1);
|
|
974
|
-
});
|
|
975
|
-
});
|
|
976
|
-
```
|