In my project, I'm looking to specifically mock the Socket
class from the net
node module. The documentation for this can be found here.
Within my codebase, there is a class structured similar to the following...
import { Socket } from 'net';
class Foo {
protected socket: Socket;
constructor() {
this.socket = new Socket();
}
connect() {
const connPromise = new Promise<undefined>(resolve => {
this.socket.connect(80, '192.168.1.1', () => {
// Perform actions with local state
resolve();
});
});
return connPromise;
}
}
I am currently uncertain about how to properly mock the Socket class in order to provide a mock implementation for this.socket.connect
.
import { Foo } from './foo';
// Is it possible to mock the Socket Class directly?
jest.mock('net')
describe("Foo", () => {
it('resolves on connect', () => {
const tester = new Foo();
expect(tester.connect()).resolves.toBeUndefined();
})
})
Any suggestions on how to manage the implementation of the this.socket.connect
method?