简体   繁体   English

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

[英]What is the proper way to test types?

I am using Jest for unit-testing and I want to test if the variable is an Object. 我正在使用Jest进行单元测试,并且我想测试变量是否为Object。 Is there a better way to do it? 有更好的方法吗?

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)
})

As always a loop might help: 与往常一样,循环可能会有所帮助:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在javascript中测试对象是否是jQuery对象的正确/正确方法是什么? - What's the correct/proper way to test if an object is a jQuery object in javascript? 使用 JavaScript 测试输入是韩文还是中文的正确方法是什么? - What is proper way to test if the input is Korean or Chinese using JavaScript? 用悬念测试 Vue3 异步设置组件的正确方法是什么? - What is the proper way to test Vue3 async setup component with suspense? 正确的方法来测试xhr成功 - Proper way to test xhr success 注释可以返回两种不同类型对象的函数的正确方法是什么? - What is the proper way to annotate a function that can return two different types of objects? 处理使用 object 破坏和 object rest 的组件的道具类型的正确方法是什么...传播收集道具 - What is the proper way to handle prop-types for a component that uses object destructing and object rest...spread to collect props 使用 Jest 和 react-testing-library 测试具有大量输入字段的表单的正确方法是什么? - What is the proper way to test a form with a lot of input fields using Jest and react-testing-library? 设置iframe的正确方法是什么? - What is the proper way to style an iframe? 猫鼬填充的正确方法是什么? - What is the proper way of mongoose populate? 可视化数据的正确方法是什么? - What is the proper way of visualizing data?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM