Skip to content

Jest tests fail when TypeScrip tsconfig baseUrl set to '.' and the project is nested in a src directory. moduleNameMapper error. #7833

Closed
@pbeshai

Description

@pbeshai

Describe the bug

When a project is nested somewhere in a directory named src, and the baseUrl in tsconfig.json is set to ".", all tests will fail with the following error:

 FAIL  src/App.test.tsx
  ● Test suite failed to run

    Configuration error:

    Could not locate module /Users/pbeshai/Workspace/bug-report/src/nested-src-bug-ts/node_modules/babel-preset-react-app/node_modules/@babel/runtime/helpers/interopRequireDefault mapped as:
    /Users/pbeshai/Workspace/bug-report/src/nested-src-bug-ts/src/nested-src-bug-ts/node_modules/babel-preset-react-app/node_modules/@babel/runtime/helpers/interopRequireDefault.

    Please check your configuration for these entries:
    {
      "moduleNameMapper": {
        "/src\/(.*)$/": "/Users/pbeshai/Workspace/bug-report/src/nested-src-bug-ts/src/$1"
      },
      "resolver": null
    }

After some investigation, I believe this is because the moduleNameMapper is not prefixed with ^ and so it matches the src earlier in the path.

Steps to reproduce

(Write your steps here:)

  1. npx create-react-app my-app --typescript
  2. Add baseUrl: "." to tsconfig.json
  3. yarn test

Reproducible demo

https://github.com/pbeshai/cra-nested-src-bug-ts

Proposed Solution

In #7755 baseUrl was allowed to be set to .. If we modify ... I now see this is already fixed in #7818 and #7822.

I'll post and close this just in case someone else is stumbling on this problem and couldn't find any results.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions