我有这个功能
const filterByTerm = (inputArr, searchTerm) => {
if(!searchTerm) throw new Error('Search term can not be empty');
if(!inputArr.length) throw new Error('Input array term can not be empty');
return inputArr.filter((el) => el.url.match(searchTerm.toLowerCase()))
}
这是一个测试,我试图在搜索词为空时测试scnario。我想测试发生时是否有错误
describe('Filter function', () => {
it('Should filter by a search term (link)', () => {
const input = [
{ id: 1, url: 'https://www.url1.dev' },
{ id: 2, url: 'https://www.url2.dev' },
{ id: 3, url: 'https://www.link3.dev' },
];
const output = [{ id: 3, url: 'https://www.link3.dev' }];
const output2 = [
{ id: 1, url: 'https://www.url1.dev' },
{ id: 2, url: 'https://www.url2.dev' }
]
expect(filterByTerm(input, '')).toThrow();
expect(filterByTerm(input, '')).toThrow(Error);
expect(filterByTerm(input, '')).toThrow('Search term can not be empty');
});
});
但我在Jest文档中看到的所有方法都不起作用。