Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • C create-react-app
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 1,547
    • Issues 1,547
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 417
    • Merge requests 417
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Meta
  • create-react-app
  • Issues
  • #7818
Closed
Open
Issue created Oct 13, 2019 by Administrator@rootContributor

Absolute imports with CRA

Created by: adrianwithah

I created a Typescript-enabled React app bootstrapped from create-react-app. After enabling baseUrl: "." for easier absolute imports, my Jest tests broke.

How to repro:

  1. With a src folder in your root directory, add baseUrl: "." to your tsconfig.json under compilerOptions.
  2. Create a Jest test file with the following code:
import { isValidAddress } from 'orbit-db';

it('doesnt fail to resolve import', () => {
    isValidAddress("asdf");
});
  1. Run npm install orbit-db ipfs and then npm run test. You should end up with something similar to the following error:
 FAIL  src/components/Random.test.tsx
  ● Test suite failed to run

    Configuration error:

    Could not locate module multicodec/src/base-table mapped as:
    path/to/my/repo/src/base-table.

    Please check your configuration for these entries:
    {
      "moduleNameMapper": {
        "/src\/(.*)$/": "path/to/my/repo/src/$1"
      },
      "resolver": null
    }

      at createNoMappedModuleFoundError (node_modules/jest-resolve/build/index.js:501:17)
      at Object.<anonymous> (node_modules/cids/src/index.js:6:16)

According to my understand of CRA, the moduleNameMapper regex is generated when the CRA script sees the baseUrl field in tsconfig.json. However, this regex also seems to wrongly capture some of the imports in files from node_modules.

Assignee
Assign to
Time tracking