简体   繁体   中英

Uncaught (in promise): TypeError: Cannot read property 'replace' of undefined

Getting Uncaught (in promise): TypeError: Cannot read property 'replace' of undefined on fixture.detectChanges() method.

I am running test cases with the Jest and angular version 8 Note: my other test cases working fine so no configuration mistake

Test.spec.ts

let requestListModel= new InvoicesRequestsListModel();
const initialState: InvoicesState = {
requestModel: new InvoicesRequestModel(),
requestListModel: new InvoicesRequestsListModel()
};


const tab: InvoiceResponse[] = [{
totalRecords: 1,
recordsReturned: 1,
invoices:  [{
supplier: "Test Supplier",
invoiceId:"3fa85f64-5717-4562-b3fc-2c963f66af11",
}];

requestListModel.updateTableData(tab)

let invoiceStoreStub: Partial<InvoicesStore> = {
store: {
    requestModel: new InvoicesRequestModel(),
    requestListModel: requestListModel,
},
store$:  new BehaviorSubject<InvoicesState>(initialState).asObservable(),
setInvoiceState: jest.fn(),
setRequestListModel: jest.fn(),
setRequestModel: jest.fn(),
};

describe('TradeInvoicesComponent', () => {
let fixture: ComponentFixture<TradeInvoicesComponent>;
let component: TradeInvoicesComponent;
let stubInvoiceService: InvoicesStore;
let selectMenu: SelectMenuTestHelper;

beforeEach(async(() => {
    const configure: ConfigureFn = testBed => {
        testBed.configureTestingModule({
            declarations: [TradeInvoicesComponent],
            imports: [HttpClientTestingModule, SharedModule, MatTableModule, BrowserAnimationsModule],
            schemas: [NO_ERRORS_SCHEMA],
            providers: [MatSnackBar, { provide: InvoicesStore, useValue: invoiceStoreStub }]
        });
    };

    configureTests(configure).then(testBed => {
        fixture = testBed.createComponent(TradeInvoicesComponent);
        component = fixture.componentInstance;
        stubInvoiceService = testBed.get(InvoicesStore);
        fixture.detectChanges();
        component.dataModel.updateTableData(tab);
    });

}));

componat.ts

@Component({
selector: 'app-trade-invoices',
templateUrl: './trade-invoices.component.html',
styleUrls: ['./trade-invoices.component.scss']
})
export class TradeInvoicesComponent implements OnInit, AfterViewInit {
@ViewChild(MatPaginator, {static: false}) paginator: MatPaginator;
@ViewChild(MatSort, {static: false}) sort: MatSort;
@ViewChild('input', {static: false}) input: ElementRef;
@ViewChild(MatTable, {static: true}) table: MatTable<[]>;

@Output() paginate: EventEmitter<InvoicesRequestsListModel> = new EventEmitter();
@Output() request: EventEmitter<any> = new EventEmitter();
@Input() setSpinner: boolean;
public dataModel: InvoicesRequestsListModel = new InvoicesRequestsListModel();
    displayedColumns: string[] = ['status', 'invoiceId', 'supplier', 'modifieddate'];
public uiPageNumber = 0;

private invoice: Invoice;


constructor(public store: InvoicesStore,
                        private paymentConsumerService: PaymentConsumerService, private 
 _snackBar: MatSnackBar) {

}

ngOnInit() {
    this.dataModel.updateDataModel('pageNumber', this.apiPageNumber);
    this.paginate.emit(this.dataModel);
}

  ngAfterViewInit() {
    this.store.store$.subscribe((invoice: InvoicesState) => {
        this.table.renderRows();
    });
    this.sort.sortChange.subscribe(() => {
        const sortOrder = `${this.sort.active} ${this.sort.direction}`;
        this.changeSort(sortOrder);
    });
}

can anyone please suggest the changes and mistake.

LONG LOGS

Error: Uncaught (in promise): TypeError: Cannot read property 'replace' of undefined TypeError: Cannot read property 'replace' of undefined at Object.eval [as updateRenderer] (ng:///DynamicTestModule/TradeInvoicesComponent.ngfactory.js:104:47) at Object.debugUpdateRenderer [as updateRenderer] (/packages/core/src/view/services.ts:410:19) at checkAndUpdateView (/packages/core/src/view/view.ts:368:12) at callViewAction (/packages/core/src/view/view.ts:615:11) at execEmbeddedViewsAction (/packages/core/src/view/view.ts:580:9) at checkAndUpdateView (/packages/core/src/view/view.ts:360:3) at callViewAction (/packages/core/src/view/view.ts:615:11) at execComponentViewsAction (/packages/core/src/view/view.ts:559:7) at checkAndUpdateView (/packages/core/src/view/view.ts:370:3) at callViewAction (/packages/core/src/view/view.ts:615:11) at execEmbeddedViewsAction (/packages/core/src/view/view.ts:580:9) at checkAndUpdateView (/packages/core/s rc/view/view.ts:360:3) at callViewAction (/packages/core/src/view/view.ts:615:11) at execComponentViewsAction (/packages/core/src/view/view.ts:559:7) at checkAndUpdateView (/packages/core/src/view/view.ts:370:3) at callViewAction (/packages/core/src/view/view.ts:615:11) at execComponentViewsAction (/packages/core/src/view/view.ts:559:7) at checkAndUpdateView (/packages/core/src/view/view.ts:370:3) at callWithDebugContext (/packages/core/src/view/services.ts:630:23) at Object.debugCheckAndUpdateView [as checkAndUpdateView] (/packages/core/src/view/services.ts:347:10) at ViewRef_.detectChanges (/packages/core/src/view/refs.ts:261:16) at ComponentFixture._tick (//packages/core/testing/src/component_fixture.ts:107:28) at packages/core/testing/src/component_fixture.ts:120:36 at ZoneDelegate.Object..ZoneDelegate.invoke (/PROJECT/node_modules/zone.js/dist/zone.js:391:26) at AsyncTestZoneSpec.Object..AsyncTestZ oneSpec.onInvoke (/PROJECT/node_modules/zone.js/dist/async-test.js:106:39) at ProxyZoneSpec.Object..ProxyZoneSpec.onInvoke (/PROJECT/node_modules/zone.js/dist/proxy.js:126:39) at ZoneDelegate.Object..ZoneDelegate.invoke (/PROJECT/node_modules/zone.js/dist/zone.js:390:52) at Object.onInvoke (/packages/core/src/zone/ng_zone.ts:273:25) at ZoneDelegate.Object..ZoneDelegate.invoke (/PROJECT/node_modules/zone.js/dist/zone.js:390:52) at Zone.Object..Zone.run (/PROJECT/node_modules/zone.js/dist/zone.js:150:43) at NgZone.run (/packages/core/src/zone/ng_zone.ts:171:50) at ComponentFixture.detectChanges (/packages/core/testing/src/component_fixture.ts:120:19) at /PROJECT/s/chocolate-demo/src/app/trade/trade-invoices/test.spec.ts:82:12 at ZoneDelegate.Object..ZoneDelegate.invoke (/PROJECT/node_modules/zone.js/dist/zone.js:391:26) at AsyncTestZoneSpec.Object..AsyncTestZoneSpec.onInvoke (/PROJECT/node_modules/zone.js/dist/async-test.js:106:39) at ProxyZoneSpec.Object..ProxyZoneSpec.onInvoke (/PROJECT/node_modules/zone.js/dist/proxy.js:126:39) at ZoneDelegate.Object..ZoneDelegate.invoke (/PROJECT/node_modules/zone.js/dist/zone.js:390:52) at Zone.Object..Zone.run (/PROJECT/node_modules/zone.js/dist/zone.js:150:43) at /PROJECT/node_modules/zone.js/dist/zone.js:910:34 at ZoneDelegate.Object..ZoneDelegate.invokeTask (/PROJECT/node_modules/zone.js/dist/zone.js:423:31) at AsyncTestZoneSpec.Object..AsyncTestZoneSpec.onInvokeTask (/PROJECT/node_modules/zone.js/dist/async-test.js:90:25) at ProxyZoneSpec.Object..ProxyZoneSpec.onInvokeTask (/PROJECT/node_modules/zone.js/dist/proxy.js:157:39) at ZoneDelegate.Z497031794414A552435F90151A C3B54BZ..ZoneDelegate.invokeTask (/PROJECT/node_modules/zone.js/dist/zone.js:422:60) at Zone.Object..Zone.runTask (/PROJECT/node_modules/zone.js/dist/zone.js:195:47) at drainMicroTaskQueue (/PROJECT/node_modules/zone.js/dist/zone.js:601:35) at processTicksAndRejections (internal/process/task_queues.js:97:5) at resolvePromise (/PROJECT/node_modules/zone.js/dist/zone.js:852:31) at /PROJECT/node_modules/zone.js/dist/zone.js:917:17 at ZoneDelegate.Object..ZoneDelegate.invokeTask (/PROJECT/node_modules/zone.js/dist/zone.js:423:31) at AsyncTestZoneSpec.Object..AsyncTestZoneSpec.onInvokeTask (/PROJECT/node_modules/zone.js/dist/async-test.js:90:25) at ProxyZoneSpec.Object..ProxyZoneSpec.onInvokeTask (/PROJECT/node_modules/zone.js/dist/proxy.js:157:39) at ZoneDelegate.Object..ZoneDelegate.invokeTask (/PROJECT/node_modules/zone.js/dist/zone.js:422:60) at Zon e.Object..Zone.runTask (/PROJECT/node_modules/zone.js/dist/zone.js:195:47) at drainMicroTaskQueue (/PROJECT/node_modules/zone.js/dist/zone.js:601:35) at processTicksAndRejections (internal/process/task_queues.js:97:5)

Looks like this error happens in the HTML of the component TradeInvoicesComponent . Probably there is a usage of the replace function on some property and this property is not defined. Try to find .replace in the trade-invoices.component.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM