ng-mocks: ng-mocks is not in JIT mode and cannot resolve declarations

ng-mocks is not in JIT mode and cannot resolve declarations

I’m getting this error after did some refactoring on moving some code to external libraries. What does this error mean and how to fix it?

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 33 (13 by maintainers)

Commits related to this issue

Most upvoted comments

I have experienced the same issue. For testing I created a new angular 13 project, installed ng-mocks, changed the generated app-component test to use ng-mocks (MockBuilder and MockRender) and it has the same issue.

I have this error after upgrading to Angular 13. I have the latest version of ng-mocks installed (I even deleted node_modules) and re-installed. “ng-mocks”: “^12.5.0”, I focused one test that IS NOT USING ng-mocks and I STILL get this error?

import { ComponentFixture, TestBed } from '@angular/core/testing';
import { MatButtonModule } from '@angular/material/button';
import { MatDialogModule, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { CommonPipesModule } from '@common/pipes/common-pipes.module';
import { ConfirmationDialogComponent } from '../confirmation-dialog/confirmation-dialog.component';
import { AlertDialogComponent } from './alert-dialog.component';

fdescribe('AlertDialogComponent', () => {
	let component: AlertDialogComponent;
	let fixture: ComponentFixture<AlertDialogComponent>;
	let resultHandler;

	const setupTestBed = async (dialogDataValue: {
		title?: string;
		contentHtml?: string;
		resultHandler: jasmine.Spy;
	}) => {
		await TestBed.configureTestingModule({
			imports: [NoopAnimationsModule, MatDialogModule, MatButtonModule, CommonPipesModule],
			declarations: [AlertDialogComponent],
			providers: [
				{
					provide: MatDialogRef,
					useValue: {
						close: (dialogResult: any) => {}
					}
				},
				{
					provide: MAT_DIALOG_DATA,
					useValue: dialogDataValue
				}
			]
		}).compileComponents();
	};

	describe(' - Defaults', () => {
		beforeEach(async () => {
			resultHandler = jasmine.createSpy();
			await setupTestBed({ resultHandler });
		});

		beforeEach(() => {
			fixture = TestBed.createComponent(AlertDialogComponent);
			component = fixture.componentInstance;
			fixture.detectChanges();
		});

		it('should create', () => {
			// expectations...
		});
	});
});
Error: AlertDialogComponent declaration has been passed into ng-mocks without Angular decorators. Therefore, it cannot be properly handled. Highly likely,  ng-mocks is not in JIT mode. Otherwise, please create an issue on github: https://github.com/ike18t/ng-mocks/issues/new?title=False%20positive%20ng-mocks%20not%20in%20JIT. Thank you in advance for support.
            at node_modules/ng-mocks/cjs/lib/common/error.missing-decorators.js:5:1
            at exports.default (node_modules/ng-mocks/cjs/lib/common/core.reflect.body-catch.js:16:1)
            at node_modules/ng-mocks/cjs/lib/common/core.reflect.directive-resolve.js:11:113
            at node_modules/ng-mocks/cjs/lib/common/core.reflect.meta.js:11:1
            at generateTouches (node_modules/ng-mocks/cjs/lib/common/ng-mocks-global-overrides.js:136:1)
            at defineTouches (node_modules/ng-mocks/cjs/lib/common/ng-mocks-global-overrides.js:169:1)
            at Function.<anonymous> (node_modules/ng-mocks/cjs/lib/common/ng-mocks-global-overrides.js:257:1)
            at Function.<anonymous> (node_modules/ng-mocks/cjs/lib/mock-helper/mock-helper.faster-install.js:43:1)
            at src/app/common/alert-dialog/alert-dialog.component.spec.ts:19:9
            at Generator.next (<anonymous>)

@satanTime can you reopen this please? any updates? Same error here with Angular 13.

I had this error because I’ve accidentally added a pipe as a MockComponents argument

If you are here because of the error in your tests:

  • try to update ng-mocks to the latest, it works with all Angular versions
  • are you using jest.mock('lib/path')? ng-mocks@12.3.1 should have a solution for it, please drop a comment here if it doesn’t work for you

I hope it helps. Thanks in advance!

Hi all, this is the issue to follow: https://github.com/ike18t/ng-mocks/issues/1427

Hi @rbirkgit,

Thanks for the info. I’ll comment in a different issue. This one is correct. For angular 13, you need ng-mocks 13.

I got this error after upgrading my project from angular @12.2.16 to @13.2.4. However I was getting the exact same error:

MyComponent declaration has been passed into ng-mocks without Angular decorators. Therefore, it cannot be properly handled. Highly likely, jest.mock() has been used on its file, or ng-mocks is not in JIT mode

My project versions:

After trying a lot of possible solutions a could solve my problem just reinstalling ng-mocks with the latest version and everything got fixed up and all tests were passing successfully.

Update ng-mocks to the latest version with:

$ npm uninstall ng-mocks
$ npm install ng-mocks@latest --save-dev

Good lucky, hope this help someone else.

Same issue here. We do use @ngneat/spectator though but I did a rewrite on one Test to only use TestBed and got the same result. Since @vorant94 already shared a repo that reproduces the error, I’ll not try to extract ours (though I could if that would help you further in any way).

@satanTime Actually I got this error because the component I tried to mock was imported from a wrong directory. So probably this error also falls on attempts to mock undefined.