fix: resolve Cannot find module '@angular/core/testing' by auto-setting moduleResolution to Node16#3629
fix: resolve Cannot find module '@angular/core/testing' by auto-setting moduleResolution to Node16#3629
Cannot find module '@angular/core/testing' by auto-setting moduleResolution to Node16#3629Conversation
…or Angular subpath imports (AI-assisted) Co-authored-by: ahnpnl <27772165+ahnpnl@users.noreply.github.com>
Cannot find module '@angular/core/testing' — document required moduleResolution
|
@copilot I think changing doc is not sufficient enough. You need to add tests and reproduce the issue. After that, based on test result and try to come up with a fix |
…r subpath exports (AI-assisted) Co-authored-by: ahnpnl <27772165+ahnpnl@users.noreply.github.com>
Done in commit 950db98. Root cause confirmed: Angular v17+ exposes subpath exports ( Fix: In Tests added:
|
Cannot find module '@angular/core/testing' — document required moduleResolutionCannot find module '@angular/core/testing' by auto-setting moduleResolution to Node16
|
Angular v17+ exposes subpath exports (
/testing,/animations, etc.) viapackage.jsonexports. TypeScript only resolves these whenmoduleResolutionis"node16","nodenext", or"bundler"— the legacy"node"default (implicit when setting"module": "CommonJS"without an explicitmoduleResolution) silently fails to find them, causing TS2307 errors.Changes
Code Fix
src/config/ng-jest-config.ts: In_resolveTsConfig, automatically defaultmoduleResolutiontoNode16when it is not explicitly configured. This ensures Angular subpath imports (@angular/core/testing,@angular/platform-browser/animations, etc.) are always resolvable without requiring users to update their tsconfig.Tests
src/config/ng-jest-config.spec.ts: Added tests verifying thatmoduleResolutionis automatically set toNode16when not configured, and that an explicitly configured value is not overridden.src/compiler/ng-jest-compiler.spec.ts: Added regression test confirming thatimport { TestBed } from '@angular/core/testing'compiles without TS2307 errors.Documentation
docs+versioned_docs/version-16.x): Add explicitmoduleResolutionto the recommendedtsconfig.spec.jsonsnippets:"moduleResolution": "node16""moduleResolution": "bundler"docs+versioned_docs/version-16.x): Add a new entry explaining the root cause and the fix for the TS2307 error on Angular subpath imports.Original prompt
This section details on the original issue you should resolve
<issue_title>[Bug]: Cannot find module '@angular/core/testing' in Github Actions</issue_title>
<issue_description>### Version
16.0.0
Steps to reproduce
Unfortunately, the repo is for work (private) and it works fine locally.
Expected behavior
I expect tests to run.
Actual behavior
Tests fail with:
Additional context
// tsconfig.spec.json
{ "extends": "../../tsconfig.json", "compilerOptions": { "baseUrl": "./", "outDir": "../../out-tsc/spec", "types": [ "jest", "node" ], "target": "ESNext" }, "include": [ "src/**/*.spec.ts", "src/**/*.d.ts" ], "exclude": [] }// setup-jest.ts
// jest.config.js
// package.json
{ "name": "ngx-pdf-viewer-demo", "version": "1.143.3", "license": "MIT", "private": true, "scripts": { "ng": "ng", "start": "ng serve", "build": "ng build --configuration production", "build:lib": "ng build ngx-pdf-viewer --configuration production", "postbuild:lib": "cpy projects/ngx-pdf-viewer/src/assets/**/* dist/ngx-pdf-viewer/assets && cpy README.md dist/ngx-pdf-viewer", "test": "jest", "test:lib": "jest", "test:lib:ci": "... </details> <!-- START COPILOT CODING AGENT SUFFIX --> - Fixes thymikee/jest-preset-angular#3529 <!-- START COPILOT CODING AGENT TIPS --> --- ✨ Let Copilot coding agent [set things up for you](https://github.com/thymikee/jest-preset-angular/issues/new?title=✨+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot) — coding agent works faster and does higher quality work when set up for your repo.