0

I am trying to run my tests and I get this error while testing nx(mono-repo) angular test using Jest

Can't resolve all parameters for ApplicationModule: (?).

      at syntaxError (../packages/compiler/src/util.ts:100:17)
      at CompileMetadataResolver._getDependenciesMetadata ......  

login-form.componenet.spec.ts

import { LoginFormComponent } from './login-form.component';
import { ComponentFixture, async, TestBed } from '@angular/core/testing';
import { FormBuilder, ReactiveFormsModule, FormsModule } from '@angular/forms';
import { CommonModule } from '@angular/common';

import {
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting
} from '@angular/platform-browser-dynamic/testing';

describe('Testing LoginForm', () => {
  let component: LoginFormComponent;
  let fixture: ComponentFixture<LoginFormComponent>;

  // create new instance of FormBuilder
  const formBuilder: FormBuilder = new FormBuilder();

  beforeEach(() => {

    TestBed.configureTestingModule({
      declarations: [LoginFormComponent],
      imports: [FormsModule, ReactiveFormsModule, CommonModule],
      providers: [{ provide: FormBuilder, useValue: formBuilder }]
    });

    fixture = TestBed.createComponent(LoginFormComponent);
    component = fixture.componentInstance;
  });

  test('should be created', () => {
    expect(component).toBeTruthy();
  });
});

test.setup.ts

import 'jest-preset-angular';

jest.config.ts

module.exports = {
  testMatch: ['**/+(*.)+(spec|test).+(ts|js)?(x)'],
  transform: {
    '^.+\\.(ts|js|html)$': 'ts-jest'
  },
  resolver: '@nrwl/jest/plugins/resolver',
  moduleFileExtensions: ['ts', 'js', 'html'],
  coverageReporters: ['html'],
  moduleNameMapper: {
    '@neural/(.*)': '<rootDir>/libs/$1/src/index.ts',    
  }
};

Versions

Angular: 8.2.14
jest: 26.0.1
nrwl: 8.8.2
jest-preset-angular: 7.1.1

constructor parameters of Login-form Login-form.component.ts

constructor(private fb: FormBuilder) {
    this.loginForm = this.fb.group({
      email: ['', Validators.compose([Validators.required, Validators.email])],
      password: ['', Validators.compose([Validators.required])]
    });
  }

Let me know if you need more information to figure out where is the problem.

pjpscriv
  • 866
  • 11
  • 20
  • Required for more information: Version of `Angular` / `nrwl` / `jest` / `jest-preset-angular`, content of referenced `index.ts` (refercenced by `import from '..'`), constructor parameters of `LoginFormComponent`. Also: why do you import `reflect` polyfill? Why do you use `reset`- / `initTestEnvironment`? – wtho Jun 23 '20 at 23:36
  • Edited, Thank you. – Arshad Shaikh Jun 24 '20 at 04:49
  • Can you also share the content of your `index.ts`? Maybe try referencing the `LoginFormComponent`-file directly when importing `LoginFormComponent`. – wtho Jun 25 '20 at 11:25
  • Updated, I tried importing directly and then referenced it, still same error, Thanks. – Arshad Shaikh Jun 26 '20 at 04:24
  • I cannot find anything unusual in the files provided. I even setup a project with your versions and your login component and the tests run flawlessly. I guess you have to dig deeper on your own. Maybe try to start with a fresh project as well and compare the differences. – wtho Jun 26 '20 at 09:46
  • Is it a Plain Angular Project? cause this works fine in aan Angular Project but, does not work in an NX. – Arshad Shaikh Jun 29 '20 at 03:19
  • Yeah, see here: https://github.com/wtho/jest-62527713-repro – wtho Jun 30 '20 at 11:19
  • I had Jest Cli installed globally and I was trying with Jest command, I removed jest cli and used angular cli command it then worked for me. – Arshad Shaikh Jul 07 '20 at 04:26
  • please formulate an answer explaining how you solved your problem – wtho Jul 07 '20 at 09:41

0 Answers0