如何使用Typescript使用Jest模拟tls.PeerCertificate?

时间:2020-05-28 22:43:35

标签: typescript

我可以像这样用茉莉花来模拟getCertificate:

const socket = new TLSSocket(new net.Socket())
socket.authorized = false
socket.authorizationError = Error('Invalid certificate')
//Mocking here
const spyOnGetCertificate = spyOn(socket, 'getCertificate').and.returnValue({'subject': {'CN': 'testing'}});

但是,当我尝试开玩笑时:

const socket = new TLSSocket(new net.Socket())
socket.authorized = false
socket.authorizationError = Error('Invalid certificate')
//Mocking here
const spyOnGetCertificate = jest.spyOn(socket, 'getCertificate').mockImplementationOnce(() => {
  return {'subject': {'CN': 'testing'}}
})
socket.getCertificate = spyOnGetCertificate

我得到一个错误:

类型'SpyInstance'不可分配给类型'()=>对象| PeerCertificate”。 类型'SpyInstance'没有提供与签名'()的匹配: PeerCertificate'.ts(2322)

设置了所有PeerCertificate属性后,仍然出现错误。

1 个答案:

答案 0 :(得分:0)

糟糕,不需要这样做?‍♂️

socket.getCertificate = spyOnGetCertificate