Jest:测试类型或 null

2024-01-05

我有一个测试,我想测试我收到的对象值类型是否与架构匹配。问题是对于某些键我可能会收到一些东西或 null

到目前为止我尝试过这个

  const attendeeSchema = {
  birthDate: expect.extend(toBeTypeOrNull("Date")),
  contact: expect.extend(toBeTypeOrNull(String)),
  createdAt: expect.any(Date),
  firstName: expect.any(String),
  id: expect.any(Number),
  idDevice: expect.extend(toBeTypeOrNull(Number)),
  information: expect.extend(toBeTypeOrNull(String)),
  lastName: expect.any(String),
  macAddress: expect.extend(toBeTypeOrNull(String)),
  updatedAt: expect.any(Date),
  // state: toBeTypeOrNull()
};

    const toBeTypeOrNull = (received, argument) => {
  const pass = expect(received).toEqual(expect.any(argument));
  if (pass || received === null) {
    return {
      message: () => `Ok`,
      pass: true
    };
  } else {
    return {
      message: () => `expected ${received} to be ${argument} type or null`,
      pass: false
    };
  }
};

在我的测试中

 expect(res.result.data).toBe(attendeeSchema);

我也尝试过 tobeEqual 和其他东西......

我的测试没有通过

TypeError: any() expects to be passed a constructor function. Please pass one or use anything() to match any object.

我不知道在这里做什么.. 如果有人有想法 谢谢


之前给出的所有回复均未正确使用expect()在他们的实施中,所以他们并没有真正发挥作用。

