Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@bielik/express-test-helpers

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@bielik/express-test-helpers - npm Package Compare versions

Comparing version 1.0.0 to 1.1.0

62

index.js
//@ts-check
const express = require('express');
module.exports.createServer = createServer;
module.exports.closeServer = closeServer;
module.exports = {
createServer,
closeServer,
closeServers
};
/**
* @type {import("http").Server}
* @typedef {Object} PortAndApp
* @property {import("express").Express} app
* @property {number} port
* @property {import("http").Server} server
*/
var server;
/**
*
*
* @type {PortAndApp[]}
*/
var servers = [];
/**
* @param {Number} port
* @returns {Promise<{app: import("express").Express, port: number}>}
* @returns {Promise<PortAndApp>}
*/

@@ -21,7 +31,6 @@

const app = express();
if (server && server.close) {
server.close();
}
server = app.listen(port, () => {
return resolve({ app, port });
let server = app.listen(port, () => {
resolve({ app, port, server });
servers.push({ server, app, port });
console.log(`Created test server on port ${port}`);
});

@@ -34,13 +43,30 @@ } catch (e) {

function closeServer() {
if (this.timeout) {
/**
*
* @param {Number} closePort
* @returns {Promise}
*/
function closeServer(closePort) {
if (typeof this.timeout === 'function') {
this.timeout(5000);
}
return new Promise((resolve) => {
if (server && server.close) {
server.close(resolve);
} else {
servers.forEach(({ server, port }) => {
if(port === closePort && server.close) {
return server.close(resolve);
}
resolve();
}
});
});
}
/**
* @returns {Promise[]}
*/
function closeServers() {
return servers.map(({ port }) => {
return closeServer(port);
});
}
{
"name": "@bielik/express-test-helpers",
"version": "1.0.0",
"version": "1.1.0",
"description": "Unit test helper functions for node/express",

@@ -5,0 +5,0 @@ "main": "index.js",

//@ts-check
const {expect} = require('chai');
const request = require('@bielik/request');
const {createServer, closeServer} = require('.');
const {createServer, closeServer, closeServers} = require('.');

@@ -19,3 +19,3 @@ describe('server creation', function(){

await closeServer();
await closeServer(port);

@@ -29,2 +29,38 @@ try {

});
it('should create and close multiple servers', async function () {
this.timeout(30e3);
let port = 7777;
const apps = await Promise.all([
createServer(port--),
createServer(port--),
]);
let test = async function({app, port}){
let payload = 'body';
let uri = `http://localhost:${port}/any`;
app.get('*', (req, res) => {
res.send(payload);
});
var body = await request({ uri });
expect(body).to.equal(payload);
};
await Promise.all(apps.map(test));
await closeServers();
let testPort = 7777;
try {
let uri = `http://localhost:${testPort}/any`;
await request({ uri });
expect('request after server closed').to.equal('should have thrown');
} catch (e) {
expect(e.message).to.equal(`connect ECONNREFUSED 127.0.0.1:${testPort}`);
}
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc