@bluelovers/jest-config 1.1.11 → 1.1.13

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/src/defaults.ts CHANGED
@@ -1,28 +1,48 @@
1
- import { IOptions, requireResolveExtra } from '@yarn-tool/require-resolve';
1
+ /**
2
+ * 預設配置模組 - 提供 Jest 各項預設設定值
3
+ * Default Configuration Module - Provides default settings for Jest
4
+ */
5
+
2
6
  import { ITSToWriteableArray } from 'ts-type/lib/helper/array/readonly';
3
- import { ITSWriteable } from 'ts-type/lib/helper/readonly';
4
- import { _handleFileExtensions, _requireResolve } from './helper';
5
- import { InitialOptionsTsJest } from 'ts-jest';
6
- import { defaultTsJestTransformerOptions } from './plugin/ts-jest';
7
- import { IRuntime } from './types';
8
7
 
8
+ /**
9
+ * 取得預設的測試檔案副檔名列表
10
+ * Get default test file extensions list
11
+ *
12
+ * 定義哪些副檔名的檔案會被視為測試檔案
13
+ * Defines which file extensions are recognized as test files
14
+ *
15
+ * @returns {string[]} 測試檔案副檔名陣列 / Array of test file extensions
16
+ */
9
17
  export function defaultTestFileExtensions()
10
18
  {
11
19
  const value = [
20
+ // TypeScript 相關副檔名 / TypeScript related extensions
21
+
12
22
  'ts',
13
23
  'tsx',
14
24
  'mts',
15
25
  'cts',
16
- // 'js',
17
- // 'jsx',
18
- // 'mjs',
19
- // 'cjs',
26
+
27
+ // JavaScript 相關副檔名(目前註解掉) / JavaScript related extensions (currently commented)
28
+
29
+ //'js',
30
+ //'jsx',
31
+ //'mjs',
32
+ //'cjs',
20
33
  ] as const;
21
34
  return value as ITSToWriteableArray<typeof value>;
22
35
  }
23
36
 
24
37
  /**
38
+ * 取得預設的模組檔案副檔名列表
39
+ * Get default module file extensions list
40
+ *
41
+ * 定義模組解析時搜尋的副檔名順序
42
+ * Defines the order of extensions to search during module resolution
43
+ *
25
44
  * @see https://jestjs.io/docs/configuration#options
45
+ * @returns {string[]} 模組檔案副檔名陣列 / Array of module file extensions
26
46
  */
27
47
  export function defaultModuleFileExtensions()
28
48
  {
@@ -41,6 +61,15 @@ export function defaultModuleFileExtensions()
41
61
  return value as ITSToWriteableArray<typeof value>;
42
62
  }
43
63
 
64
+ /**
65
+ * 取得預設的覆蓋率檔案副檔名列表
66
+ * Get default coverage file extensions list
67
+ *
68
+ * 定義哪些副檔名的檔案會被納入覆蓋率統計
69
+ * Defines which file extensions are included in coverage statistics
70
+ *
71
+ * @returns {string[]} 覆蓋率檔案副檔名陣列 / Array of coverage file extensions
72
+ */
44
73
  export function defaultCoverageFileExtensions()
