@bielik/express-test-helpers
Advanced tools
Comparing version 1.2.1 to 1.2.2
/// <reference types="node" /> | ||
import express from "express"; | ||
import http from "http"; | ||
import express from 'express'; | ||
import http from 'http'; | ||
export interface IPortAndApp { | ||
@@ -23,3 +23,3 @@ app: express.Express; | ||
*/ | ||
export declare function closeServers(): Promise<void[]>; | ||
export declare function closeServers(): Promise<void>; | ||
//# sourceMappingURL=index.d.ts.map |
92
index.js
@@ -5,3 +5,3 @@ "use strict"; | ||
var tslib_1 = require("tslib"); | ||
var express_1 = (0, tslib_1.__importDefault)(require("express")); | ||
var express_1 = tslib_1.__importDefault(require("express")); | ||
exports.servers = []; | ||
@@ -13,17 +13,36 @@ /** | ||
function createServer(port) { | ||
return new Promise(function (resolve, reject) { | ||
try { | ||
var app_1 = (0, express_1.default)(); | ||
var server_1 = app_1.listen(port, function () { | ||
resolve({ app: app_1, port: port, server: server_1 }); | ||
exports.servers.push({ server: server_1, app: app_1, port: port }); | ||
console.log("Created test server on port " + port); | ||
}); | ||
} | ||
catch (e) { | ||
reject(e); | ||
} | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
return tslib_1.__generator(this, function (_a) { | ||
return [2 /*return*/, new Promise(function (resolve, reject) { | ||
try { | ||
var app_1 = (0, express_1.default)(); | ||
var server_1 = app_1.listen(port, function () { | ||
exports.servers.push({ server: server_1, app: app_1, port: port }); | ||
resolve({ app: app_1, port: port, server: server_1 }); | ||
}); | ||
} | ||
catch (e) { | ||
reject(e); | ||
} | ||
})]; | ||
}); | ||
}); | ||
} | ||
exports.createServer = createServer; | ||
function _close(server) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, new Promise(function (resolve, reject) { | ||
server.close(function (error) { | ||
return error ? reject(error) : resolve(undefined); | ||
}); | ||
})]; | ||
case 1: | ||
_a.sent(); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
} | ||
/** | ||
@@ -34,11 +53,23 @@ * @param {Number} portToClose | ||
function closeServer(portToClose) { | ||
return new Promise(function (resolve) { | ||
exports.servers.forEach(function (_a) { | ||
var server = _a.server, port = _a.port; | ||
if (port === portToClose && server.close) { | ||
return server.close(function () { | ||
resolve(); | ||
}); | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var _i, servers_1, serverAndPort; | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
_i = 0, servers_1 = exports.servers; | ||
_a.label = 1; | ||
case 1: | ||
if (!(_i < servers_1.length)) return [3 /*break*/, 4]; | ||
serverAndPort = servers_1[_i]; | ||
if (!(serverAndPort.port === portToClose && serverAndPort.server.close)) return [3 /*break*/, 3]; | ||
return [4 /*yield*/, _close(serverAndPort.server)]; | ||
case 2: | ||
_a.sent(); | ||
exports.servers.splice(exports.servers.findIndex(function (s) { return s.port === portToClose; }), 1); | ||
_a.label = 3; | ||
case 3: | ||
_i++; | ||
return [3 /*break*/, 1]; | ||
case 4: return [2 /*return*/]; | ||
} | ||
resolve(); | ||
}); | ||
@@ -52,8 +83,19 @@ }); | ||
function closeServers() { | ||
var promises = exports.servers.map(function (_a) { | ||
var port = _a.port; | ||
return closeServer(port); | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var promises; | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
promises = exports.servers.map(function (_a) { | ||
var port = _a.port; | ||
return closeServer(port); | ||
}); | ||
return [4 /*yield*/, Promise.all(promises)]; | ||
case 1: | ||
_a.sent(); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
return Promise.all(promises); | ||
} | ||
exports.closeServers = closeServers; |
42
index.ts
@@ -1,3 +0,3 @@ | ||
import express from "express"; | ||
import http from "http"; | ||
import express from 'express'; | ||
import http from 'http'; | ||
@@ -16,3 +16,3 @@ export interface IPortAndApp { | ||
*/ | ||
export function createServer(port: number): Promise<IPortAndApp> { | ||
export async function createServer(port: number): Promise<IPortAndApp> { | ||
return new Promise((resolve, reject) => { | ||
@@ -22,5 +22,4 @@ try { | ||
let server = app.listen(port, () => { | ||
servers.push({ server, app, port }); | ||
resolve({ app, port, server }); | ||
servers.push({ server, app, port }); | ||
console.log(`Created test server on port ${port}`); | ||
}); | ||
@@ -33,2 +32,10 @@ } catch (e) { | ||
async function _close(server: http.Server): Promise<void> { | ||
await new Promise((resolve, reject) => { | ||
server.close((error) => { | ||
return error ? reject(error) : resolve(undefined); | ||
}); | ||
}); | ||
} | ||
/** | ||
@@ -38,13 +45,12 @@ * @param {Number} portToClose | ||
*/ | ||
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(); | ||
}); | ||
}); | ||
export async function closeServer(portToClose: number): Promise<void> { | ||
for (const serverAndPort of servers) { | ||
if (serverAndPort.port === portToClose && serverAndPort.server.close) { | ||
await _close(serverAndPort.server); | ||
servers.splice( | ||
servers.findIndex((s) => s.port === portToClose), | ||
1 | ||
); | ||
} | ||
} | ||
} | ||
@@ -55,7 +61,7 @@ | ||
*/ | ||
export function closeServers(): Promise<void[]> { | ||
export async function closeServers(): Promise<void> { | ||
const promises = servers.map(({ port }) => { | ||
return closeServer(port); | ||
}); | ||
return Promise.all(promises); | ||
await Promise.all(promises); | ||
} |
{ | ||
"name": "@bielik/express-test-helpers", | ||
"version": "1.2.1", | ||
"version": "1.2.2", | ||
"description": "Unit test helper functions for node/express", | ||
@@ -13,4 +13,3 @@ "main": "index.js", | ||
"@bielik/request": "1.3.0", | ||
"express": "4.16.3", | ||
"tslib": "1.10.0" | ||
"express": "4.16.3" | ||
}, | ||
@@ -22,3 +21,4 @@ "devDependencies": { | ||
"chai": "4.1.2", | ||
"mocha": "5.2.0" | ||
"mocha": "5.2.0", | ||
"typescript": "^4.6.2" | ||
}, | ||
@@ -25,0 +25,0 @@ "repository": { |
48
tests.js
@@ -6,10 +6,9 @@ "use strict"; | ||
var chai_1 = require("chai"); | ||
var request_1 = (0, tslib_1.__importDefault)(require("@bielik/request")); | ||
var request_1 = tslib_1.__importDefault(require("@bielik/request")); | ||
var index_1 = require("./index"); | ||
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 () { | ||
describe('server creation', function () { | ||
it('should create and close a server', function () { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var payload, _a, app, port, uri, body, e_1; | ||
return (0, tslib_1.__generator)(this, function (_b) { | ||
return tslib_1.__generator(this, function (_b) { | ||
switch (_b.label) { | ||
@@ -19,9 +18,9 @@ case 0: | ||
this.timeout(30e3); | ||
payload = "body"; | ||
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) { | ||
(0, chai_1.expect)(index_1.servers.length).to.equal(1); | ||
uri = "http://localhost:".concat(port, "/any"); | ||
app.get('*', function (req, res) { | ||
res.send(payload); | ||
@@ -42,7 +41,7 @@ }); | ||
_b.sent(); | ||
(0, 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(); | ||
(0, 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:".concat(port)); | ||
return [3 /*break*/, 7]; | ||
@@ -54,6 +53,6 @@ case 7: return [2 /*return*/]; | ||
}); | ||
it("should create and close multiple servers", function () { | ||
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () { | ||
it('should create and close multiple servers', function () { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var port, apps, test, testPort, uri, e_2; | ||
return (0, tslib_1.__generator)(this, function (_a) { | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
@@ -63,2 +62,3 @@ case 0: | ||
port = 7777; | ||
(0, chai_1.expect)(index_1.servers.length).to.equal(0); | ||
return [4 /*yield*/, Promise.all([ | ||
@@ -70,13 +70,13 @@ (0, index_1.createServer)(port--), | ||
apps = _a.sent(); | ||
(0, chai_1.expect)(index_1.servers.length).to.equal((serverCount += 2)); | ||
(0, chai_1.expect)(index_1.servers.length).to.equal(apps.length); | ||
test = function (_a) { | ||
var app = _a.app, port = _a.port; | ||
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var payload, uri, body; | ||
return (0, tslib_1.__generator)(this, function (_b) { | ||
return tslib_1.__generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
payload = "body"; | ||
uri = "http://localhost:" + port + "/any"; | ||
app.get("*", function (req, res) { | ||
payload = 'body'; | ||
uri = "http://localhost:".concat(port, "/any"); | ||
app.get('*', function (req, res) { | ||
res.send(payload); | ||
@@ -103,11 +103,11 @@ }); | ||
_a.trys.push([4, 6, , 7]); | ||
uri = "http://localhost:" + testPort + "/any"; | ||
uri = "http://localhost:".concat(testPort, "/any"); | ||
return [4 /*yield*/, (0, request_1.default)({ uri: uri })]; | ||
case 5: | ||
_a.sent(); | ||
(0, 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(); | ||
(0, chai_1.expect)(e_2.message).to.equal("connect ECONNREFUSED 127.0.0.1:" + testPort); | ||
(0, chai_1.expect)(e_2.code).to.equal('ECONNREFUSED'); | ||
return [3 /*break*/, 7]; | ||
@@ -114,0 +114,0 @@ case 7: return [2 /*return*/]; |
36
tests.ts
//@ts-check | ||
import { expect } from "chai"; | ||
import express from "express"; | ||
import request from "@bielik/request"; | ||
import { createServer, closeServer, closeServers, servers } from "./index"; | ||
import { expect } from 'chai'; | ||
import express from 'express'; | ||
import request from '@bielik/request'; | ||
import { createServer, closeServer, closeServers, servers } from './index'; | ||
describe("server creation", function () { | ||
let serverCount = 0; | ||
it("should create and close a server", async function () { | ||
describe('server creation', function () { | ||
it('should create and close a server', async function () { | ||
expect(servers.length).to.equal(0); | ||
this.timeout(30e3); | ||
const payload = "body"; | ||
const payload = 'body'; | ||
const { app, port } = await createServer(7777); | ||
expect(servers.length).to.equal(++serverCount); | ||
expect(servers.length).to.equal(1); | ||
const uri = `http://localhost:${port}/any`; | ||
app.get("*", (req, res) => { | ||
app.get('*', (req, res) => { | ||
res.send(payload); | ||
@@ -28,3 +26,3 @@ }); | ||
await request({ uri }); | ||
expect("request after server closed").to.equal("should have thrown"); | ||
expect('request after server closed').to.equal('should have thrown'); | ||
} catch (e) { | ||
@@ -35,6 +33,6 @@ expect(e.message).to.equal(`connect ECONNREFUSED 127.0.0.1:${port}`); | ||
it("should create and close multiple servers", async function () { | ||
it('should create and close multiple servers', async function () { | ||
this.timeout(30e3); | ||
let port = 7777; | ||
expect(servers.length).to.equal(0); | ||
const apps = await Promise.all([ | ||
@@ -44,3 +42,3 @@ createServer(port--), | ||
]); | ||
expect(servers.length).to.equal((serverCount += 2)); | ||
expect(servers.length).to.equal(apps.length); | ||
let test = async function ({ | ||
@@ -53,5 +51,5 @@ app, | ||
}) { | ||
let payload = "body"; | ||
let payload = 'body'; | ||
let uri = `http://localhost:${port}/any`; | ||
app.get("*", (req, res) => { | ||
app.get('*', (req, res) => { | ||
res.send(payload); | ||
@@ -72,7 +70,7 @@ }); | ||
await request({ uri }); | ||
expect("request after server closed").to.equal("should have thrown"); | ||
expect('request after server closed').to.equal('should have thrown'); | ||
} catch (e) { | ||
expect(e.message).to.equal(`connect ECONNREFUSED 127.0.0.1:${testPort}`); | ||
expect(e.code).to.equal('ECONNREFUSED'); | ||
} | ||
}); | ||
}); |
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
20841
2
11
417
6
- Removedtslib@1.10.0