Skip to content

fix: resolve Cannot find module '@angular/core/testing' by auto-setting moduleResolution to Node16#3629

Draft
Copilot wants to merge 3 commits intomainfrom
copilot/fix-angular-testing-module-error
Draft

fix: resolve Cannot find module '@angular/core/testing' by auto-setting moduleResolution to Node16#3629
Copilot wants to merge 3 commits intomainfrom
copilot/fix-angular-testing-module-error

Conversation

Copy link
Contributor

Copilot AI commented Mar 1, 2026

Angular v17+ exposes subpath exports (/testing, /animations, etc.) via package.json exports. TypeScript only resolves these when moduleResolution is "node16", "nodenext", or "bundler" — the legacy "node" default (implicit when setting "module": "CommonJS" without an explicit moduleResolution) silently fails to find them, causing TS2307 errors.

Changes

Code Fix

  • src/config/ng-jest-config.ts: In _resolveTsConfig, automatically default moduleResolution to Node16 when 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 that moduleResolution is automatically set to Node16 when not configured, and that an explicitly configured value is not overridden.
  • src/compiler/ng-jest-compiler.spec.ts: Added regression test confirming that import { TestBed } from '@angular/core/testing' compiles without TS2307 errors.

Documentation

  • Installation guide (docs + versioned_docs/version-16.x): Add explicit moduleResolution to the recommended tsconfig.spec.json snippets:
    • CJS: "moduleResolution": "node16"
    • ESM: "moduleResolution": "bundler"
  • Troubleshooting guide (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:

Run npm run test:lib:ci
> ngx-pdf-viewer-demo@1.143.3 test:lib:ci
> jest --watch=false --coverage
FAIL projects/ngx-pdf-viewer/src/components/toolbar.component.spec.ts
  ● Test suite failed to run
    projects/ngx-pdf-viewer/src/components/toolbar.component.spec.ts:2:60 - error TS2307: Cannot find module '@angular/core/testing' or its corresponding type declarations.
    2 import { TestBed, ComponentFixture, fakeAsync, tick } from '@angular/core/testing';
                                                                 ~~~~~~~~~~~~~~~~~~~~~~~
    projects/ngx-pdf-viewer/src/components/toolbar.component.spec.ts:12:41 - error TS2307: Cannot find module '@angular/platform-browser/animations' or its corresponding type declarations.
    12 import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
                                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
FAIL projects/ngx-pdf-viewer/src/components/pdf-viewer.component.spec.ts
  ● Test suite failed to run
    projects/ngx-pdf-viewer/src/components/pdf-viewer.component.spec.ts:4:41 - error TS2307: Cannot find module '@angular/platform-browser/animations' or its corresponding type declarations.
    4 import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
                                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    projects/ngx-pdf-viewer/src/components/pdf-viewer.component.spec.ts:5:43 - error TS2307: Cannot find module '@angular/core/testing' or its corresponding type declarations.
    5 import { ComponentFixture, TestBed } from '@angular/core/testing';
                                                ~~~~~~~~~~~~~~~~~~~~~~~
FAIL projects/ngx-pdf-viewer-demo/src/app/app.component.spec.ts
  ● Test suite failed to run
    projects/ngx-pdf-viewer-demo/src/app/app.component.spec.ts:1:25 - error TS2307: Cannot find module '@angular/core/testing' or its corresponding type declarations.
    1 import { TestBed } from '@angular/core/testing';
                              ~~~~~~~~~~~~~~~~~~~~~~~
    projects/ngx-pdf-viewer-demo/src/app/app.component.spec.ts:2:41 - error TS2307: Cannot find module '@angular/platform-browser/animations' or its corresponding type declarations.
    2 import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
                                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
FAIL projects/ngx-pdf-viewer/src/services/pdf.service.spec.ts
  ● Test suite failed to run
    projects/ngx-pdf-viewer/src/services/pdf.service.spec.ts:1:33 - error TS2307: Cannot find module '@angular/core/testing' or its corresponding type declarations.
    1 import { TestBed, inject } from '@angular/core/testing';

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

import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone';

setupZoneTestEnv();

// jest.config.js

module.exports = {
  preset: 'jest-preset-angular',
  setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'],
  moduleFileExtensions: ['ts', 'html', 'js', 'json', 'mjs'],
  transform: {
    '^.+\\.(ts|js|mjs)$': [
      'jest-preset-angular',
      {
        tsconfig: 'projects/ngx-pdf-viewer/tsconfig.spec.json'
      }
    ],
    '^.+\\.(html|svg)$': '<rootDir>/jest-html-transformer.js'
  },
  collectCoverageFrom: [
    'projects/ngx-pdf-viewer/src/**/*.{ts,js}',
    '!projects/ngx-pdf-viewer/src/**/*.d.ts'
  ],
  testMatch: [
    '<rootDir>/projects/ngx-pdf-viewer/src/**/*.spec.ts',
    '<rootDir>/projects/ngx-pdf-viewer-demo/src/**/*.spec.ts'
  ],
  transformIgnorePatterns: [
    'node_modules/(?!(.*\\.mjs$|lodash-es))'
  ],
  moduleNameMapper: {
    '\\.(css|less|scss)$': 'identity-obj-proxy'
  }
};

// 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.

…or Angular subpath imports (AI-assisted)

Co-authored-by: ahnpnl <27772165+ahnpnl@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix cannot find module '@angular/core/testing' in tests docs: fix Cannot find module '@angular/core/testing' — document required moduleResolution Mar 1, 2026
@ahnpnl
Copy link
Collaborator

ahnpnl commented Mar 2, 2026

@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>
Copy link
Contributor Author

Copilot AI commented Mar 2, 2026

@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

Done in commit 950db98.

Root cause confirmed: Angular v17+ exposes subpath exports (@angular/core/testing, @angular/platform-browser/animations, etc.) via the exports field in package.json. TypeScript only resolves exports when moduleResolution is Node16, NodeNext, or Bundler. Without an explicit moduleResolution, TypeScript defaults to the legacy Node10 (for module: CommonJS) or Classic, neither of which support exports — causing TS2307 errors.

Fix: In NgJestConfig._resolveTsConfig (src/config/ng-jest-config.ts), when moduleResolution is not explicitly configured, default it to Node16. This makes Angular subpath imports resolve correctly without requiring users to update their tsconfig.

Tests added:

  • ng-jest-config.spec.ts: verifies moduleResolution is automatically set to Node16 when not configured, and is NOT overridden when explicitly set
  • ng-jest-compiler.spec.ts: regression test that import { TestBed } from '@angular/core/testing' compiles without TS2307 errors

Copilot AI changed the title docs: fix Cannot find module '@angular/core/testing' — document required moduleResolution fix: resolve Cannot find module '@angular/core/testing' by auto-setting moduleResolution to Node16 Mar 2, 2026
@github-actions
Copy link

github-actions bot commented Mar 2, 2026

PR Preview Action v1.8.1

QR code for preview link

🚀 View preview at
https://thymikee.github.io/jest-preset-angular/pr-preview/pr-3629/

Built to branch gh-pages at 2026-03-02 08:27 UTC.
Preview will be ready when the GitHub Pages deployment is complete.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants