Skip to content

Commit 1027c00

Browse files
jaypatrickCopilot
andauthored
Fix TypeScript build and test errors for ES modules (#142)
* fix: resolve TypeScript build and test errors for ES modules - Convert jest.config.js from CommonJS to ES module format - Add cross-env for cross-platform NODE_OPTIONS support - Import jest globals from @jest/globals in test files - Replace require.main check with import.meta.url for ES modules - Remove unnecessary type assertions flagged by ESLint All 101 tests passing, build and lint successful. * Update src/rules-compiler-typescript/package.json Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: Jayson Knight <jayson.knight@jaysonknight.com> --------- Signed-off-by: Jayson Knight <jayson.knight@jaysonknight.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent def301c commit 1027c00

6 files changed

Lines changed: 50 additions & 184 deletions

File tree

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
/** @type {import('jest').Config} */
2-
module.exports = {
3-
preset: 'ts-jest',
2+
export default {
3+
preset: 'ts-jest/presets/default-esm',
44
testEnvironment: 'node',
5+
extensionsToTreatAsEsm: ['.ts'],
56
roots: ['<rootDir>/src'],
67
testMatch: ['**/*.test.ts'],
78
collectCoverageFrom: ['src/**/*.ts', '!src/**/*.test.ts', '!src/index.ts'],
89
coverageDirectory: 'coverage',
910
coverageReporters: ['text', 'lcov'],
11+
moduleNameMapper: {
12+
'^(\\.{1,2}/.*)\\.js$': '$1',
13+
},
1014
transform: {
1115
'^.+\\.tsx?$': ['ts-jest', {
1216
tsconfig: 'tsconfig.test.json',
17+
useESM: true,
1318
}],
1419
},
1520
};

src/rules-compiler-typescript/package-lock.json

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

src/rules-compiler-typescript/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
"compile:yaml": "node --import tsx src/cli.ts -c compiler-config.yaml",
2323
"compile:toml": "node --import tsx src/cli.ts -c compiler-config.toml",
2424
"lint": "eslint src/",
25-
"test": "jest",
26-
"test:coverage": "jest --coverage",
25+
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest",
26+
"test:coverage": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --coverage",
2727
"clean": "rm -rf dist",
2828
"bun:build": "bun build src/cli.ts --outdir dist --target node",
2929
"bun:start": "bun dist/cli.js",
@@ -47,6 +47,7 @@
4747
"devDependencies": {
4848
"@types/jest": "^29.5.14",
4949
"@types/node": "^22.15.0",
50+
"cross-env": "^7.0.3",
5051
"eslint": "^9.28.0",
5152
"jest": "^29.7.0",
5253
"ts-jest": "^29.4.0",

src/rules-compiler-typescript/src/cli.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ export async function main(args: string[] = process.argv.slice(2)): Promise<numb
369369
}
370370
}
371371

372-
// Run if executed directly
373-
if (require.main === module) {
372+
// Run if executed directly (ES module check)
373+
if (import.meta.url === `file://${process.argv[1]}`) {
374374
void main().then((code) => process.exit(code));
375375
}

0 commit comments

Comments
 (0)