我正在使用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] 删除。
我来说两句