3

I just migrated an application module to be an importable library.

I'm trying to make the tests work correctly, just as they worked before, but I get this error:

Error: Can't resolve all parameters for ApplicationModule: (?).
at syntaxError (webpack:///C:/Repositories/MyProject/my-library/node_modules/@angular/compiler/fesm5/compiler.js?:1275:17)
at CompileMetadataResolver._getDependenciesMetadata (webpack:///C:/Repositories/MyProject/my-library/node_modules/@angular/compiler/fesm5/compiler.js?:11176:35)
at CompileMetadataResolver._getTypeMetadata (webpack:///C:/Repositories/MyProject/my-library/node_modules/@angular/compiler/fesm5/compiler.js?:11069:26)
at CompileMetadataResolver.getNgModuleMetadata (webpack:///C:/Repositories/MyProject/my-library/node_modules/@angular/compiler/fesm5/compiler.js?:10937:24)
at CompileMetadataResolver.getNgModuleSummary (webpack:///C:/Repositories/MyProject/my-library/node_modules/@angular/compiler/fesm5/compiler.js?:10747:35)
at eval (webpack:///C:/Repositories/MyProject/my-library/node_modules/@angular/compiler/fesm5/compiler.js?:10861:51)
at Array.forEach (<anonymous>)
at CompileMetadataResolver.getNgModuleMetadata (webpack:///C:/Repositories/MyProject/my-library/node_modules/@angular/compiler/fesm5/compiler.js?:10849:49)
at CompileMetadataResolver.getNgModuleSummary (webpack:///C:/Repositories/MyProject/my-library/node_modules/@angular/compiler/fesm5/compiler.js?:10747:35)
at eval (webpack:///C:/Repositories/MyProject/my-library/node_modules/@angular/compiler/fesm5/compiler.js?:10861:51)

I've seen these possible solutions: Error: Can't resolve all parameters for ApplicationModule: (?) and Cannot resolve parameters for ApplicationModule: (?) but they cannot fix my problem.

projects/my-library/src/test.ts

// This file is required by karma.conf.js and loads recursively all the .spec and framework files

import { getTestBed } from '@angular/core/testing';
import {
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting
} from '@angular/platform-browser-dynamic/testing';
import 'core-js/es7/reflect';
import 'zone.js/dist/zone';
import 'zone.js/dist/zone-testing';

declare const require: any;

// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting()
);
// Then we find all the tests.
const context = require.context('./', true, /\.spec\.ts$/);
// And load the modules.
context.keys().map(context);

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

Alavaros
  • 1,404
  • 6
  • 28
  • 47

4 Answers4

6

At last, I found where the problem was, I had a hard time finding it because it was not related. The problem comes from this tslint configuration:

tslint.json:

...
"ordered-imports" [
  true,
  { "named-imports-order": "lowercase-last" }
]
...

Even if the configuration is only "ordered-imports": true it was causing a problem in test.ts file when ng lint is launched, so I changed that file to fulfill the tslint requirement:

test.ts:

// This file is required by karma.conf.js and loads recursively all the .spec and framework files

import { getTestBed } from '@angular/core/testing';
import {
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting
} from '@angular/platform-browser-dynamic/testing';
import 'core-js/es7/reflect'; // <-- Moved from the top of imports
import 'zone.js/dist/zone'; // <-- Moved from the top of imports
import 'zone.js/dist/zone-testing'; // <-- Moved from the top of imports

declare const require: any;

// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting()
);
// Then we find all the tests.
const context = require.context('./', true, /\.spec\.ts$/);
// And load the modules.
context.keys().map(context);

And this file was causing the error I was getting. This file must look like this:

Correct test.ts:

import 'core-js/es7/reflect';
import 'zone.js/dist/zone';
import 'zone.js/dist/zone-testing';
import { getTestBed } from '@angular/core/testing';
import {
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting
} from '@angular/platform-browser-dynamic/testing';

declare const require: any;

// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting()
);
// Then we find all the tests.
const context = require.context('./', true, /\.spec\.ts$/);
// And load the modules.
context.keys().map(context);
Alavaros
  • 1,404
  • 6
  • 28
  • 47
1

For angular 7 and higher, try adding in polyfill.ts file :

import "core-js/features/reflect";

  • 2
    A little more explanation would help a lot to make this a more useful answer - and would be more likely to get yours upvoted – Ruben Helsloot Feb 24 '21 at 07:42
0

You have a dependency in your app module that you did not provide or import in any way. You can determine which by looking at your ApplicationModule constructor method - it likely has some service or similar injected, and you didn't provide it there in imports array of NgModule metadata.

Zlatko
  • 16,414
  • 12
  • 60
  • 116
0

Add this in polifills.ts file

 import 'core-js/es7/reflect';
shanthan
  • 409
  • 6
  • 7
  • 2
    A little more explanation would help a lot to make this a more useful answer - and would be more likely to get yours upvoted – Ruben Helsloot Feb 24 '21 at 07:42