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.

Typescript Path not recognized in Jest Angular

See original GitHub issue

## šŸ› Bug Report

versions jest ^23.1.0 types/jest ^22.2.23 jest-preset-angular ^5.2.2 jest-zone-patch ^0.0.8

Angular application Windows 10 computer VS code 1.23.1

Typescript 2.7.2

Context:

Typescript allows the use of paths in the tsconfig.json file such as

"baseUrl": "./src", 
"paths": {
  "@app/*": ["app/*"]
}

So the imports can be done this way: import { component } from ā€œAT app/componentā€ (note: AT is the at sign that I canā€™t insert here)

instead of import { Franchisee } from ā€œā€¦/ā€¦/ā€¦/componentā€

which makes for a more readable, easier to maintain code.

Bug: Jest fails any test using paths with this message:

Cannot find module ā€˜AT app/componentā€™ from ā€˜component.spec.tsā€™ at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:210:17) at Object.<anonymous> (src/app/component/component.spec.ts:11:20)

(note: AT is the at sign that I canā€™t insert here)

To Reproduce

Steps to reproduce the behavior: Configure tsconfig.ts with a path. In that path, change the import { component } from ā€˜./componentā€™

to import { component } from ā€˜AT app/componentā€™ (Same note, at symbol doesnā€™t get printed)

and welcome to the danceā€¦

Expected behavior

Expected Jest to understand pathsā€¦

I have a ngrx platform example app using jest and also using paths and they work fine, but they refer to librarires (@ngrx/effects instead of ā€¦/ā€¦/ngrx/effects) for example.

Link to repl or repo

The repository https://github.com/mdelgadov/ngrx-platform the tsconfig.ts at the root directory has a path ATbooks for example-app/app/books the file example-app/app/books/containers/collection-page.component.spec.ts has ATbooks instead of ā€¦/ this is just an example for testing purposes.

Run yarn example:test and will receive the error

Cannot find module ā€˜@books/components/book-preview-list.componentā€™ from ā€˜collection-page.component.spec.tsā€™

npx envinfo --preset jest

Paste the results here: C:\Users\mdelg\AppData\Roaming\npm-cache_npx\35952\node_modules\envinfo\dist\cli.js

System: OS: Windows 10 CPU: x64 IntelĀ® Coreā„¢ i7-4790 CPU @ 3.60GHz Binaries: Yarn: 1.6.0 - C:\Program Files (x86)\Yarn\bin\yarn.CMD npm: 5.6.0 - C:\Program Files (x86)\nodejs\npm.CMD


Issue Analytics

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

github_iconTop GitHub Comments

6reactions
mdelgadovcommented, Jun 2, 2018

Yes, that was it! ā€œmoduleNameMapperā€: { ā€œ^@books(.*)ā€: ā€œ<rootDir>/example-app/app/books/$1ā€ },

did the trickā€¦

thanks to thymikee and simenb and everybody elseā€¦

2reactions
latchezar-kostov-ascendlearning-comcommented, Aug 24, 2018

@mdelgadov I am running into the same issue and my struggling to come up with a solution.

"paths": { "@myscope/*": [ "libs/*/src", ] }

The lib/<name>/index.ts has all of the exports. What should the jest moduleNameMapper look like in order to be able to resolve these paths?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest is not recognized by typescript - Stack Overflow
I think I have corrupted my npm modules because I tried to install some more recent version of jest and ts-jest related modules...
Read more >
Configuring Jest
If the file specified by path is not found, an error is returned. For example, with the following configuration: JavaScript; TypeScript.
Read more >
Configuring Module Resolution On Typescript and Jest
Run git checkout step-2 . Setting the baseUrl and paths specifies to the Typescript transpiler how to resolve modules. baseUrl: Specifies where to...
Read more >
Paths mapping | ts-jest - GitHub Pages
ts-jest provides a helper to transform the mapping from tsconfig to Jest config format, but it needs the . js version of the...
Read more >
TypeScript - Cypress Documentation
You may have to restart your IDE's TypeScript server if the setup above does not appear to work. For example: VS Code (within...
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