编写用于在节点 js 中搜索记录和排序记录的单元测试

Writing a unit test for searching record and sorting the record in node js

刚开始写单元测试,想问下下面的示例代码写单元测试的例子是什么,比如什么时候用assert?以及如何在下面的代码中测试过滤器?谢谢你。我在 uni 测试中使用 mocha

#当前单元测试代码

.....
      it('load the hook', () => {
        assert.ok(search, 'Load the hook');
      });
    
      it('searches for record and sort', async () => {
    
        const search_key = "Rajesh"
    
        const people = await People.find({
          query: {
            $limit: 25,
            $skip: 0,
            $sort: {
              'people': 1,
            },
            $or: [{
              name: {
                $like: search_key,
              },
            }],
          },
        });
      });

正如我们所讨论的,我添加了用于按名称和排序检查过滤器的测试用例。对于排序,您可以使用 chai-sorting 包:npm install chai-sorted。您需要根据下面的评论稍微调整一下代码。希望对你有帮助

it('searches for record and sort', async () => {
    const search_key = "Rajesh";
    const limit = 25;
    //you will need to have `data` variable and link `search_key` and `limit` with it somehow
    const people = await search_data()(data);
    if (people) {
      //length must be less than or equal to `limit`
      expect(people.length).to.be.at.most(limit);

      // check sorting by name. you can change it as descendingBy if you need
      expect(people).to.be.ascendingBy("name");

      people.forEach(person => { 
        //checking filter for name
        expect(person.name).to.have.string(search_key);
      });
    }        
});