question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug]: Enum with path alias throws error: Could not locate module @foo/bar mapped as

See original GitHub issue

Version

27.0.7

Steps to reproduce

  1. Clone this repo: https://github.com/xyxc0673/test-ts-jest
  2. yarn
  3. yarn test

Expected behavior

all tests pass

Actual behavior

    Configuration error:
    
    Could not locate module @foo/bar mapped as:
    /Users/carl/workspace/tests/ts-jest/examples/react/foo/$1.
    
    Please check your configuration for these entries:
    {
      "moduleNameMapper": {
        "/^@foo\/(.*)$/": "/Users/carl/workspace/tests/ts-jest/examples/react/foo/$1"
      },
      "resolver": undefined
    }

    > 1 | import { BarInterface, BarEnum } from "@foo/bar"
        | ^
      2 |
      3 | export const testEnum = () => {
      4 |     const a: BarInterface = {

      at createNoMappedModuleFoundError (node_modules/.pnpm/jest-resolve@27.4.2/node_modules/jest-resolve/build/resolver.js:579:17)
      at Object.<anonymous> (src/test.ts:1:1)

Debug log

{"context":{"logLevel":20,"namespace":"Importer","package":"ts-jest","version":"27.0.7"},"message":"creating Importer singleton","sequence":1,"time":"2021-12-01T03:17:34.745Z"}
{"context":{"actualVersion":"27.4.2","expectedVersion":">=27.0.0 <28","logLevel":20,"namespace":"versions","package":"ts-jest","version":"27.0.7"},"message":"checking version of jest: OK","sequence":2,"time":"2021-12-01T03:17:34.751Z"}
{"context":{"logLevel":20,"namespace":"ts-jest-transformer","package":"ts-jest","version":"27.0.7"},"message":"created new transformer","sequence":3,"time":"2021-12-01T03:17:34.751Z"}
��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������{"context":{"logLevel":20,"namespace":"ts-jest-transformer","package":"ts-jest","version":"27.0.7"},"message":"created new transformer","sequence":6,"time":"2021-12-01T03:17:35.714Z"}

Additional context

No response

Environment

System:
    OS: macOS 12.0.1
    CPU: (10) arm64 Apple M1 Pro
  Binaries:
    Node: 16.13.0 - ~/.nvm/versions/node/v16.13.0/bin/node
    Yarn: 1.22.17 - /opt/homebrew/bin/yarn
    npm: 8.1.0 - ~/.nvm/versions/node/v16.13.0/bin/npm
  npmPackages:
    jest: ^27.3.1 => 27.4.2

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
ahnpnlcommented, Dec 11, 2021

Hi, I found your problem. The problem was actually informed by Jest that it cannot map the correct path.

As you can see the error message is

Please check your configuration for these entries:
    {
      "moduleNameMapper": {
        "/^@foo\/(.*)$/": "/Users/carl/workspace/tests/ts-jest/examples/react/foo/$1"
      },
      "resolver": undefined
}

the path /Users/carl/workspace/tests/ts-jest/examples/react/foo/$1 isn’t correct, it misses the src

Therefore, you need to change your config to

// jest.config.js
module.exports = {
   //...
   moduleNameMapper: {
      '^react-native$': 'react-native-web',
      '^.+\\.module\\.(css|sass|scss)$': 'identity-obj-proxy',
       ...pathsToModuleNameMapper(compilerOptions.paths, {
        prefix: '<rootDir>/src',
    }),
   }
}

When compiling, TypeScript strips away the interfaces or type aliases. However, enum is not the case as TypeScript will try to resolve that enum first before attempting to compile to js, therefore you get the error.

This is not an issue of ts-jest so i will close the issue.

0reactions
xyxc0673commented, Dec 1, 2021

It is not the case. I use the example from the repo and forget to delete the original code.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Build test with Jest fails with `Could not locate module @/ ...
I'm building an app with vuejs and testing with jest and running the tests locally works, but on circleci fails with the error...
Read more >
Jest Path Mapping Error: Could not locate module xyz ...
to my jest config will make it work. I needed to remove every import with leading @app/ . Pretty strange, all other aliases...
Read more >
TSConfig Reference - Docs on every TSConfig option
All relative paths found in the configuration file will be resolved relative to the ... This does not affect errors on the basis...
Read more >
Error Handling - The Rust Programming Language
Typically, one writes io::Result<T> , which makes it clear that you're using the io module's type alias instead of the plain definition from...
Read more >
3. Using GHCi — Glasgow Haskell Compiler 9.4.3 User's ...
This means that for most modules, the module name must match the filename. If it doesn't, GHCi won't be able to find it....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found