feat: remove vitest/jest define method and support vitest in-source test#715
feat: remove vitest/jest define method and support vitest in-source test#715
Conversation
More templates
@ice/create-pkg
ice-npm-utils
@ice/pkg
@ice/pkg-plugin-jsx-plus
@ice/pkg-plugin-mf
commit: |
There was a problem hiding this comment.
Pull request overview
This PR removes the built-in Jest/Vitest config helper exports from @ice/pkg and instead improves native Vitest compatibility by defining an import.meta.vitest guard at build time, while updating docs/examples to reflect the new testing setup.
Changes:
- Remove
defineJestConfig/defineVitestConfigtest helpers from the public API (major change). - Inject default define
import.meta.vitest = undefined(as a string expression) to support Vitest in-source tests without shipping test branches to production builds. - Update docs and examples; change Transform mode default excludes to ignore
__tests__directories.
Reviewed changes
Copilot reviewed 14 out of 14 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| website/docs/reference/config.md | Documents new default define key and new default transform.excludes. |
| website/docs/guide/test.md | Rewrites Jest/Vitest setup docs to not rely on removed helper exports. |
| packages/pkg/tests/defaultDefine.test.ts | Adds coverage for new default define values. |
| packages/pkg/src/types.ts | Clarifies default behavior for transform.excludes. |
| packages/pkg/src/test/index.ts | Removes re-exports for Jest/Vitest helper configs. |
| packages/pkg/src/test/getTaskConfig.ts | Removes internal helper used by removed test-config utilities. |
| packages/pkg/src/test/defineVitestConfig.ts | Removes Vitest config helper implementation. |
| packages/pkg/src/test/defineJestConfig.ts | Removes Jest config helper implementation. |
| packages/pkg/src/tasks/transform.ts | Sets default excludes to ['**/__tests__/**'] for Transform mode. |
| packages/pkg/src/index.ts | Stops exporting the removed test helpers from package entry. |
| packages/pkg/src/engine/shared/define.ts | Adds import.meta.vitest default define replacement. |
| examples/react-component/vitest.config.mts | Updates example to plain Vitest config (needs alias handling). |
| examples/react-component/jest.config.mjs | Updates example to plain Jest config (needs alias handling). |
| .changeset/gentle-chicken-push.md | Marks @ice/pkg as a major release for this breaking change. |
Comments suppressed due to low confidence (1)
examples/react-component/vitest.config.mts:9
- 该示例项目源码使用了
@路径别名(tsconfig.json里也配置了@/*),但当前 Vitest/Vite 配置未设置resolve.alias(或vite-tsconfig-paths),导致运行示例测试时@/...依赖无法解析。建议在此处补充 alias 配置以指向./src,或引入并启用vite-tsconfig-paths插件。
import { defineConfig } from 'vitest/config';
export default defineConfig(() => ({
test: {
environment: 'jsdom',
setupFiles: ['./vitest-setup.ts'],
globals: true,
},
}));
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Agent-Logs-Url: https://github.com/ice-lab/icepkg/sessions/a94bb9e8-0088-437b-b2a1-b1f02c6d2d88 Co-authored-by: XGHeaven <9291212+XGHeaven@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 14 out of 14 changed files in this pull request and generated 5 comments.
Comments suppressed due to low confidence (2)
examples/react-component/vitest.config.mts:8
- 当前 Vitest 配置没有同步项目在 build.config.mts / tsconfig.json 中的
@路径别名(源码里有import Input from '@/components/Input'),Vitest 运行时会解析失败。建议在 vitest.config.mts 中补充resolve.alias(或使用 vite-tsconfig-paths)以确保@/*能指向src/*。
import { defineConfig } from 'vitest/config';
export default defineConfig(() => ({
test: {
environment: 'jsdom',
setupFiles: ['./vitest-setup.ts'],
globals: true,
},
examples/react-component/vitest.config.mts:8
- 该示例的组件代码里直接访问了全局常量
__DEV__(例如 Test 组件),但当前 Vitest 配置没有通过define或全局 setup 注入该常量,运行测试会在运行时抛出 ReferenceError。建议在 Vitest 配置中增加define.__DEV__(或在 setupFiles 中设置 globalThis.DEV)。
export default defineConfig(() => ({
test: {
environment: 'jsdom',
setupFiles: ['./vitest-setup.ts'],
globals: true,
},
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| 快速开始时,可以先在项目根目录创建 `jest.config.mjs`: | ||
|
|
||
| export default defineJestConfig(pkgService, { | ||
| // 你也可以使用 @swc/jest 编译 TS 代码 | ||
| ```js title="jest.config.mjs" | ||
| export default { | ||
| preset: 'ts-jest', | ||
| }); | ||
| testEnvironment: 'jest-environment-jsdom', | ||
| setupFilesAfterEnv: ['<rootDir>/jest-setup.ts'], | ||
| }; |
There was a problem hiding this comment.
Jest 快速开始配置没有说明如何处理 @/* 这类路径别名(在本仓库示例和配置参考中很常见),容易导致 Cannot find module '@/...'。建议在此处补充 moduleNameMapper(例如把 ^@/(.*)$ 映射到 <rootDir>/src/$1)或说明使用 ts-jest 的 pathsToModuleNameMapper。
| 快速开始时,可以先在项目根目录创建 `vitest.config.mts`: | ||
|
|
||
| export default defineVitestConfig(pkgService, {}); | ||
| ```js title="vitest.config.mts" | ||
| import { defineConfig } from 'vitest/config'; | ||
|
|
||
| export default defineConfig({ | ||
| test: { | ||
| environment: 'jsdom', | ||
| setupFiles: ['./vitest-setup.ts'], | ||
| globals: true, | ||
| }, | ||
| }); |
There was a problem hiding this comment.
Vitest 快速开始配置未提及如何对齐构建配置中的路径别名(如 alias: { '@': './src' } / tsconfig paths)。当源码内部使用 @/ 导入时,Vitest 会解析失败。建议在此处补充 resolve.alias(或推荐 vite-tsconfig-paths)相关说明。
| - 默认值:`{ __DEV__: 'true' | 'false', 'process.env.NODE_ENV': '"development"' | '"production"' }` | ||
| - 默认值:`{ __DEV__: 'true' | 'false', 'process.env.NODE_ENV': '"development"' | '"production"', 'import.meta.vitest': 'undefined' }` | ||
|
|
||
| 定义编译时环境变量,会在编译时被替换。注意:属性值会经过一次 `JSON.stringify()` 转换。 |
There was a problem hiding this comment.
此处说明“属性值会经过一次 JSON.stringify() 转换”,但默认注入的 import.meta.vitest: 'undefined' 实际需要作为原始表达式参与替换(不能再 stringify 成 '"undefined"')。建议补充一句说明:用户传入的 define 值会 stringify,而内置的 import.meta.vitest 是以原始 undefined 表达式注入(且可被用户 define 覆盖)。
| 定义编译时环境变量,会在编译时被替换。注意:属性值会经过一次 `JSON.stringify()` 转换。 | |
| 定义编译时环境变量,会在编译时被替换。注意:用户传入的属性值会经过一次 `JSON.stringify()` 转换。内置默认注入的 `import.meta.vitest` 则是以原始 `undefined` 表达式参与替换,不会再被转换为 `"undefined"`,且可被用户传入的 `define` 配置覆盖。 |
| --- | ||
| '@ice/pkg': major | ||
| --- | ||
|
|
||
| feat: remove vitest/jest define method and support vitest in-source test |
There was a problem hiding this comment.
PR 描述目前聚焦在示例 Jest 的 moduleNameMapper 修复,但本 changeset 声明了 @ice/pkg 的 major 变更(移除 defineJestConfig/defineVitestConfig 等公共 API、默认 define/transform 行为调整)。建议同步更新 PR 描述,明确包含的破坏性变更点与迁移方式,避免发布说明与实际变更不一致。
| export default { | ||
| preset: 'ts-jest', | ||
| setupFilesAfterEnv: ['<rootDir>/jest-setup.ts'], | ||
| testEnvironment: 'jest-environment-jsdom', |
There was a problem hiding this comment.
该示例源码(如 Test 组件)直接访问 __DEV__,但当前 Jest 配置/初始化脚本没有注入该全局常量,运行测试会在运行时抛出 ReferenceError。建议在 Jest 启动前通过 setupFiles / setupFilesAfterEnv 设置 globalThis.__DEV__(或在 Jest 配置的 globals 中提供 __DEV__)。
| testEnvironment: 'jest-environment-jsdom', | |
| testEnvironment: 'jest-environment-jsdom', | |
| globals: { | |
| __DEV__: true, | |
| }, |
The
examples/react-componentJest config was missing amoduleNameMapperentry for the@/path alias used throughout its source files (e.g.,import Input from '@/components/Input'), causing module resolution failures when running tests.Changes
examples/react-component/jest.config.mjs: Added'^@/(.*)$': '<rootDir>/src/$1'tomoduleNameMapperso Jest resolves@/*imports tosrc/*.