TypeError: Cannot read properties of undefined (reading 'filter') at <Jasmine>

TypeError: Cannot read properties of undefined (reading 'filter') at <Jasmine>

我正在为 clear Item 创建一个测试,但是当我 运行 测试时我得到了错误 “TypeError:无法读取未定义的属性(读取 'filter'),位于“

如何在测试中安排 运行 过滤器?

我的函数:

clearItem = (item, key) => {
        this.form[key] = this.form[key].filter(el => el !== item);
        this.chipsOptions = this.chipsOptions.filter(el => el !== item);
    }

我的测试:

describe('clearItem()', () => {
        it('should clear item', () => {
            let item = {id:1, name:'test'};
            component.form = { key: {id:1}, id: 1, name: 'testForm' };

            component.addItem(item);
            component.clearItem(item, {key: { id: 1 }});

            expect(component.form['key']).not.toEqual(item);
            expect(component.chipsOptions['key']).not.toEqual(item);
        });
    });

filter 方法是为 array 对象定义的。在使用filter方法之前,请尝试检查您使用此方法的对象类型。

console.log(Array.isArray(this.form[key]));
console.log(Array.isArray(this.chipsOptions));
// When both of them are true, then you can use filter.

filter method reference

isArray method reference