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

后端 未结 4 1563
别那么骄傲
别那么骄傲 2021-01-20 06:43

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:

相关标签:
4条回答
  • 2021-01-20 06:48

    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.

    0 讨论(0)
  • 2021-01-20 06:49

    Add this in polifills.ts file

     import 'core-js/es7/reflect';
    
    0 讨论(0)
  • 2021-01-20 06:55

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

    import "core-js/features/reflect";

    0 讨论(0)
  • 2021-01-20 07:07

    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);
    
    0 讨论(0)
提交回复
热议问题