无法解析ApplicationModule的所有参数:(?)

时间:2018-12-24 12:38:53

标签: angular testing karma-jasmine

我刚刚将应用程序模块迁移为可导入库。

我正尝试使测试正常工作,就像以前一样,但是出现此错误:

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)

我已经看到了以下可能的解决方案: Error: Can't resolve all parameters for ApplicationModule: (?)Cannot resolve parameters for ApplicationModule: (?),但它们不能解决我的问题。

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);

让我知道您是否需要更多信息来找出问题所在。

4 个答案:

答案 0 :(得分:2)

最后,我找到了问题所在,但很难找到它,因为它不相关。问题来自此tslint配置:

tslint.json

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

即使配置仅为"ordered-imports": true,在启动test.ts时,也会在ng lint文件中引起问题,所以我更改了该文件以满足tslint要求:

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);

此文件导致了我得到的错误。该文件必须如下所示:

更正 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);

答案 1 :(得分:0)

您的应用程序模块中有一个依赖项,您没有以任何方式提供或导入。您可以通过查看您的ApplicationModule构造函数方法来确定哪个方法-它可能注入了某些服务或类似的服务,并且您没有在imports元数据的NgModule数组中提供该服务。

答案 2 :(得分:0)

将此添加到polifills.ts文件中

Layer

答案 3 :(得分:0)

对于Angle 7及更高版本,请尝试添加polyfill.ts文件:

reddit.submission(id=LoadedPosts.pop(0))