45
74
  {
46
75
  const value = [
@@ -58,6 +87,15 @@ export function defaultCoverageFileExtensions()
58
87
  return value as ITSToWriteableArray<typeof value>;
59
88
  }
60
89
 
90
+ /**
91
+ * 取得預設的轉換器檔案副檔名列表
92
+ * Get default transformer file extensions list
93
+ *
94
+ * 定義哪些副檔名的檔案需要經過 ts-jest 轉換
95
+ * Defines which file extensions need to be transformed by ts-jest
96
+ *
97
+ * @returns {string[]} 轉換器檔案副檔名陣列 / Array of transformer file extensions
98
+ */
61
99
  export function defaultTransformFileExtensions()
62
100
  {
63
101
  const value = [
@@ -69,6 +107,15 @@ export function defaultTransformFileExtensions()
69
107
  return value as ITSToWriteableArray<typeof value>;
70
108
  }
71
109
 
110
+ /**
111
+ * 取得預設的覆蓋率忽略路徑模式
112
+ * Get default coverage path ignore patterns
113
+ *
114
+ * 定義哪些路徑的檔案不納入覆蓋率統計
115
+ * Defines which paths are excluded from coverage statistics
116
+ *
117
+ * @returns {string[]} 覆蓋率忽略路徑模式陣列 / Array of coverage ignore patterns
118
+ */
72
119
  export function defaultCoveragePathIgnorePatterns()
73
120
  {
74
121
  const value = [
@@ -88,6 +135,15 @@ export function defaultCoveragePathIgnorePatterns()
88
135
  return value as ITSToWriteableArray<typeof value>;
89
136
  }
90
137
 
138
+ /**
139
+ * 取得預設的測試路徑忽略模式
140
+ * Get default test path ignore patterns
141
+ *
142
+ * 定義哪些路徑的檔案不會被視為測試檔案
143
+ * Defines which paths are excluded from test file recognition
144
+ *
145
+ * @returns {string[]} 測試路徑忽略模式陣列 / Array of test path ignore patterns
146
+ */
91
147
  export function defaultTestPathIgnorePatterns()
92
148
  {
93
149
  const value = [
@@ -103,47 +159,3 @@ export function defaultTestPathIgnorePatterns()
103
159
  return value as ITSToWriteableArray<typeof value>;
104
160
  }
105
161
 
106
- export function defaultTransform(runtime: IRuntime)
107
- {
108
- const paths: string[] = [
109
- requireResolveExtra('@bluelovers/jest-config').result,
110
- ].filter(Boolean);
111
-
112
- const opts: IOptions = {
113
- includeGlobal: true,
114
- includeCurrentDirectory: true,
115
- paths,
116
- };
117
-
118
- let ts_transform: InitialOptionsTsJest["transform"][string] = _requireResolve('ts-jest') as 'ts-jest';
119
-
120
- ts_transform = [ts_transform, defaultTsJestTransformerOptions(runtime)];
121
-
122
- const { result: tsd } = requireResolveExtra('jest-tsd-transform', opts);
123
-
124
- if (tsd?.length)
125
- {
126
- const { result: chain } = requireResolveExtra('jest-chain-transform', opts);
127
-
128
- if (chain?.length)
129
- {
130
- ts_transform = [
131
- chain as 'jest-chain-transform', {
132
- transformers: [
133
- tsd as 'jest-tsd-transform',
134
- // @ts-ignore
135
- ts_transform as 'ts-jest',
136
- ],
137
- },
138
- ] satisfies [
139
- string,
140
- Record<string, unknown>
141
- ]
142
- }
143
- }
144
-
145
- const value = {
146
- [`.(${_handleFileExtensions(defaultTransformFileExtensions(), '|')})$`]: ts_transform,
147
- } as const
148
- return value as ITSWriteable<typeof value>;
149
- }
package/src/helper.ts CHANGED
@@ -1,36 +1,87 @@
1
+ /**
2
+ * 輔助函數模組 - 提供 Jest 配置處理的通用工具函數
3
+ * Helper Functions Module - Provides utility functions for Jest configuration processing
4
+ */
5
+
1
6
  import { requireResolveCore, requireResolveExtra } from '@yarn-tool/require-resolve';
2
- import { InitialOptionsTsJest } from 'ts-jest';
3
7
  import { ITSValueOrArrayMaybeReadonly } from 'ts-type/lib/type/base';
4
8
  import { console } from 'debug-color2';
5
9
  import { IJestConfig } from './types';
6
10
  import { defaultTestFileExtensions } from './defaults';
7
11
  import { array_unique } from 'array-hyper-unique';
8
12
 
13
+ /**
14
+ * 解析模組路徑的內部輔助函數
15
+ * Internal helper function for resolving module paths
16
+ *
17
+ * 優先從 @bluelovers/tsdx 或 tsdx 套件路徑中搜尋,若失敗則使用標準 require.resolve
18
+ * Searches from @bluelovers/tsdx or tsdx package paths first, falls back to standard require.resolve
19
+ *
20
+ * @param {string} name - 要解析的模組名稱 / Module name to resolve
21
+ * @returns {string} 解析後的模組絕對路徑 / Resolved absolute module path
22
+ */
9
23
  export function _requireResolve(name: string)
10
24
  {
25
+ /**
26
+ * 搜尋路徑列表,包含 tsdx 相關套件路徑
27
+ * Search paths list including tsdx related package paths
28
+ */
11
29
  const paths = [
12
30
  requireResolveExtra('@bluelovers/tsdx').result,
13
31
  requireResolveExtra('tsdx').result,
14
32
  ].filter(Boolean);
15
33
 
34
+ /**
35
+ * 使用增強的解析器進行模組解析
36
+ * Use enhanced resolver for module resolution
37
+ */
16
38
  const result = requireResolveCore(name, {
17
39
  includeGlobal: true,
18
40
  includeCurrentDirectory: true,
19
41
  paths,
20
42
  });
21
43
 
44
+ // 輸出除錯資訊 / Output debug info
22
45
  console.debug('[require.resolve]', name, '=>', result)
23
46
 
24
47
  return result
25
48
  }
26
49
 
27
- export function makeTestRegexConfig<T extends string>(testExt: ITSValueOrArrayMaybeReadonly<T>): Pick<InitialOptionsTsJest, 'testMatch' | 'testRegex'>
50
+ export function _requireResolve2(name: string)
51
+ {
52
+ return requireResolveExtra(name, {
53
+ includeGlobal: true,
54
+ includeCurrentDirectory: true,
55
+ })
56
+ }
57
+
58
+ /**
59
+ * 建立測試正則表達式配置
60
+ * Create test regex configuration
61
+ *
62
+ * 根據測試檔案副檔名生成 testMatch 和 testRegex 配置
63
+ * Generates testMatch and testRegex configs based on test file extensions
64
+ *
65
+ * @param {T} testExt - 測試檔案副檔名或陣列 / Test file extension(s)
66
+ * @returns {object} 包含 testMatch 和 testRegex 的配置物件 / Object containing testMatch and testRegex
67
+ */
68
+ export function makeTestRegexConfig<T extends string>(testExt: ITSValueOrArrayMaybeReadonly<T>): Pick<IJestConfig, 'testMatch' | 'testRegex'>
28
69
  {
70
+ // 若未提供副檔名則使用預設值 / Use default if no extensions provided
29
71
  testExt ??= defaultTestFileExtensions() as any as T[];
30
72
  const _testExt = _handleFileExtensions(testExt, '|')
31
73
 
32
74
  return {
75
+ /** 不使用 testMatch(使用 testRegex 替代) / Not using testMatch (using testRegex instead) */
33
76
  testMatch: null as undefined,
77
+ /**
78
+ * 測試檔案正則表達式模式陣列
79
+ * Test file regex patterns array
80
+ *
81
+ * 匹配以下模式:
82
+ * - *.test.ts, *.spec.ts, *.tests.ts
83
+ * - __tests__ 目錄下的測試檔案
84
+ */
34
85
  testRegex: [
35
86
  `\\.(tests?|spec)\\.(${_testExt})$`,
36
87
  `__tests__\/\.*\\.(tests?|spec)\\.(${_testExt})$`,
@@ -38,34 +89,80 @@ export function makeTestRegexConfig<T extends string>(testExt: ITSValueOrArrayMa
38
89
  }
39
90
  }
40
91
 
92
+ /**
93
+ * 處理檔案副檔名陣列的核心函數
94
+ * Core function for processing file extension arrays
95
+ *
96
+ * 將輸入轉換為唯一的副檔名陣列
97
+ * Converts input to unique array of extensions
98
+ *
99
+ * @param {T} testExt - 輸入的副檔名(可為陣列或單一值) / Input extension(s)
100
+ * @returns {T[]} 唯一的副檔名陣列 / Unique array of extensions
101
+ */
41
102
  export function _handleFileExtensionsCore<T extends string>(testExt: ITSValueOrArrayMaybeReadonly<T>)
42
103
  {
43
104
  return array_unique([testExt].flat()) as T[]
44
105
  }
45
106
 
107
+ /**
108
+ * 處理檔案副檔名並以指定分隔符連接
109
+ * Process file extensions and join with specified separator
110
+ *
111
+ * @param {T} testExt - 輸入的副檔名(可為陣列或單一值) / Input extension(s)
112
+ * @param {string} sep - 分隔符號 / Separator string
113
+ * @returns {string} 連接後的副檔名字串 / Joined extension string
114
+ */
46
115
  export function _handleFileExtensions<T extends string>(testExt: ITSValueOrArrayMaybeReadonly<T>, sep: string)
47
116
  {
48
117
  return _handleFileExtensionsCore(testExt).join(sep)
49
118
  }
50
119
 
120
+ /**
121
+ * 修復 Jest 配置中的相容性問題
122
+ * Fix compatibility issues in Jest configuration
123
+ *
124
+ * 處理 testMatch/testRegex 互斥問題以及 testURL 已棄用選項的遷移
125
+ * Handles testMatch/testRegex mutual exclusivity and testURL deprecated option migration
126
+ *
127
+ * @param {T} jestConfig - 原始 Jest 配置 / Original Jest configuration
128
+ * @returns {T} 修復後的 Jest 配置 / Fixed Jest configuration
129
+ */
51
130
  export function fixJestConfig<T extends IJestConfig>(jestConfig: T): T
52
131
  {
132
+ /**
133
+ * testMatch 和 testRegex 是互斥的選項
134
+ * testMatch and testRegex are mutually exclusive options
135
+ *
136
+ * 如果同時設定兩者會造成衝突,因此需要清除其中一個
137
+ * Having both set causes conflicts, so we need to clear one
138
+ */
53
139
  if (jestConfig.testMatch)
54
140
  {
141
+ // 如果使用 testMatch,清除 testRegex
142
+ // If using testMatch, clear testRegex
55
143
  jestConfig.testRegex = null;
56
144
  }
57
145
  else if (jestConfig.testRegex)
58
146
  {
147
+ // 如果使用 testRegex,清除 testMatch
148
+ // If using testRegex, clear testMatch
59
149
  jestConfig.testMatch = null;
60
150
  }
61
151
 
62
152
  /**
63
- * Option "testURL" was replaced by passing the URL via "testEnvironmentOptions.url".
153
+ * testURL 選項已被 testEnvironmentOptions.url 取代
154
+ * testURL option has been replaced by testEnvironmentOptions.url
155
+ *
156
+ * @see https://jestjs.io/docs/configuration#testurl-string
64
157
  */
65
158
  if (jestConfig.testURL)
66
159
  {
160
+ // 確保 testEnvironmentOptions 物件存在
161
+ // Ensure testEnvironmentOptions object exists
67
162
  jestConfig.testEnvironmentOptions ??= {};
68
163
 
164
+ // 將 testURL 遷移到 testEnvironmentOptions.url
165
+ // Migrate testURL to testEnvironmentOptions.url
69
166
  jestConfig.testURL = (jestConfig.testEnvironmentOptions['url'] ??= jestConfig.testURL) as string
70
167
  }
71
168
 
package/src/index.ts CHANGED
@@ -1,70 +1,127 @@
1
+ /**
2
+ * 核心入口模組 - 提供 Jest 配置混合功能
3
+ * Core Entry Module - Provides Jest configuration mixing functionality
4
+ */
5
+
1
6
  import { _requireResolve, fixJestConfig, makeTestRegexConfig } from './helper';
2
7
  import {
3
8
  defaultCoveragePathIgnorePatterns,
4
9
  defaultModuleFileExtensions,
5
10
  defaultTestFileExtensions,
6
- defaultTestPathIgnorePatterns, defaultTransform,
11
+ defaultTestPathIgnorePatterns,
12
+
7
13
  } from './defaults';
8
14
  import { IOptionsPrintJestConfigInfo, printJestConfigInfo } from './print';
9
15
  import { IJestConfig } from './types';
10
16
  import { getJestCacheDirectory } from 'jest-cache-directory';
17
+ import { defaultTransform } from './default-transform';
18
+ import { defaultSetupFiles } from './default-setup-fles';
11
19
 
20
+ // 重新匯出輔助函數和類型 / Re-export helper functions and types
12
21
  export * from './helper';
13
22
  export * from './defaults';
14
23
  export * from './print';
24
+ export { defaultTransform } from './default-transform';
25
+ export { defaultSetupFiles } from './default-setup-fles';
15
26
 
27
+ /** Jest 快取目錄路徑 / Jest cache directory path */
16
28
  const cacheDirectory = getJestCacheDirectory();
17
29
 
18
30
  export { cacheDirectory }
19
31
 
32
+ /**
33
+ * 混合 Jest 配置函數 - 將預設配置與使用者自定義配置合併
34
+ * Mix Jest configuration function - Merges default config with user custom config
35
+ *
36
+ * 此函數提供了一個完整的 Jest 配置模板,包含 TypeScript 支援、快取管理、覆蓋率收集等功能
37
+ * This function provides a complete Jest configuration template with TypeScript support,
38
+ * cache management, coverage collection, and more.
39
+ *
40
+ * @param {T} jestConfig - 使用者自定義的 Jest 配置 / User custom Jest configuration
41
+ * @param {boolean} autoPrint - 是否自動印出配置資訊 / Whether to auto print config info
42
+ * @param {IOptionsPrintJestConfigInfo} options - 印出配置的選項 / Options for printing config
43
+ * @returns {T} 合併後的完整 Jest 配置 / Merged complete Jest configuration
44
+ */
20
45
  export function mixinJestConfig<T extends IJestConfig>(jestConfig?: T, autoPrint?: boolean,
21
46
  options?: IOptionsPrintJestConfigInfo)
22
47
  {
23
48
  // @ts-ignore
49
+ // 如果未提供配置則使用空對象 / Use empty object if no config provided
24
50
  jestConfig ??= {};
51
+
52
+ /**
53
+ * 建立基礎 Jest 配置物件
54
+ * Create base Jest configuration object
55
+ *
56
+ * 包含以下預設設定:
57
+ * - 快取目錄管理 / Cache directory management
58
+ * - 單一工作執行緒以確保測試穩定性 / Single worker for test stability
59
+ * - 自動清除模擬物件 / Auto clear mocks
60
+ * - 無測試時仍通過 / Pass with no tests
61
+ * - TypeScript 檔案支援 / TypeScript file support
62
+ * - 覆蓋率使用 v8 提供者 / Coverage using v8 provider
63
+ */
25
64
  const newJestConfig = fixJestConfig({
26
65
  globals: {
66
+ // ts-jest 全域配置預留位置 / ts-jest global config placeholder
27
67
  // 'ts-jest': {
28
68
  // //tsconfig: 'tsconfig.spec.json',
29
69
  // },
30
70
  },
71
+ /** 快取目錄路徑 / Cache directory path */
31
72
  cacheDirectory,
73
+ /** 最大工作執行緒數(設為 1 確保測試順序執行) / Max workers (set to 1 for sequential execution) */
32
74
  maxWorkers: 1,
75
+ /** 每次測試前清除模擬物件 / Clear mocks before each test */
33
76
  clearMocks: true,
77
+ /** 無測試檔案時仍視為通過 / Pass even when no test files */
34
78
  passWithNoTests: true,
79
+ /** 模組檔案副檔名列表 / Module file extensions list */
35
80
  moduleFileExtensions: defaultModuleFileExtensions(),
81
+ // 測試環境設定(預設 node) / Test environment setting
36
82
  //testEnvironment: 'node',
83
+ // 測試匹配模式 / Test match patterns
37
84
  //testMatch: ['**/*.test.ts', '**/*.spec.ts'],
85
+ // 合併測試正則配置 / Merge test regex config
38
86
  ...makeTestRegexConfig(defaultTestFileExtensions()),
87
+ /** 測試路徑忽略模式 / Test path ignore patterns */
39
88
  testPathIgnorePatterns: defaultTestPathIgnorePatterns(),
89
+ // 測試執行器設定 / Test runner setting
40
90
  //testRunner: 'jest-circus/runner',
41
- setupFilesAfterEnv: [
42
- //"jest-chain",
43
- //"jest-extended/all",
44
- //"jest-extended-extra",
45
- //"jest-num-close-with",
46
- /**
47
- * https://medium.com/doctolib/how-to-run-the-same-jest-test-suite-across-several-platforms-jest-os-detection-plugin-included-f8113832482b
48
- * https://github.com/doctolib/jest-os-detection
49
- */
50
- //'jest-os-detection',
51
- ],
91
+ /** 環境設置後載入的檔案 / Files to load after environment setup */
92
+ ...defaultSetupFiles(),
93
+ // 轉換器設定(後面單獨處理) / Transform setting (handled separately below)
52
94
  //transform: defaultTransform(),
95
+ /** 詳細輸出模式 / Verbose output mode */
53
96
  verbose: true,
54
97
  /**
55
- * if didn't set `coverageProvider` to `v8`
56
- * with `collectCoverage` `true`, nodejs debug point maybe will fail
98
+ * 覆蓋率提供者設定為 v8
99
+ * Coverage provider set to v8
100
+ *
101
+ * 若使用 collectCoverage: true 時未設為 v8,Node.js 除錯點可能會失效
102
+ * If not set to v8 with collectCoverage: true, Node.js debug points may fail
57
103
  */
58
104
  coverageProvider: 'v8',
105
+ /** 是否收集覆蓋率 / Whether to collect coverage */
59
106
  collectCoverage: false,
107
+ /** 覆蓋率忽略路徑模式 / Coverage path ignore patterns */
60
108
  coveragePathIgnorePatterns: defaultCoveragePathIgnorePatterns(),
61
109
  /**
62
- * https://github.com/facebook/jest/issues/9771#issuecomment-872764344
110
+ * Jest 模組解析器參考 / Jest module resolver reference
111
+ * @see https://github.com/facebook/jest/issues/9771#issuecomment-872764344
63
112
  */
64
113
  //resolver: 'jest-node-exports-resolver',
114
+ // 合併使用者自定義配置 / Merge user custom config
65
115
  ...jestConfig,
66
116
  });
67
117
 
118
+ /**
119
+ * 設定轉換器(如果尚未設定)
120
+ * Set up transform (if not already set)
121
+ *
122
+ * 使用預設的 ts-jest 轉換器,並支援 jest-tsd-transform 和 jest-chain-transform 鏈式轉換
123
+ * Uses default ts-jest transformer with support for jest-tsd-transform and jest-chain-transform chaining
124
+ */
68
125
  newJestConfig.transform ??= defaultTransform({
69
126
  jestConfig,
70
127
  autoPrint,
@@ -72,9 +129,14 @@ export function mixinJestConfig<T extends IJestConfig>(jestConfig?: T, autoPrint
72
129
  newJestConfig,
73
130
  });
74
131
 
132
+ // 如果啟用自動印出,則顯示配置資訊 / If autoPrint enabled, display config info
75
133
  autoPrint && printJestConfigInfo(newJestConfig, options);
76
134
 
77
135
  return newJestConfig
78
136
  }
79
137
 
138
+ /**
139
+ * 預設匯出 - mixinJestConfig 函數
140
+ * Default export - mixinJestConfig function
141
+ */
80
142
  export default mixinJestConfig
@@ -1,23 +1,103 @@
1
+ /**
2
+ * ts-jest 轉換器配置模組 - 提供 ts-jest 的預設選項設定
3
+ * ts-jest Transformer Configuration Module - Provides default options for ts-jest
4
+ */
5
+
1
6
  import { TsJestTransformerOptions } from 'ts-jest';
2
7
  import { IRuntime } from '../types';
8
+ import { array_unique } from 'array-hyper-unique';
3
9
 
10
+ /**
11
+ * 取得預設的 ts-jest 轉換器選項
12
+ * Get default ts-jest transformer options
13
+ *
14
+ * 此函數會合併使用者自定義的 ts-jest 設定與預設值,確保測試環境的最佳配置
15
+ * This function merges user custom ts-jest settings with defaults for optimal test environment
16
+ *
17
+ * 主要調整項目:
18
+ * - 禁用輸出檔案(noEmit: true)
19
+ * - 允許未使用的參數和標籤(用於測試開發)
20
+ * - 禁用嚴格的型別檢查選項(確保測試能順利執行)
21
+ *
22
+ * Key adjustments:
23
+ * - Disable file output (noEmit: true)
24
+ * - Allow unused parameters and labels (for test development)
25
+ * - Disable strict type checking options (ensure tests run smoothly)
26
+ *
27
+ * @param runtime - 執行時期配置,包含原始 Jest 配置 / Runtime configuration containing original Jest config
28
+ * @returns ts-jest 轉換器選項 / ts-jest transformer options
29
+ */
4
30
  export function defaultTsJestTransformerOptions(runtime: IRuntime)
5
31
  {
6
- const old: TsJestTransformerOptions = runtime.jestConfig.globals?.['ts-jest'] ?? {};
32
+ /**
33
+ * 取得使用者自定義的 ts-jest 設定(如果存在)
34
+ * Get user custom ts-jest settings (if exists)
35
+ *
36
+ * 從 jestConfig.globals['ts-jest'] 中讀取既有設定
37
+ * Reads existing settings from jestConfig.globals['ts-jest']
38
+ */
39
+ const old: TsJestTransformerOptions = (runtime.jestConfig.globals?.['ts-jest'] ?? {}) as TsJestTransformerOptions;
7
40
 
41
+ /**
42
+ * 處理 tsconfig 設定物件
43
+ * Process tsconfig settings object
44
+ *
45
+ * 如果使用者提供了物件形式的 tsconfig,則使用它;否則建立空物件
46
+ * Uses user-provided object tsconfig or creates empty object
47
+ */
8
48
  const tsconfig = typeof old.tsconfig === 'object' ? old.tsconfig : {};
9
49
 
50
+ const types = array_unique([...(tsconfig.types ?? []), 'jest']);
51
+
52
+ /**
53
+ * 回傳合併後的 ts-jest 選項
54
+ * Return merged ts-jest options
55
+ *
56
+ * 預設值優先於使用者設定,但使用者設定可覆蓋預設值
57
+ * Defaults take precedence but user settings can override them
58
+ */
10
59
  return {
60
+ // 保留既有設定 / Preserve existing settings
11
61
  ...old,
12
62
  tsconfig: {
63
+ /**
64
+ * 不輸出編譯後的檔案(僅用於型別檢查和轉換)
65
+ * Don't emit compiled files (only for type checking and transformation)
66
+ */
13
67
  noEmit: true,
68
+ /**
69
+ * 不僅輸出宣告檔案(確保完整編譯)
70
+ * Don't emit declaration only (ensure full compilation)
71
+ */
14
72
  emitDeclarationOnly: false,
73
+ /**
74
+ * 允許未使用的參數(測試中常見佔位符)
75
+ * Allow unused parameters (common placeholders in tests)
76
+ */
15
77
  noUnusedParameters: false,
78
+ /**
79
+ * 允許未使用的標籤(方便測試標記)
80
+ * Allow unused labels (convenient for test marking)
81
+ */
16
82
  allowUnusedLabels: true,
83
+ /**
84
+ * 允許未使用的區域變數(測試開發彈性)
85
+ * Allow unused locals (flexibility in test development)
86
+ */
17
87
  noUnusedLocals: false,
88
+ /**
89
+ * 允許從索引簽名進行屬性存取(簡化測試語法)
90
+ * Allow property access from index signature (simpler test syntax)
91
+ */
18
92
  noPropertyAccessFromIndexSignature: false,
93
+ /**
94
+ * 允許隱含的 any 型別(減少測試中的型別宣告負擔)
95
+ * Allow implicit any type (reduce type declaration overhead in tests)
96
+ */
19
97
  noImplicitAny: false,
98
+ // 合併使用者自定義的 tsconfig 設定 / Merge user custom tsconfig settings
20
99
  ...tsconfig,
100
+ types,
21
101
  },
22
- } satisfies TsJestTransformerOptions
102
+ } as TsJestTransformerOptions
23
103
  }