[英]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.