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:
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.
Add this in polifills.ts file
import 'core-js/es7/reflect';
For angular 7 and higher, try adding in polyfill.ts file :
import "core-js/features/reflect";
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);