Component is not resolved ... Did you run and wait for 'resolveComponentResources()'?

See original GitHub issue

๐Ÿ› Bug Report

To Reproduce

  • Install the default workspace with a single Angular application using npx create-nx-workspace@latest command.
  • Updating packages:
"jest": "27.0.4",
"jest-preset-angular": "9.0.3",
"ts-jest": "27.0.3"
  • Updating the root jest.config.js to:
require('jest-preset-angular/ngcc-jest-processor');

module.exports = {
  projects: ['<rootDir>/apps/test'],
};
  • Updating the application jest.config.js to:
module.exports = {
  displayName: 'test',
  preset: '../../jest.preset.js',
  setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],
  globals: {
    'ts-jest': {
      tsconfig: '<rootDir>/tsconfig.spec.json',
      stringifyContentPathRegex: '\\.(html|svg)$'
    }
  },
  coverageDirectory: '../../coverage/apps/test',
  snapshotSerializers: [
    'jest-preset-angular/build/serializers/no-ng-attributes',
    'jest-preset-angular/build/serializers/ng-snapshot',
    'jest-preset-angular/build/serializers/html-comment'
  ]
};

Updating "postinstall": "node ./decorate-angular-cli.js && ngcc" and repeating npm i to generate UMD packages.

Steps to reproduce the behavior:

Run npm run test.

Result:

Component 'AppComponent' is not resolved:
     - templateUrl: ./app.component.html
     - styleUrls: ["./app.component.scss"]
Did you run and wait for 'resolveComponentResources()'?

Expected behavior

Correct test processing as with default setting and versions of:

"jest": "26.2.2",
"jest-preset-angular": "8.4.0",
"ts-jest": "26.5.5"

Error log:

nx run test:test 
 FAIL   test  apps/test/src/app/app.component.spec.ts
  AppComponent
    ร— should create the app (10 ms)
    ร— should have as title 'test' (2 ms)
    ร— should render title (1 ms)

  โ— AppComponent โ€บ should create the app

    Component 'AppComponent' is not resolved:
     - templateUrl: ./app.component.html
     - styleUrls: ["./app.component.scss"]
    Did you run and wait for 'resolveComponentResources()'?

      at Function.get (../../../packages/core/src/render3/jit/directive.ts:82:17)
      at getComponentDef (../../../packages/core/src/render3/definition.ts:735:14)
      at verifyDeclarationsHaveDefinitions (../../../packages/core/src/render3/jit/module.ts:242:17)
          at Array.forEach (<anonymous>)
      at verifySemanticsOfNgModuleDef (../../../packages/core/src/render3/jit/module.ts:212:16)
      at Function.get (../../../packages/core/src/render3/jit/module.ts:167:13)
      at R3TestBedCompiler.Object.<anonymous>.R3TestBedCompiler.applyProviderOverridesToModule (../../../packages/core/testing/src/r3_test_bed_compiler.ts:414:49)
      at R3TestBedCompiler.Object.<anonymous>.R3TestBedCompiler.compileTestModule (../../../packages/core/testing/src/r3_test_bed_compiler.ts:677:10)
      at R3TestBedCompiler.Object.<anonymous>.R3TestBedCompiler.finalize (../../../packages/core/testing/src/r3_test_bed_compiler.ts:245:10)
      at TestBedRender3.get (../../../packages/core/testing/src/r3_test_bed.ts:370:43)

envinfo

System:
    OS: Windows 10, NodeJS 14.17.0

Npm packages:
    jest: 27.0.4
    jest-preset-angular: 9.0.3
    typescript: 4.2.4

Issue Analytics

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

github_iconTop GitHub Comments

9reactions
ahnpnlcommented, Jun 8, 2021

this is because Nrwl Nx needs to have migration for Jest config. In root jest-preset.js,

const nxPreset = require('@nrwl/jest/preset');

module.exports = { ...nxPreset };

which refers to node_modules/@nrwl/jest/preset/jest-preset.js, the config should be

module.exports = {
    testMatch: ['**/+(*.)+(spec|test).+(ts|js)?(x)'],
    resolver: '@nrwl/jest/plugins/resolver',
    moduleFileExtensions: ['ts', 'js', 'html'],
    coverageReporters: ['html'],
  testEnvironment: 'jsdom', <=== Jest 27 default testEnvironment is node
    transform: {
        '^.+\\.(ts|js|html)$': 'jest-preset-angular', <=== breaking change in 9.0.0
    },
};

If you want to use Jest 27, you need to override root jest-preset.js with the config above or wait for Nrwl Nx team to release a new version.

5reactions
yharaskrikcommented, Jun 23, 2021

@mlc-mlapis do you mind sharing exactly what it was that fixed your issue? I just upgraded out Nx workspace to 12.4.0 (which has the migration that @ahnpnl mentions, ie. testEnvironment: 'jsdom' in the root) but I am getting the same error as you still. This is all the code is

import { async, TestBed } from '@angular/core/testing';
import { FundraiserPageEditorPublishDialogModule } from './fundraiser-page-editor-publish-dialog.module';

describe('FundraiserPageEditorPublishDialogModule', () => {
    beforeEach(async(() => {
        TestBed.configureTestingModule({
            imports: [FundraiserPageEditorPublishDialogModule],
        }).compileComponents();
    }));

    it('should create', () => {
        expect(FundraiserPageEditorPublishDialogModule).toBeDefined();
    });
});

jest-preset-angular: 9.0.3 jest: 27.0.3 ts-jest: 27.0.3

I tried to override test environment in the projects jest.config.js as well but no dice.

Read more comments on GitHub >

github_iconTop Results From Across the Web

testing angular components with jest gives ... - Stack Overflow
I'm trying to test Angular components with jest via jest-preset-angular. it can run the ... app.component.scss"] Did you run and wait for ......
Read more >
Update Jest to 27.x version - Itilin
The second error โ€“ Component is not resolved. This error, like the previous one, ... Did you run and wait for 'resolveComponentResources()'?
Read more >
Upgraded to Angular 12, SSR not working, cannot resolve ...
app.component.css"] Did you run and wait for 'resolveComponentResources()'? The error is thrown after ssr-build and ssr-serve are completeย ...
Read more >
Angular >=13 | jest-preset-angular - GitHub Pages
With Jest 28 and jest-preset-angular v12.0.0, ng-jest-resolver is no longer required to have in Jest config. This resolver is also excluded from our...
Read more >
jest-preset-angular - Bountysource
[Bug]: Component not resolved error when testing conditional templateUrl $ 0 ... After upgrading angular version to 14,when running unit testcases(npm run ......
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