-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathjest-utils.ts
More file actions
168 lines (154 loc) · 4.39 KB
/
jest-utils.ts
File metadata and controls
168 lines (154 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* Shared Jest configuration utilities for @swc/jest-based projects
* This file contains common configuration helpers used across the monorepo
* for projects that use @swc/jest as their transformer.
*
* Note: apps/frontend uses ts-jest and should NOT import from this file.
*/
// Helper to convert TypeScript paths to Jest moduleNameMapper format
export function pathsToModuleNameMapper(
paths: Record<string, string[]>,
prefix: string,
) {
const moduleNameMapper: Record<string, string> = {};
for (const [key, [value]] of Object.entries(paths)) {
// Convert TS path pattern to Jest regex pattern
const regexKey = '^' + key.replace(/\*/g, '(.*)') + '$';
// Replace each * with its corresponding capture group ($1, $2, etc.)
let groupIndex = 0;
const mappedValue = prefix + value.replace(/\*/g, () => `$${++groupIndex}`);
moduleNameMapper[regexKey] = mappedValue;
}
return moduleNameMapper;
}
/**
* Standard SWC Jest transformer configuration for TypeScript projects
*/
export const swcTransformConfig = {
jsc: {
parser: { syntax: 'typescript' as const, tsx: false },
target: 'es2022' as const,
},
};
/**
* SWC Jest transformer configuration with additional options for better compatibility
*/
export const swcTransformConfigWithDefineFields = {
jsc: {
parser: { syntax: 'typescript' as const, tsx: false },
target: 'es2022' as const,
transform: {
// Enable better module mocking compatibility
useDefineForClassFields: false,
},
},
module: {
// Use commonjs for better Jest compatibility with mocks
type: 'commonjs' as const,
},
};
/**
* SWC Jest transformer configuration with decorator support (without extra transforms)
*/
export const swcTransformConfigWithDecoratorsOnly = {
jsc: {
parser: {
syntax: 'typescript' as const,
tsx: false,
decorators: true, // Enable decorators
},
target: 'es2022' as const,
transform: {
legacyDecorator: true, // Use legacy decorator implementation
decoratorMetadata: true, // Enable decorator metadata
},
},
};
/**
* SWC Jest transformer configuration for NestJS projects with decorator support
*/
export const swcTransformConfigWithDecorators = {
jsc: {
parser: {
syntax: 'typescript' as const,
tsx: false,
decorators: true, // Enable decorators for NestJS
},
target: 'es2022' as const,
transform: {
legacyDecorator: true, // Use legacy decorator implementation
decoratorMetadata: true, // Enable decorator metadata (needed for NestJS DI)
useDefineForClassFields: false,
},
},
module: {
type: 'commonjs' as const,
},
};
/**
* Standard transform configuration for @swc/jest
*/
export const swcTransform = {
'^.+\\.[tj]s$': ['@swc/jest', swcTransformConfig],
};
/**
* Transform configuration for @swc/jest with better compatibility
*/
export const swcTransformWithDefineFields = {
'^.+\\.[tj]s$': ['@swc/jest', swcTransformConfigWithDefineFields],
};
/**
* Transform configuration for @swc/jest with decorators only
*/
export const swcTransformWithDecoratorsOnly = {
'^.+\\.[tj]s$': ['@swc/jest', swcTransformConfigWithDecoratorsOnly],
};
/**
* Transform configuration for @swc/jest with NestJS decorator support
*/
export const swcTransformWithDecorators = {
'^.+\\.[tj]s$': ['@swc/jest', swcTransformConfigWithDecorators],
};
/**
* Standard transformIgnorePatterns for node packages
*/
export const standardTransformIgnorePatterns = [
'/node_modules/(?!slug).+\\.js$',
];
/**
* Standard moduleFileExtensions for TypeScript projects
*/
export const standardModuleFileExtensions = ['ts', 'js', 'html'];
/**
* SWC Jest transformer configuration for React/TSX projects
*/
export const swcTransformConfigTsx = {
jsc: {
parser: {
syntax: 'typescript' as const,
tsx: true,
decorators: false,
},
transform: {
react: {
runtime: 'automatic',
},
},
target: 'es2022' as const,
},
module: {
type: 'commonjs' as const,
},
};
/**
* Transform configuration for @swc/jest with TSX support (React components)
*/
export const swcTransformTsx = {
'^.+\\.[tj]sx?$': ['@swc/jest', swcTransformConfigTsx],
};
/**
* Transform ignore patterns for frontend with Chakra UI, framer-motion, etc.
*/
export const frontendTransformIgnorePatterns = [
'/node_modules/(?!(slug|marked|@chakra-ui|@zag-js|framer-motion|lucide-react)).+\\.[tj]sx?$',
];