Skip to content

Fix: Preserve moduleNameMapper from TypeScript Jest configs in monorepos#2010

Open
mohammedahmed18 wants to merge 1 commit intomainfrom
fix/typescript-modulemapper-monorepo
Open

Fix: Preserve moduleNameMapper from TypeScript Jest configs in monorepos#2010
mohammedahmed18 wants to merge 1 commit intomainfrom
fix/typescript-modulemapper-monorepo

Conversation

@mohammedahmed18
Copy link
Copy Markdown
Contributor

Problem

When Jest configs are TypeScript files (.ts), Codeflash's runtime config creation skipped them (cannot require() TypeScript directly) and created a standalone config WITHOUT moduleNameMapper. This caused monorepo workspace packages like @budibase/backend-core to fail with "Cannot find module" errors.

Root Cause

In test_runner.py, _create_runtime_jest_config() had two branches:

  1. If base config is .js/.cjs: extends it with require()
  2. If base config is .ts: creates standalone config (no moduleNameMapper)

The standalone config only set: roots, testMatch, moduleDirectories, globalSetup/globalTeardown. It did NOT include moduleNameMapper, which is critical for monorepo workspace package resolution.

Solution

  1. Added _extract_module_name_mapper_from_ts_config() helper that uses ts-node to load TypeScript configs and extract moduleNameMapper
  2. Modified standalone config generation to call this helper and serialize moduleNameMapper into the runtime config
  3. Falls back gracefully if extraction fails (logs debug message)

Impact

  • Severity: HIGH - Systematic failure on ALL TypeScript monorepo projects
  • Scope: 100% reproducible (not transient)
  • Affected: budibase, any project with workspace packages like @workspace/*
  • Trace IDs: 0130fa38-9ede-4c1f-9369-b54c31f7e938 and 40+ others in budibase logs

Testing

Files Changed

  • codeflash/languages/javascript/test_runner.py (+154 lines: helper function + config generation logic)
  • tests/test_languages/test_typescript_jest_config_modulemapper.py (+122 lines: new test file)
  • tests/test_languages/test_javascript_test_runner.py (updated test expectations)

Example

Before (missing moduleNameMapper):

module.exports = {
  roots: ["/workspace/target/packages/server", ...],
  testMatch: ['**/*.test.ts', ...],
  globalSetup: undefined,
  globalTeardown: undefined,
};

After (with moduleNameMapper):

module.exports = {
  roots: ["/workspace/target/packages/server", ...],
  testMatch: ['**/*.test.ts', ...],
  moduleNameMapper: {
    "@budibase/backend-core/(.*)": "<rootDir>/../backend-core/$1",
    "@budibase/backend-core": "<rootDir>/../backend-core/src",
    "@budibase/shared-core": "<rootDir>/../shared-core/src",
  },
  globalSetup: undefined,
  globalTeardown: undefined,
};

Co-authored-by: Claude Sonnet 4.5 noreply@anthropic.com

**Problem:**
When Jest configs are TypeScript files (.ts), Codeflash's runtime config
creation skipped them (cannot require() TypeScript directly) and created
a standalone config WITHOUT moduleNameMapper. This caused monorepo workspace
packages like @budibase/backend-core to fail with "Cannot find module" errors.

**Root Cause:**
In test_runner.py, _create_runtime_jest_config() had two branches:
1. If base config is .js/.cjs: extends it with require()
2. If base config is .ts: creates standalone config (no moduleNameMapper)

The standalone config only set: roots, testMatch, moduleDirectories,
globalSetup/globalTeardown. It did NOT include moduleNameMapper.

**Fix:**
1. Added _extract_module_name_mapper_from_ts_config() helper that uses
   ts-node to load TypeScript configs and extract moduleNameMapper
2. Modified standalone config generation to call this helper and serialize
   moduleNameMapper into the runtime config
3. Falls back gracefully if extraction fails (logs debug message)

**Impact:**
- Fixes ALL TypeScript monorepo projects (100% systematic)
- Examples: budibase, any project with @workspace/* packages
- Trace IDs affected: 0130fa38-9ede-4c1f-9369-b54c31f7e938 and 40+ others

**Testing:**
- Added test_typescript_jest_config_modulemapper.py with 2 new tests
- Updated test_javascript_test_runner.py test expectations (PR #2009 changed
  behavior to always create runtime configs)
- All 36 tests pass
- No linting/type errors (uv run prek passes)

**Files Changed:**
- codeflash/languages/javascript/test_runner.py (main fix)
- tests/test_languages/test_typescript_jest_config_modulemapper.py (new tests)
- tests/test_languages/test_javascript_test_runner.py (updated expectations)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
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.

1 participant