测试类型的正确方法是什么?

用户名

我正在使用Jest进行单元测试,并且我想测试变量是否为Object。有更好的方法吗?

it('should throw an Error if options is not an Object', () => {
  const error = 'Options should be an Object.'

  expect(() => new Foo([])).toThrow(error)
  expect(() => new Foo(123)).toThrow(error)
  expect(() => new Foo('')).toThrow(error)
  expect(() => new Foo(true)).toThrow(error)
  expect(() => new Foo(NaN)).toThrow(error)
  expect(() => new Foo(null)).toThrow(error)
  expect(() => new Foo(() => {})).toThrow(error)
})
乔纳斯·威尔姆斯

与往常一样,循环可能会有所帮助:

 for(const type of [[], 123, '', true, NaN, null])
   expect(() => new Foo(type)).toThrow(error);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

测试此任务的正确方法是什么?

测试类型是否为分配器的正确方法是什么?

将Variant转换为正确类型的正确方法是什么?

测试Java方法输出的“正确”方法是什么?

XSLT1.0 中测试由变量给出的节点是否为特定类型(例如文本)的正确方法是什么?

测试硒中必填字段的正确方法是什么

存储用户测验/测试答案的正确方法是什么?

测试抛出IntegrityError的SQLAlchemy代码的正确方法是什么?

编写用Flow验证的Jest测试的正确方法是什么?

在Laravel中测试控制器的正确方法是什么?

为Eclipse片段创建JUnit测试的正确方法是什么?

解决Codility的PermMissingElem测试的正确方法是什么?(Java)

在Rust中组织代码/测试的正确方法是什么?

测试链接的catchError函数的正确方法是什么

使用pytest管理测试数据的正确方法是什么?

运行注入代码的集成测试库的正确方法是什么?

@@ ROWCOUNT的正确类型是什么?

C#方法签名-限制类型-正确的术语是什么?

为该模块添加类型定义的正确方法是什么

检查网址类型值的正确方法是什么?

在Flow中定义子类型的正确方法是什么?

为 Vue 道具创建类型的正确方法是什么

打印rlim_t类型的值的正确方法是什么?

使用EVReflection解析Dictionary类型的正确方法是什么?

在Bash脚本中过滤类型错误的正确方法是什么?

为Redux动作定义类型的正确方法是什么?

正确的方法是什么?

打字稿中 React 测试库包装器的正确类型是什么?

在应用集成测试时,测试组合功能的正确方法是什么?