![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
#Dummy.js
Simple testing of node tcp/tls servers.
Useful with testing libraries such as Mocha and Should.js.
Getting it:
npm install dummy
Using it:
var Dummy = require('dummy');
Imagine a server that responds with the data you sent it, prepended with 'you sent : ', if we send the server 'hey' it should respond with 'you sent : hey'. Pretty simple, right?. Let's say you wanted to write some tests for your nifty server that verified it's behavior, even when multiple things are sent to the server. Using Mocha and Should.js, let us examine what this code might look like without Dummy.js.
//leaving the requires out to keep it short
describe('awesome server', function() {
//before any tests are run, get an instance of AwesomeServer up and
// listening for connections
var awesomeServer;
before(function(done) {
awesomeServer = new AwesomeServer();
awesomeServer.listen(1234, function() {
done();
});
});
it('should respond correctly', function(done) {
//connect to the AwesomeServer
var client = net.connect(1234, function() {
client.write('hey\n');
});
//we got some data, let's look at it
client.on('data', function(data) {
data = data.toString();
if(data === 'you sent : hey) {
client.write('haha\n');
} if(data === 'you sent : haha') {
done;
}
});
});
});
This test ignores a lot of things. The server could incorrectly be sending two pieces of data to the client, and they happened to fit into one packet. The server could be sending one very long message that did not fit into one packet of data. We need to delimit our data and work accordingly. Secondly, without adding some sort of count, we cannot be sure that the response received is associated with the first write, or the second write.
This is where Dummy.js comes in handy. Let's look at the same problem again, but use Dummy.js.
//leaving the requires out to keep it short
describe('awesome server', function() {
//before any tests are run, get an instance of AwesomeServer up and
// listening for connections
var awesomeServer;
before(function(done) {
awesomeServer = new AwesomeServer();
awesomeServer.listen(1234, function() {
done();
});
});
it('should respond correctly', function(done) {
var dummy = new Dummy(false, 1234, '127.0.0.1', '\n', function() {
dummy.send('hey\n', 'you sent : hey', function(expected, data) {
expected.should.equal.true;
dummy.send('haha\n', 'you sent : haha', function(expected, data) {
expected.should.equal.true;
done();
});
});
});
});
});
This makes a lot more sense, and is pretty easy to follow.
#####+ Dummy(secure, port, hostname, delimiter, connectionCallback)
#####- send(data, response, responseCallback(expected, data))
Right now, only send more data to the server in callbacks that the Dummy calls. This keeps weird conditions from happening. I am working on an implementation around this issue.
This project is very short and sweet, however, I will be making changes to it frequently. Feel free to fork me. Also, feel free to comment on my coding style, I'm new to Javascript, and I'm not sure if I'm doing it right.
Thanks!
FAQs
a tcp/tls dummy client for server testing
The npm package dummy receives a total of 70 weekly downloads. As such, dummy popularity was classified as not popular.
We found that dummy demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.