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
[4mSystem:[0m OS: Windows 10 CPU: x64 IntelĀ® Coreā¢ i7-4790 CPU @ 3.60GHz [4mBinaries:[0m 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:
- Created 5 years ago
- Reactions:2
- Comments:8
Top GitHub Comments
Yes, that was it! āmoduleNameMapperā: { ā^@books(.*)ā: ā<rootDir>/example-app/app/books/$1ā },
did the trickā¦
thanks to thymikee and simenb and everybody elseā¦
@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 jestmoduleNameMapper
look like in order to be able to resolve these paths?