你想要的是一个笑话匹配器,其工作原理如下any()但接受空值并且不使用expect()其实施中的职能。您可以通过基本上复制原始内容来实现它作为扩展any()实施(从Jasmine https://github.com/jasmine/jasmine/blob/master/src/core/asymmetric_equality/Any.js),但在开头添加了一个空测试:

expect.extend({
  nullOrAny(received, expected) {
    if (received === null) {
      return {
        pass: true,
        message: () => `expected null or instance of ${this.utils.printExpected(expected) }, but received ${ this.utils.printReceived(received) }`
      };
    }

    if (expected == String) {
      return {
        pass: typeof received == 'string' || received instanceof String,
        message: () => `expected null or instance of ${this.utils.printExpected(expected) }, but received ${ this.utils.printReceived(received) }`
      };        
    }

    if (expected == Number) {
      return {
        pass: typeof received == 'number' || received instanceof Number,
        message: () => `expected null or instance of ${this.utils.printExpected(expected)}, but received ${this.utils.printReceived(received)}`
      };
    }

    if (expected == Function) {
      return {
        pass: typeof received == 'function' || received instanceof Function,
        message: () => `expected null or instance of ${this.utils.printExpected(expected)}, but received ${this.utils.printReceived(received)}`
      };
    }

    if (expected == Object) {
      return {
        pass: received !== null && typeof received == 'object',
        message: () => `expected null or instance of ${this.utils.printExpected(expected)}, but received ${this.utils.printReceived(received)}`
      };
    }

    if (expected == Boolean) {
      return {
        pass: typeof received == 'boolean',
        message: () => `expected null or instance of ${this.utils.printExpected(expected)}, but received ${this.utils.printReceived(received)}`
      };
    }

    /* jshint -W122 */
    /* global Symbol */
    if (typeof Symbol != 'undefined' && this.expectedObject == Symbol) {
      return {
        pass: typeof received == 'symbol',
        message: () => `expected null or instance of ${this.utils.printExpected(expected)}, but received ${this.utils.printReceived(received)}`
      };
    }
    /* jshint +W122 */

    return {
      pass: received instanceof expected,
      message: () => `expected null or instance of ${this.utils.printExpected(expected)}, but received ${this.utils.printReceived(received)}`
    };
  }
});

将以上内容放入.js文件,然后开玩笑地指向该文件setupFilesAfterEnv https://jestjs.io/docs/en/configuration#setupfilesafterenv-array配置变量。现在您可以运行测试,例如:

const schema = {
  person: expect.nullOrAny(Person),
  age: expect.nullOrAny(Number)
};

expect(object).toEqual(schema);

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Jest:测试类型或 null 的相关文章

随机推荐

  • 使用 PowerShell 匹配存储在变量中的字符串

    我正在尝试创建一个备份脚本来移动超过 30 天的文件 但我希望能够从列表中排除文件夹 a C Temp Exclude test b C Temp Exclude 如果我运行以下命令 a match b 下列的 Guy Guy Thomas
  • Select2 使用ajax响应数据生成id

    我的 JSON 响应数据不包含 ID 字段 而 Select2 需要该字段才能显示结果 在文档中 他们提供了一种生成 id 的方法 但是我无法这样做 有人可以提供一个关于如何执行此操作的示例吗 到目前为止我已经尝试过了 itemSearch
  • Net Core 2 中 HandleErrorAttribute 的等效项

    我正在将 Net 4 6 2 项目迁移到 Net Core 2 相当于什么HandleErrorAttribute 第 2 行以下接收错误 public static void RegisterGlobalFilters GlobalFil
  • 在 Java 中使用 volatile 关键字的完整示例?

    我需要一个简单的使用示例volatileJava 中的关键字 由于不使用而导致行为不一致volatile 理论部分volatile用法对我来说已经很清楚了 首先 没有保证由于非易失性变量而暴露缓存的方式 您的 JVM 可能一直对您非常友善
  • 访问VBA:根据非绑定列在组合框中查找项目

    我在 Access 表单上有一个两列组合框 表示键到代码的映射 组合框的第一列是 绑定列 即 当MyComboBox Value叫做 我需要动态设置Value我的组合框基于第二列中找到的值 例如 如果我的组合框源是 Value Code A
  • 蓝牙+模拟鼠标

    有人知道是否可以制作一个应用程序通过蓝牙模拟触摸屏鼠标或触控板 如何使 PC 或 MAC 将我识别为鼠标设备 问候 胡安 您应该查看蓝牙 HID 规范 这可能是可能的 具体取决于您用来模拟鼠标 触控板的设备堆栈 我不熟悉 Android 上
  • 由于错误 800a025e,无法完成操作

    这个错误在 IE10 11 中意味着什么 Error Could not complete the operation due to error 800a025e 我该如何调试它 它说的是这一行 this nativeSelection r
  • TabPanel 中的 gwt ScrollPanel:没有垂直滚动条

    EDIT 我通过调整组件内的大小来修复空白行为VerticalPanel 这似乎对面板尺寸产生了影响 但控制台却忽略了这一点 我不太明白怎么办 但是 我的面板仍然没有显示垂直滚动条 在 GWT 项目中 我具有以下结构 Page DockLa
  • 如何检查我是否有 Base Clearcase 或 UCM?

    我是 ClearCase 的新手 我以前用过理性协同 我们在项目中使用 ClearCase 进行版本控制 在我的旧项目中 我使用了合理的协同作用 其中我们为文件中的任何修改创建 任务 我了解到我们在 ClearCase 中有活动 我想在我们
  • 用于启用/禁用用户的 Firebase 触发器

    在 Firebase Auth 控制台中 每个用户都有一个选项 例如启用 禁用其帐户 如何在 Firebase 函数和 Android 应用程序中触发此事件 函数无法在这种事件上触发 至少现在还没有 函数只会在这些情况下触发 查看doc h
  • Java 中的函数式编程 [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 Java 有没有一个好的函数式编程库 我正在寻找类似的东西谓词 http msdn microsoft com en us library
  • 读取调制解调器固件版本:Android

    我正在开发一个 iPhone 和 Android 中的应用程序 我需要阅读Modem Firmware Version正如 iPhone 开发人员在他身边所做的那样 我在 Internet SO 上搜索 但找不到与我的问题相关的任何内容 是
  • 在 JUnit5 (Eclipse) 中创建 TestSuite

    我在 eclipse 中创建了多个测试用例 java 文件 JUnit 的版本是 JUnit5 现在 我尝试通过 eclipse GUI 创建 Junit TestSuite 在创建过程中 我没有在可用版本中看到 JUnit5 这是我为创建
  • 浏览器中自动完成下拉菜单的样式

    例如 在许多网站上 当输入用户名时 会在显示先前输入的位置出现一个下拉菜单 以便用户可以轻松选择某些内容而不用输入 我知道您可以通过让表单或输入具有以下属性来在浏览器中关闭此功能autocomplete off 问题是当我想要它打开并且输入
  • 告诉 `endl` 不要刷新

    我的程序打印大量短行cout 作为一个稍微做作的例子 我的线条看起来有点像这样 cout lt lt The variable s value is lt
  • 使用核心数据实体更新表节标题的有效方法?

    我为我的 UITableView 使用 NSFetchedResultsController 它显示了我存储在核心数据中的一堆事件 我想做的是按相对日期 即今天 明天 本周等 对表格进行分组 每个事件都有一个开始日期 我尝试在事件实体中创建
  • 为什么索引会使查询变得非常慢?

    有一天我回答了一个question https stackoverflow com questions 5642880 slow mysql query 5642908 5642908就这样 被认为是正确的 但答案给我留下了很大的疑问 不久
  • 使用 vmap 时,Jax 不支持不可散列的静态参数

    这与这个问题 https stackoverflow com questions 65612989 jax cannot find the static argnums 经过一些工作 我设法将其更改为最后一个错误 代码现在看起来像这样 im
  • 显示替代文本时的 img 大小

    我在 HTML 页面上有一组图像 它们都设置了宽度和高度属性 img width 88 height 78 src document show 0759122435f5333493726f9f1a845490 type THUMBNAIL
  • Jest:测试类型或 null

    我有一个测试 我想测试我收到的对象值类型是否与架构匹配 问题是对于某些键我可能会收到一些东西或 null 到目前为止我尝试过这个 const attendeeSchema birthDate expect extend toBeTypeOr