Skip to content

Commit 1c820c9

Browse files
Copilotshati-patel
andcommitted
Fix ESM compatibility with @actions v3 packages using module mapping
Co-authored-by: shati-patel <42641846+shati-patel@users.noreply.github.com>
1 parent d16d687 commit 1c820c9

File tree

4 files changed

+315
-7
lines changed

4 files changed

+315
-7
lines changed

jest-resolver.cjs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Custom Jest resolver to handle ESM packages from @actions
3+
*/
4+
5+
module.exports = (request, options) => {
6+
// Use Jest's default resolver
7+
return options.defaultResolver(request, {
8+
...options,
9+
// Add package condition for node environment
10+
packageFilter: (pkg) => {
11+
// For ESM packages that have conditional exports, ensure we resolve correctly
12+
if (pkg.exports && typeof pkg.exports === 'object') {
13+
// If the package has exports, ensure we're using the right conditions
14+
const conditions = options.conditions || [];
15+
if (!conditions.includes('node')) {
16+
options.conditions = [...conditions, 'node', 'import', 'require', 'default'];
17+
}
18+
}
19+
return pkg;
20+
},
21+
});
22+
};

jest.config.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ const config: Config = {
8383
// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
8484
moduleNameMapper: {
8585
"^(\\.{1,2}/.*)\\.js$": "$1",
86+
"^@actions/core$": "<rootDir>/node_modules/@actions/core/lib/core.js",
87+
"^@actions/io$": "<rootDir>/node_modules/@actions/io/lib/io.js",
88+
"^@actions/io/lib/(.*)$": "<rootDir>/node_modules/@actions/io/lib/$1",
89+
"^@actions/exec$": "<rootDir>/node_modules/@actions/exec/lib/exec.js",
90+
"^@actions/exec/lib/(.*)$": "<rootDir>/node_modules/@actions/exec/lib/$1",
91+
"^@actions/http-client$": "<rootDir>/node_modules/@actions/http-client/lib/index.js",
92+
"^@actions/http-client/lib/(.*)$": "<rootDir>/node_modules/@actions/http-client/lib/$1",
93+
"^@actions/core/lib/(.*)$": "<rootDir>/node_modules/@actions/core/node_modules/@actions/http-client/lib/$1",
8694
},
8795

8896
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
@@ -95,14 +103,14 @@ const config: Config = {
95103
// notifyMode: "failure-change",
96104

97105
// A preset that is used as a base for Jest's configuration
98-
preset: "ts-jest/presets/default-esm",
99-
100-
// An array of file extensions your modules use
101-
extensionsToTreatAsEsm: [".ts"],
106+
preset: "ts-jest",
102107

103108
// Run tests from one or more projects
104109
// projects: undefined,
105110

111+
// A path to a custom resolver
112+
resolver: "<rootDir>/jest-resolver.cjs",
113+
106114
// Use this configuration option to add custom reporters to Jest
107115
// reporters: undefined,
108116

@@ -179,10 +187,20 @@ const config: Config = {
179187
},
180188
],
181189
"^.+\\.(js|jsx|mjs|cjs)$": [
182-
"babel-jest",
190+
"@swc/jest",
183191
{
184-
presets: ["@babel/preset-env"],
185-
plugins: ["@babel/plugin-transform-modules-commonjs"],
192+
jsc: {
193+
parser: {
194+
syntax: "ecmascript",
195+
},
196+
transform: {
197+
useDefineForClassFields: false,
198+
},
199+
target: "es2022",
200+
},
201+
module: {
202+
type: "commonjs",
203+
},
186204
},
187205
],
188206
},

package-lock.json

Lines changed: 266 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
"@eslint/eslintrc": "^3.3.3",
4242
"@eslint/js": "^9.39.2",
4343
"@octokit/types": "^16.0.0",
44+
"@swc/core": "^1.15.11",
45+
"@swc/jest": "^0.2.39",
4446
"@types/archiver": "^7.0.0",
4547
"@types/jest": "^30.0.0",
4648
"@types/node": "^25.2.0",

0 commit comments

Comments
 (0)