Skip to content

feat: remove vitest/jest define method and support vitest in-source test#715

Merged
XGHeaven merged 3 commits intonextfrom
feat/remove-test-file
Apr 8, 2026
Merged

feat: remove vitest/jest define method and support vitest in-source test#715
XGHeaven merged 3 commits intonextfrom
feat/remove-test-file

Conversation

@XGHeaven
Copy link
Copy Markdown
Collaborator

@XGHeaven XGHeaven commented Apr 8, 2026

The examples/react-component Jest config was missing a moduleNameMapper entry 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' to moduleNameMapper so Jest resolves @/* imports to src/*.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Apr 8, 2026

More templates

@ice/create-pkg

npm i https://pkg.pr.new/ice-lab/icepkg/@ice/create-pkg@715

ice-npm-utils

npm i https://pkg.pr.new/ice-lab/icepkg/ice-npm-utils@715

@ice/pkg

npm i https://pkg.pr.new/ice-lab/icepkg/@ice/pkg@715

@ice/pkg-plugin-jsx-plus

npm i https://pkg.pr.new/ice-lab/icepkg/@ice/pkg-plugin-jsx-plus@715

@ice/pkg-plugin-mf

npm i https://pkg.pr.new/ice-lab/icepkg/@ice/pkg-plugin-mf@715

commit: b6796f7

@XGHeaven XGHeaven mentioned this pull request Apr 8, 2026
30 tasks
@XGHeaven XGHeaven requested a review from Copilot April 8, 2026 10:03
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 / defineVitestConfig test 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>
Copilot AI changed the title feat: remove vitest/jest define method and support vitest in-source test fix: add @/* path alias mapping to react-component Jest config Apr 8, 2026
@XGHeaven XGHeaven changed the title fix: add @/* path alias mapping to react-component Jest config feat: remove vitest/jest define method and support vitest in-source test Apr 8, 2026
@XGHeaven XGHeaven requested a review from Copilot April 8, 2026 11:17
@XGHeaven XGHeaven merged commit 2c04129 into next Apr 8, 2026
6 checks passed
@XGHeaven XGHeaven deleted the feat/remove-test-file branch April 8, 2026 11:23
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +24 to +31
快速开始时,可以先在项目根目录创建 `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'],
};
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jest 快速开始配置没有说明如何处理 @/* 这类路径别名(在本仓库示例和配置参考中很常见),容易导致 Cannot find module '@/...'。建议在此处补充 moduleNameMapper(例如把 ^@/(.*)$ 映射到 <rootDir>/src/$1)或说明使用 ts-jest 的 pathsToModuleNameMapper。

Copilot uses AI. Check for mistakes.
Comment on lines +131 to +142
快速开始时,可以先在项目根目录创建 `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,
},
});
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vitest 快速开始配置未提及如何对齐构建配置中的路径别名(如 alias: { '@': './src' } / tsconfig paths)。当源码内部使用 @/ 导入时,Vitest 会解析失败。建议在此处补充 resolve.alias(或推荐 vite-tsconfig-paths)相关说明。

Copilot uses AI. Check for mistakes.
- 默认值:`{ __DEV__: 'true' | 'false', 'process.env.NODE_ENV': '"development"' | '"production"' }`
- 默认值:`{ __DEV__: 'true' | 'false', 'process.env.NODE_ENV': '"development"' | '"production"', 'import.meta.vitest': 'undefined' }`

定义编译时环境变量,会在编译时被替换。注意:属性值会经过一次 `JSON.stringify()` 转换。
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

此处说明“属性值会经过一次 JSON.stringify() 转换”,但默认注入的 import.meta.vitest: 'undefined' 实际需要作为原始表达式参与替换(不能再 stringify 成 '"undefined"')。建议补充一句说明:用户传入的 define 值会 stringify,而内置的 import.meta.vitest 是以原始 undefined 表达式注入(且可被用户 define 覆盖)。

Suggested change
定义编译时环境变量,会在编译时被替换。注意:属性值会经过一次 `JSON.stringify()` 转换。
定义编译时环境变量,会在编译时被替换。注意:用户传入的属性值会经过一次 `JSON.stringify()` 转换。内置默认注入的 `import.meta.vitest` 则是以原始 `undefined` 表达式参与替换,不会再被转换为 `"undefined"`,且可被用户传入的 `define` 配置覆盖

Copilot uses AI. Check for mistakes.
Comment on lines +1 to +5
---
'@ice/pkg': major
---

feat: remove vitest/jest define method and support vitest in-source test
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR 描述目前聚焦在示例 Jest 的 moduleNameMapper 修复,但本 changeset 声明了 @ice/pkg 的 major 变更(移除 defineJestConfig/defineVitestConfig 等公共 API、默认 define/transform 行为调整)。建议同步更新 PR 描述,明确包含的破坏性变更点与迁移方式,避免发布说明与实际变更不一致。

Copilot uses AI. Check for mistakes.
export default {
preset: 'ts-jest',
setupFilesAfterEnv: ['<rootDir>/jest-setup.ts'],
testEnvironment: 'jest-environment-jsdom',
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

该示例源码(如 Test 组件)直接访问 __DEV__,但当前 Jest 配置/初始化脚本没有注入该全局常量,运行测试会在运行时抛出 ReferenceError。建议在 Jest 启动前通过 setupFiles / setupFilesAfterEnv 设置 globalThis.__DEV__(或在 Jest 配置的 globals 中提供 __DEV__)。

Suggested change
testEnvironment: 'jest-environment-jsdom',
testEnvironment: 'jest-environment-jsdom',
globals: {
__DEV__: true,
},

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants