Angular 如何预防‘;放大验证器’;“不是已知元素”;使用Amplify UI组件时单元测试出错
我用的是v10。在对我的应用程序进行单元测试时,我收到以下错误:Angular 如何预防‘;放大验证器’;“不是已知元素”;使用Amplify UI组件时单元测试出错,angular,karma-jasmine,aws-amplify,Angular,Karma Jasmine,Aws Amplify,我用的是v10。在对我的应用程序进行单元测试时,我收到以下错误: ERROR: ‘amplify-authenticator’ is not a known element 为了防止此错误,我将在组件的测试配置中导入AmplifyUIAngularModule: import { AmplifyUIAngularModule } from '@aws-amplify/ui-angular'; beforeEach(async () => { await TestBed.conf
ERROR: ‘amplify-authenticator’ is not a known element
为了防止此错误,我将在组件的测试配置中导入AmplifyUIAngularModule:
import { AmplifyUIAngularModule } from '@aws-amplify/ui-angular';
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [
AmplifyUIAngularModule
]
})
})
这会解决第一个错误,但会触发另一个错误:
Error: Amplify has not been configured correctly.
The configuration object is missing required auth properties.
Did you run amplify push after adding auth via amplify add auth?
See https://aws-amplify.github.io/docs/js/authentication#amplify-project-setup for more information
很明显,它告诉我,如果没有配置,AmplificUIAngularModule是无法初始化的。我试图添加它,尽管感觉不对,因为组件不应该在单元测试中连接,而是应该被模拟:
Amplify.configure({
Auth: {
....
},
});
这确实解决了错误,但当karma/jasmine尝试连接到无头chrome进行测试时,会出现超时。我怀疑这些东西确实在尝试连接,这不可能是单元测试的方式
关于如何模拟AWS/Amplify调用,有各种各样的线程,但我找不到任何关于如何防止缺少Amplify验证器依赖性的内容。正如正确指出的那样,模拟整个组件工作得很好:
@Component({
selector: 'amplify-authenticator',
template: '',
})
class AmplifyAuthenticatorMock {}
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [
AmplifyAuthenticatorMock,
AppComponent,
],
}).compileComponents();
});
如果您不需要任何功能,可以模拟组件