Skip to content

jaenster/fake-socket

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fake-node-socket

When writing unit tests, i get pretty annoyed i cant test with an actual socket.

So, i write a small lib that fakes a socket so i can use unit as if it was an actual socket. Currently the close/open stuff isnt implemented and its pretty basic.

This code comes straight from the unit test:

import {Socket} from "net";
import {FakeSocket} from "fake-node-socket"


(async function () {
    // the payload
    const testData = Buffer.from([1, 3, 3, 7]);

    // To show typescript really believes they are Socket objects
    const sockets: Socket[] = FakeSocket.createPair();

    const [one, two] = sockets;

    const recved = await new Promise(resolve => {
        // hook on the data event of the first socket
        one.on('data', data => resolve(data));

        // write data over the second socket
        two.write(testData);
    });

    console.log(recved === testData); // true, its even the same Buffer object
})();

esm - commonjs

Since this package is so small it supports both esm and commonjs

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published