@bielik/express-test-helpers
Advanced tools
Comparing version 1.2.0 to 1.2.1
/// <reference types="node" /> | ||
import express from "express"; | ||
import http from 'http'; | ||
export { createServer, closeServer, closeServers }; | ||
import http from "http"; | ||
export interface IPortAndApp { | ||
@@ -10,2 +9,3 @@ app: express.Express; | ||
} | ||
export declare let servers: IPortAndApp[]; | ||
/** | ||
@@ -15,3 +15,3 @@ * @param {Number} port | ||
*/ | ||
declare function createServer(port: number): Promise<IPortAndApp>; | ||
export declare function createServer(port: number): Promise<IPortAndApp>; | ||
/** | ||
@@ -21,7 +21,7 @@ * @param {Number} portToClose | ||
*/ | ||
declare function closeServer(portToClose: number): Promise<void>; | ||
export declare function closeServer(portToClose: number): Promise<void>; | ||
/** | ||
* @description closes all servers created here | ||
*/ | ||
declare function closeServers(): Promise<void[]>; | ||
export declare function closeServers(): Promise<void[]>; | ||
//# sourceMappingURL=index.d.ts.map |
13
index.js
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.closeServers = exports.closeServer = exports.createServer = exports.servers = void 0; | ||
var tslib_1 = require("tslib"); | ||
var express_1 = tslib_1.__importDefault(require("express")); | ||
var servers = []; | ||
var express_1 = (0, tslib_1.__importDefault)(require("express")); | ||
exports.servers = []; | ||
/** | ||
@@ -13,6 +14,6 @@ * @param {Number} port | ||
try { | ||
var app_1 = express_1.default(); | ||
var app_1 = (0, express_1.default)(); | ||
var server_1 = app_1.listen(port, function () { | ||
resolve({ app: app_1, port: port, server: server_1 }); | ||
servers.push({ server: server_1, app: app_1, port: port }); | ||
exports.servers.push({ server: server_1, app: app_1, port: port }); | ||
console.log("Created test server on port " + port); | ||
@@ -33,3 +34,3 @@ }); | ||
return new Promise(function (resolve) { | ||
servers.forEach(function (_a) { | ||
exports.servers.forEach(function (_a) { | ||
var server = _a.server, port = _a.port; | ||
@@ -50,3 +51,3 @@ if (port === portToClose && server.close) { | ||
function closeServers() { | ||
var promises = servers.map(function (_a) { | ||
var promises = exports.servers.map(function (_a) { | ||
var port = _a.port; | ||
@@ -53,0 +54,0 @@ return closeServer(port); |
71
index.ts
import express from "express"; | ||
import http from 'http'; | ||
import http from "http"; | ||
export { | ||
createServer, | ||
closeServer, | ||
closeServers | ||
}; | ||
export interface IPortAndApp { | ||
app: express.Express; | ||
port: number; | ||
server: http.Server | ||
app: express.Express; | ||
port: number; | ||
server: http.Server; | ||
} | ||
var servers: IPortAndApp[] = []; | ||
export let servers: IPortAndApp[] = []; | ||
@@ -22,15 +16,15 @@ /** | ||
*/ | ||
function createServer(port: number): Promise<IPortAndApp> { | ||
return new Promise((resolve, reject) => { | ||
try { | ||
const app = express(); | ||
let server = app.listen(port, () => { | ||
resolve({ app, port, server }); | ||
servers.push({ server, app, port }); | ||
console.log(`Created test server on port ${port}`); | ||
}); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}); | ||
export function createServer(port: number): Promise<IPortAndApp> { | ||
return new Promise((resolve, reject) => { | ||
try { | ||
const app = express(); | ||
let server = app.listen(port, () => { | ||
resolve({ app, port, server }); | ||
servers.push({ server, app, port }); | ||
console.log(`Created test server on port ${port}`); | ||
}); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}); | ||
} | ||
@@ -42,24 +36,23 @@ | ||
*/ | ||
function closeServer(portToClose: number): Promise<void> { | ||
return new Promise((resolve) => { | ||
servers.forEach(({ server, port }) => { | ||
if (port === portToClose && server.close) { | ||
return server.close(() => { | ||
resolve(); | ||
}); | ||
} | ||
resolve(); | ||
export function closeServer(portToClose: number): Promise<void> { | ||
return new Promise((resolve) => { | ||
servers.forEach(({ server, port }) => { | ||
if (port === portToClose && server.close) { | ||
return server.close(() => { | ||
resolve(); | ||
}); | ||
} | ||
resolve(); | ||
}); | ||
}); | ||
} | ||
/** | ||
* @description closes all servers created here | ||
*/ | ||
function closeServers(): Promise<void[]> { | ||
const promises = servers.map(({ port }) => { | ||
return closeServer(port); | ||
}); | ||
return Promise.all(promises); | ||
export function closeServers(): Promise<void[]> { | ||
const promises = servers.map(({ port }) => { | ||
return closeServer(port); | ||
}); | ||
return Promise.all(promises); | ||
} |
{ | ||
"name": "@bielik/express-test-helpers", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "Unit test helper functions for node/express", | ||
@@ -28,2 +28,2 @@ "main": "index.js", | ||
"homepage": "https://bitbucket.org/davidbielik/express-test-helpers#readme" | ||
} | ||
} |
62
tests.js
@@ -6,25 +6,28 @@ "use strict"; | ||
var chai_1 = require("chai"); | ||
var request_1 = tslib_1.__importDefault(require("@bielik/request")); | ||
var request_1 = (0, tslib_1.__importDefault)(require("@bielik/request")); | ||
var index_1 = require("./index"); | ||
describe('server creation', function () { | ||
it('should create and close a server', function () { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
describe("server creation", function () { | ||
var serverCount = 0; | ||
it("should create and close a server", function () { | ||
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () { | ||
var payload, _a, app, port, uri, body, e_1; | ||
return tslib_1.__generator(this, function (_b) { | ||
return (0, tslib_1.__generator)(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
(0, chai_1.expect)(index_1.servers.length).to.equal(0); | ||
this.timeout(30e3); | ||
payload = 'body'; | ||
return [4 /*yield*/, index_1.createServer(7777)]; | ||
payload = "body"; | ||
return [4 /*yield*/, (0, index_1.createServer)(7777)]; | ||
case 1: | ||
_a = _b.sent(), app = _a.app, port = _a.port; | ||
(0, chai_1.expect)(index_1.servers.length).to.equal(++serverCount); | ||
uri = "http://localhost:" + port + "/any"; | ||
app.get('*', function (req, res) { | ||
app.get("*", function (req, res) { | ||
res.send(payload); | ||
}); | ||
return [4 /*yield*/, request_1.default({ uri: uri })]; | ||
return [4 /*yield*/, (0, request_1.default)({ uri: uri })]; | ||
case 2: | ||
body = _b.sent(); | ||
chai_1.expect(body).to.equal(payload); | ||
return [4 /*yield*/, index_1.closeServer(port)]; | ||
(0, chai_1.expect)(body).to.equal(payload); | ||
return [4 /*yield*/, (0, index_1.closeServer)(port)]; | ||
case 3: | ||
@@ -35,10 +38,10 @@ _b.sent(); | ||
_b.trys.push([4, 6, , 7]); | ||
return [4 /*yield*/, request_1.default({ uri: uri })]; | ||
return [4 /*yield*/, (0, request_1.default)({ uri: uri })]; | ||
case 5: | ||
_b.sent(); | ||
chai_1.expect('request after server closed').to.equal('should have thrown'); | ||
(0, chai_1.expect)("request after server closed").to.equal("should have thrown"); | ||
return [3 /*break*/, 7]; | ||
case 6: | ||
e_1 = _b.sent(); | ||
chai_1.expect(e_1.message).to.equal("connect ECONNREFUSED 127.0.0.1:" + port); | ||
(0, chai_1.expect)(e_1.message).to.equal("connect ECONNREFUSED 127.0.0.1:" + port); | ||
return [3 /*break*/, 7]; | ||
@@ -50,6 +53,6 @@ case 7: return [2 /*return*/]; | ||
}); | ||
it('should create and close multiple servers', function () { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
it("should create and close multiple servers", function () { | ||
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () { | ||
var port, apps, test, testPort, uri, e_2; | ||
return tslib_1.__generator(this, function (_a) { | ||
return (0, tslib_1.__generator)(this, function (_a) { | ||
switch (_a.label) { | ||
@@ -60,23 +63,24 @@ case 0: | ||
return [4 /*yield*/, Promise.all([ | ||
index_1.createServer(port--), | ||
index_1.createServer(port--), | ||
(0, index_1.createServer)(port--), | ||
(0, index_1.createServer)(port--), | ||
])]; | ||
case 1: | ||
apps = _a.sent(); | ||
(0, chai_1.expect)(index_1.servers.length).to.equal((serverCount += 2)); | ||
test = function (_a) { | ||
var app = _a.app, port = _a.port; | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () { | ||
var payload, uri, body; | ||
return tslib_1.__generator(this, function (_b) { | ||
return (0, tslib_1.__generator)(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
payload = 'body'; | ||
payload = "body"; | ||
uri = "http://localhost:" + port + "/any"; | ||
app.get('*', function (req, res) { | ||
app.get("*", function (req, res) { | ||
res.send(payload); | ||
}); | ||
return [4 /*yield*/, request_1.default({ uri: uri })]; | ||
return [4 /*yield*/, (0, request_1.default)({ uri: uri })]; | ||
case 1: | ||
body = _b.sent(); | ||
chai_1.expect(body).to.equal(payload); | ||
(0, chai_1.expect)(body).to.equal(payload); | ||
return [2 /*return*/]; | ||
@@ -90,3 +94,3 @@ } | ||
_a.sent(); | ||
return [4 /*yield*/, index_1.closeServers()]; | ||
return [4 /*yield*/, (0, index_1.closeServers)()]; | ||
case 3: | ||
@@ -99,10 +103,10 @@ _a.sent(); | ||
uri = "http://localhost:" + testPort + "/any"; | ||
return [4 /*yield*/, request_1.default({ uri: uri })]; | ||
return [4 /*yield*/, (0, request_1.default)({ uri: uri })]; | ||
case 5: | ||
_a.sent(); | ||
chai_1.expect('request after server closed').to.equal('should have thrown'); | ||
(0, chai_1.expect)("request after server closed").to.equal("should have thrown"); | ||
return [3 /*break*/, 7]; | ||
case 6: | ||
e_2 = _a.sent(); | ||
chai_1.expect(e_2.message).to.equal("connect ECONNREFUSED 127.0.0.1:" + testPort); | ||
(0, chai_1.expect)(e_2.message).to.equal("connect ECONNREFUSED 127.0.0.1:" + testPort); | ||
return [3 /*break*/, 7]; | ||
@@ -109,0 +113,0 @@ case 7: return [2 /*return*/]; |
106
tests.ts
@@ -5,62 +5,70 @@ //@ts-check | ||
import request from "@bielik/request"; | ||
import { createServer, closeServer, closeServers } from "./index"; | ||
import { createServer, closeServer, closeServers, servers } from "./index"; | ||
describe('server creation', function () { | ||
it('should create and close a server', async function () { | ||
this.timeout(30e3); | ||
const payload = 'body'; | ||
const { app, port } = await createServer(7777); | ||
const uri = `http://localhost:${port}/any`; | ||
app.get('*', (req, res) => { | ||
res.send(payload); | ||
}); | ||
describe("server creation", function () { | ||
let serverCount = 0; | ||
var body = await request({ uri }); | ||
expect(body).to.equal(payload); | ||
await closeServer(port); | ||
try { | ||
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:${port}`); | ||
} | ||
it("should create and close a server", async function () { | ||
expect(servers.length).to.equal(0); | ||
this.timeout(30e3); | ||
const payload = "body"; | ||
const { app, port } = await createServer(7777); | ||
expect(servers.length).to.equal(++serverCount); | ||
const uri = `http://localhost:${port}/any`; | ||
app.get("*", (req, res) => { | ||
res.send(payload); | ||
}); | ||
it('should create and close multiple servers', async function () { | ||
this.timeout(30e3); | ||
let port = 7777; | ||
var body = await request({ uri }); | ||
expect(body).to.equal(payload); | ||
const apps = await Promise.all([ | ||
createServer(port--), | ||
createServer(port--), | ||
]); | ||
await closeServer(port); | ||
let test = async function ({ app, port }: { app: express.Express, port: number }) { | ||
let payload = 'body'; | ||
let uri = `http://localhost:${port}/any`; | ||
app.get('*', (req, res) => { | ||
res.send(payload); | ||
}); | ||
try { | ||
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:${port}`); | ||
} | ||
}); | ||
var body = await request({ uri }); | ||
expect(body).to.equal(payload); | ||
}; | ||
it("should create and close multiple servers", async function () { | ||
this.timeout(30e3); | ||
let port = 7777; | ||
await Promise.all(apps.map(test)); | ||
const apps = await Promise.all([ | ||
createServer(port--), | ||
createServer(port--), | ||
]); | ||
expect(servers.length).to.equal((serverCount += 2)); | ||
let test = async function ({ | ||
app, | ||
port, | ||
}: { | ||
app: express.Express; | ||
port: number; | ||
}) { | ||
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 closeServers(); | ||
await Promise.all(apps.map(test)); | ||
let testPort = 7777; | ||
try { | ||
await closeServers(); | ||
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}`); | ||
} | ||
}); | ||
}); | ||
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}`); | ||
} | ||
}); | ||
}); |
@@ -10,2 +10,3 @@ { | ||
"checkJs": false, /* Report errors in .js files. */ | ||
"useUnknownInCatchVariables": false, | ||
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */ | ||
@@ -66,2 +67,2 @@ "declaration": true, /* Generates corresponding '.d.ts' file. */ | ||
] | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
18609
370
0