@prairielearn/express-test-utils 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
File without changes
@@ -0,0 +1,9 @@
1
+ /// <reference types="node" />
2
+ import * as express from 'express';
3
+ import { Server } from 'node:http';
4
+ export interface WithServerContext {
5
+ server: Server;
6
+ port: number;
7
+ url: string;
8
+ }
9
+ export declare function withServer(app: express.Express, fn: (ctx: WithServerContext) => Promise<void>): Promise<void>;
package/dist/index.js ADDED
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.withServer = void 0;
4
+ async function withServer(app, fn) {
5
+ const server = app.listen();
6
+ await new Promise((resolve, reject) => {
7
+ server.on('listening', () => resolve());
8
+ server.on('error', (err) => reject(err));
9
+ });
10
+ try {
11
+ await fn({
12
+ server,
13
+ port: getServerPort(server),
14
+ url: `http://localhost:${getServerPort(server)}`,
15
+ });
16
+ }
17
+ finally {
18
+ server.close();
19
+ }
20
+ }
21
+ exports.withServer = withServer;
22
+ function getServerPort(server) {
23
+ const address = server.address();
24
+ // istanbul ignore next
25
+ if (!address)
26
+ throw new Error('Server is not listening');
27
+ // istanbul ignore next
28
+ if (typeof address === 'string')
29
+ throw new Error('Server is listening on a pipe');
30
+ return address.port;
31
+ }
32
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AASO,KAAK,UAAU,UAAU,CAC9B,GAAoB,EACpB,EAA6C;IAE7C,MAAM,MAAM,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC;IAE5B,MAAM,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QAC1C,MAAM,CAAC,EAAE,CAAC,WAAW,EAAE,GAAG,EAAE,CAAC,OAAO,EAAE,CAAC,CAAC;QACxC,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC;IAC3C,CAAC,CAAC,CAAC;IAEH,IAAI;QACF,MAAM,EAAE,CAAC;YACP,MAAM;YACN,IAAI,EAAE,aAAa,CAAC,MAAM,CAAC;YAC3B,GAAG,EAAE,oBAAoB,aAAa,CAAC,MAAM,CAAC,EAAE;SACjD,CAAC,CAAC;KACJ;YAAS;QACR,MAAM,CAAC,KAAK,EAAE,CAAC;KAChB;AACH,CAAC;AApBD,gCAoBC;AAED,SAAS,aAAa,CAAC,MAAc;IACnC,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,EAAE,CAAC;IAEjC,uBAAuB;IACvB,IAAI,CAAC,OAAO;QAAE,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;IAEzD,uBAAuB;IACvB,IAAI,OAAO,OAAO,KAAK,QAAQ;QAAE,MAAM,IAAI,KAAK,CAAC,+BAA+B,CAAC,CAAC;IAElF,OAAO,OAAO,CAAC,IAAI,CAAC;AACtB,CAAC"}
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "@prairielearn/express-test-utils",
3
+ "version": "1.0.0",
4
+ "main": "dist/index.js",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/PrairieLearn/PrairieLearn.git",
8
+ "directory": "packages/express-test-utils"
9
+ },
10
+ "scripts": {
11
+ "build": "tsc",
12
+ "dev": "tsc --watch --preserveWatchOutput"
13
+ },
14
+ "dependencies": {
15
+ "@types/express": "^4.17.20"
16
+ }
17
+ }
package/src/index.ts ADDED
@@ -0,0 +1,42 @@
1
+ import * as express from 'express';
2
+ import { Server } from 'node:http';
3
+
4
+ export interface WithServerContext {
5
+ server: Server;
6
+ port: number;
7
+ url: string;
8
+ }
9
+
10
+ export async function withServer(
11
+ app: express.Express,
12
+ fn: (ctx: WithServerContext) => Promise<void>,
13
+ ) {
14
+ const server = app.listen();
15
+
16
+ await new Promise<void>((resolve, reject) => {
17
+ server.on('listening', () => resolve());
18
+ server.on('error', (err) => reject(err));
19
+ });
20
+
21
+ try {
22
+ await fn({
23
+ server,
24
+ port: getServerPort(server),
25
+ url: `http://localhost:${getServerPort(server)}`,
26
+ });
27
+ } finally {
28
+ server.close();
29
+ }
30
+ }
31
+
32
+ function getServerPort(server: Server): number {
33
+ const address = server.address();
34
+
35
+ // istanbul ignore next
36
+ if (!address) throw new Error('Server is not listening');
37
+
38
+ // istanbul ignore next
39
+ if (typeof address === 'string') throw new Error('Server is listening on a pipe');
40
+
41
+ return address.port;
42
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "extends": "@prairielearn/tsconfig/tsconfig.package.json",
3
+ "compilerOptions": {
4
+ "outDir": "./dist",
5
+ "rootDir": "./src",
6
+ "types": ["node"]
7
+ },
8
+ "ts-node": {
9
+ "transpileOnly": true
10
+ }
11
+ }