【问题标题】:'arraycontaining' does not exist on type 'jestmatchers''jestmatchers' 类型上不存在'array contains'
【发布时间】:2021-09-14 10:34:52
【问题描述】:

我在 Nestjs 中为我的控制器编写测试。我希望一个员工数组包含对象{id:1, firstname: 'john', lastname:'Dole'}。所以我写:

it('should get an employee', () => {
    return controller.findAll('john').then((data) => {
      expect(data).arrayContaining([
        {
          id: 1,
          firstname: 'john',
          lastname: 'Dole',
        },
      ]);
    });
  });

但出现错误roperty 'arrayContaining' does not exist on type 'JestMatchers<Employee[]>' 我应该在 Nestjs 中安装额外的包或更新 jest 吗?我已经安装了"@nestjs/testing": "^7.6.15",

【问题讨论】:

标签: typescript jestjs nestjs


【解决方案1】:

arrayContaining 本身不进行任何匹配,而是返回一个匹配器,可以与例如toEqual,像这样:

      expect(data).toEqual(expect.arrayContaining([
        {
          id: 1,
          firstname: 'john',
          lastname: 'Dole',
        },
      ]));

【讨论】:

    猜你喜欢
    • 2023-03-09
    • 2022-12-17
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 2017-04-03
    • 2020-04-04
    • 1970-01-01
    • 2011-09-19
    相关资源
    最近更新 更多