Javascript 使用beforeAll而不是beforeach优化jest测试
我正在编写测试,以检查在子组件发出一些数据时是否调用父组件,下面是我如何设置组件进行测试:Javascript 使用beforeAll而不是beforeach优化jest测试,javascript,angular,jestjs,Javascript,Angular,Jestjs,我正在编写测试,以检查在子组件发出一些数据时是否调用父组件,下面是我如何设置组件进行测试: describe('OperationComponent', () => { let component: OperationComponent; let fixture: ComponentFixture<OperationComponent>; let store: Store; let de: DebugElement; let data: any; l
describe('OperationComponent', () => {
let component: OperationComponent;
let fixture: ComponentFixture<OperationComponent>;
let store: Store;
let de: DebugElement;
let data: any;
let cmp: any;
beforeEach(async(() => {
const {
imports,
providers
} = configureDefaultTestingMouduleForSecureComponent(OperationComponent);
TestBed.resetTestingModule();
TestBed.configureTestingModule({
declarations: [OperationComponent],
imports: [...imports, NgxsModule.forFeature([RoutesOperationState])],
providers: [...providers, RoutesOperationEntity]
}).compileComponents();
store = TestBed.get(Store);
}));
beforeEach(() => {
fixture = TestBed.createComponent(OperationComponent);
component = fixture.componentInstance;
de = fixture.debugElement;
data = de.query(By.directive(RouteOperationsComponent));
cmp = data.componentInstance;
component.routeId = mockRoutes[0].id;
});
describe('cancel action', () => {
it('should be called on cancelOperation when it emits data', () => {
jest.spyOn(component, 'handleCancelRouteOperation');
cmp.cancelRouteOperation.emit(true);
expect(component.handleCancelRouteOperation).toHaveBeenCalledWith(true);
});
});
请包括您得到的错误。@Erbsenkoenig我已经在我的问题描述中添加了错误消息。我猜看了之后,beforeAll块甚至在测试设置完成之前就执行了,因为它在beforeach中,并且它们在同一个descripe块中。您以前是否尝试过将其移动到“取消操作”描述块中?请尝试在stackblitz或类似事件中重现错误,否则很难帮助您解决问题。
expect(spy).toHaveBeenCalledWith(...expected)
Expected: true
Number of calls: 0