@zukhruf/testing 0.6.9 → 0.6.11
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.
- package/README.md +52 -0
- package/dist/http/http-server.d.ts.map +1 -1
- package/dist/http/http-server.js +9 -18
- package/dist/http/http-server.js.map +1 -1
- package/dist/preload/index.d.ts +7 -0
- package/dist/preload/index.d.ts.map +1 -0
- package/dist/preload/index.js +16 -0
- package/dist/preload/index.js.map +1 -0
- package/dist/preload/not-installed.d.ts +2 -0
- package/dist/preload/not-installed.d.ts.map +1 -0
- package/dist/preload/not-installed.js +18 -0
- package/dist/preload/not-installed.js.map +1 -0
- package/package.json +8 -2
package/README.md
CHANGED
|
@@ -25,6 +25,7 @@ Each area has its own import path, so a test loads only the drivers it uses. The
|
|
|
25
25
|
| `@zukhruf/testing/streams` | `StreamHarness` | |
|
|
26
26
|
| `@zukhruf/testing/fs` | `TemporaryFiles` | |
|
|
27
27
|
| `@zukhruf/testing/msw` | `Msw` | `msw` |
|
|
28
|
+
| `@zukhruf/testing/preload` | `notInstalled` | |
|
|
28
29
|
|
|
29
30
|
## Parameterized tests
|
|
30
31
|
|
|
@@ -256,6 +257,57 @@ spawn(command, args, {
|
|
|
256
257
|
});
|
|
257
258
|
```
|
|
258
259
|
|
|
260
|
+
## Preloads
|
|
261
|
+
|
|
262
|
+
A package can have optional peer dependencies. A project that does not use a part of the package does not install the peer of that part. This repository installs each peer. Thus a wrong import of a peer stays green here, and fails only in the project of a consumer.
|
|
263
|
+
|
|
264
|
+
`notInstalled(...packages)` makes installed packages look not installed, in one child process. Give its result to the `--import` flag of the child, then import your module in the child. The hook changes the resolution of the whole process, and the test process needs the packages. Thus the check runs in a child.
|
|
265
|
+
|
|
266
|
+
```ts
|
|
267
|
+
import spawn from 'nano-spawn';
|
|
268
|
+
|
|
269
|
+
import { notInstalled } from '@zukhruf/testing/preload';
|
|
270
|
+
|
|
271
|
+
test('the fs area loads without msw', async () => {
|
|
272
|
+
await spawn(process.execPath, [
|
|
273
|
+
'--import',
|
|
274
|
+
notInstalled('msw'),
|
|
275
|
+
'--input-type=module',
|
|
276
|
+
'--eval',
|
|
277
|
+
"await import('@zukhruf/testing/fs');",
|
|
278
|
+
]);
|
|
279
|
+
});
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
`notInstalled` returns the URL of a preload: a file of this package that Node.js runs before the entry point of the child. The query of the URL holds the names. The preload adds a `resolve` hook with `registerHooks` of `node:module`. The hook stops each specifier that is a name, or that starts with a name and `/`. Thus `notInstalled('msw')` also stops `msw/node`, and it does not stop `ms`. The hook gives each other specifier to the next hook. It stops a name for each importer, also for an installed package that depends on it: msw requires `ms`, so `notInstalled('ms')` also stops msw.
|
|
283
|
+
|
|
284
|
+
The hook throws the error that Node.js throws for a package that is not installed:
|
|
285
|
+
|
|
286
|
+
| Call | Error |
|
|
287
|
+
| --------------------- | ---------------------------------------------------------------------------------------- |
|
|
288
|
+
| `import('msw/node')` | `Cannot find package 'msw' imported from <path of the importer>`, `ERR_MODULE_NOT_FOUND` |
|
|
289
|
+
| `require('msw/node')` | `Cannot find module 'msw/node'`, `MODULE_NOT_FOUND` |
|
|
290
|
+
|
|
291
|
+
Thus code that reads `error.code` to find a peer that is not installed takes the same path as in the project of a consumer. The error of `require` has only the first line of the message of Node.js. It has no require stack, because the hook knows only the importer.
|
|
292
|
+
|
|
293
|
+
To use more preloads, give one `--import` flag for each. Node.js runs them in order, and each hook gives the specifiers that it does not stop to the hook before it:
|
|
294
|
+
|
|
295
|
+
```ts
|
|
296
|
+
spawn(process.execPath, [
|
|
297
|
+
'--import',
|
|
298
|
+
notInstalled('typescript'),
|
|
299
|
+
'--import',
|
|
300
|
+
notInstalled('@typescript/native'),
|
|
301
|
+
'--import',
|
|
302
|
+
yourOwnPreload,
|
|
303
|
+
entry,
|
|
304
|
+
]);
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
In `NODE_OPTIONS`, as `` `--import=${notInstalled('msw')}` ``, the preload also runs in each Node.js process that the child starts with its environment.
|
|
308
|
+
|
|
309
|
+
A call with no name does not compile. [ADR 0018](./docs/adr/0018-a-preload-is-a-file-of-the-package-and-its-url-gives-its-settings.md) records this design.
|
|
310
|
+
|
|
259
311
|
## Intercepted requests
|
|
260
312
|
|
|
261
313
|
`new Msw().intercept(...handlers)` starts an msw server in this process with the handlers, and returns an interception. The handle has `server`, `requests` and `cleanup`. Disposal closes the server.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http-server.d.ts","sourceRoot":"","sources":["../../src/http/http-server.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,KAAK,eAAe,EACpB,KAAK,MAAM,EACX,KAAK,cAAc,EAEpB,MAAM,WAAW,CAAC;AAEnB;;;GAGG;AACH,MAAM,MAAM,WAAW,GAAG,CACxB,OAAO,EAAE,eAAe,EACxB,QAAQ,EAAE,cAAc,KACrB,OAAO,CAAC;AAEb,MAAM,WAAW,gBAAiB,SAAQ,eAAe;IACvD,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,iEAAiE;IACjE,OAAO,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CAC9B;AAED,yEAAyE;AACzE,qBAAa,UAAU;IACf,KAAK,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"http-server.d.ts","sourceRoot":"","sources":["../../src/http/http-server.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,KAAK,eAAe,EACpB,KAAK,MAAM,EACX,KAAK,cAAc,EAEpB,MAAM,WAAW,CAAC;AAEnB;;;GAGG;AACH,MAAM,MAAM,WAAW,GAAG,CACxB,OAAO,EAAE,eAAe,EACxB,QAAQ,EAAE,cAAc,KACrB,OAAO,CAAC;AAEb,MAAM,WAAW,gBAAiB,SAAQ,eAAe;IACvD,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,iEAAiE;IACjE,OAAO,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CAC9B;AAED,yEAAyE;AACzE,qBAAa,UAAU;IACf,KAAK,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,gBAAgB,CAAC;CA2B7D"}
|
package/dist/http/http-server.js
CHANGED
|
@@ -5,14 +5,20 @@ import { createServer, } from 'node:http';
|
|
|
5
5
|
export class HttpServer {
|
|
6
6
|
async start(handler) {
|
|
7
7
|
await using resources = new AsyncDisposableStack();
|
|
8
|
-
const server =
|
|
9
|
-
|
|
8
|
+
const server = createServer((request, response) => {
|
|
9
|
+
void handler(request, response);
|
|
10
|
+
});
|
|
11
|
+
const listening = once(server, 'listening');
|
|
12
|
+
server.listen(0, '127.0.0.1');
|
|
13
|
+
await listening;
|
|
10
14
|
resources.defer(async () => {
|
|
11
15
|
const closed = server[Symbol.asyncDispose]();
|
|
12
16
|
server.closeAllConnections();
|
|
13
17
|
await closed;
|
|
14
18
|
});
|
|
15
|
-
const
|
|
19
|
+
const address = server.address();
|
|
20
|
+
assert(address && typeof address === 'object');
|
|
21
|
+
const origin = `http://127.0.0.1:${address.port}`;
|
|
16
22
|
const owned = resources.move();
|
|
17
23
|
const cleanup = () => owned.disposeAsync();
|
|
18
24
|
return {
|
|
@@ -23,19 +29,4 @@ export class HttpServer {
|
|
|
23
29
|
};
|
|
24
30
|
}
|
|
25
31
|
}
|
|
26
|
-
function serve(handler) {
|
|
27
|
-
return createServer((request, response) => {
|
|
28
|
-
void handler(request, response);
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
async function listen(server) {
|
|
32
|
-
const listening = once(server, 'listening');
|
|
33
|
-
server.listen(0, '127.0.0.1');
|
|
34
|
-
await listening;
|
|
35
|
-
}
|
|
36
|
-
function originOf(server) {
|
|
37
|
-
const address = server.address();
|
|
38
|
-
assert(address && typeof address === 'object');
|
|
39
|
-
return `http://127.0.0.1:${address.port}`;
|
|
40
|
-
}
|
|
41
32
|
//# sourceMappingURL=http-server.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http-server.js","sourceRoot":"","sources":["../../src/http/http-server.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,oBAAoB,CAAC;AACxC,OAAO,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AACnC,OAAO,EAIL,YAAY,GACb,MAAM,WAAW,CAAC;AAkBnB,yEAAyE;AACzE,MAAM,OAAO,UAAU;IACrB,KAAK,CAAC,KAAK,CAAC,OAAoB;QAC9B,YAAY,SAAS,GAAG,IAAI,oBAAoB,EAAE,CAAC;QACnD,MAAM,MAAM,GAAG,
|
|
1
|
+
{"version":3,"file":"http-server.js","sourceRoot":"","sources":["../../src/http/http-server.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,oBAAoB,CAAC;AACxC,OAAO,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AACnC,OAAO,EAIL,YAAY,GACb,MAAM,WAAW,CAAC;AAkBnB,yEAAyE;AACzE,MAAM,OAAO,UAAU;IACrB,KAAK,CAAC,KAAK,CAAC,OAAoB;QAC9B,YAAY,SAAS,GAAG,IAAI,oBAAoB,EAAE,CAAC;QACnD,MAAM,MAAM,GAAG,YAAY,CAAC,CAAC,OAAO,EAAE,QAAQ,EAAE,EAAE;YAChD,KAAK,OAAO,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;QAClC,CAAC,CAAC,CAAC;QACH,MAAM,SAAS,GAAG,IAAI,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;QAC5C,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,WAAW,CAAC,CAAC;QAC9B,MAAM,SAAS,CAAC;QAEhB,SAAS,CAAC,KAAK,CAAC,KAAK,IAAI,EAAE;YACzB,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,YAAY,CAAC,EAAE,CAAC;YAC7C,MAAM,CAAC,mBAAmB,EAAE,CAAC;YAC7B,MAAM,MAAM,CAAC;QACf,CAAC,CAAC,CAAC;QACH,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,EAAE,CAAC;QACjC,MAAM,CAAC,OAAO,IAAI,OAAO,OAAO,KAAK,QAAQ,CAAC,CAAC;QAC/C,MAAM,MAAM,GAAG,oBAAoB,OAAO,CAAC,IAAI,EAAE,CAAC;QAClD,MAAM,KAAK,GAAG,SAAS,CAAC,IAAI,EAAE,CAAC;QAC/B,MAAM,OAAO,GAAG,GAAG,EAAE,CAAC,KAAK,CAAC,YAAY,EAAE,CAAC;QAE3C,OAAO;YACL,MAAM;YACN,MAAM;YACN,OAAO;YACP,CAAC,MAAM,CAAC,YAAY,CAAC,EAAE,OAAO;SAC/B,CAAC;IACJ,CAAC;CACF"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The URL of a preload that makes each package, and each of its subpaths,
|
|
3
|
+
* look not installed in the process that loads it. Give it to the `--import`
|
|
4
|
+
* flag of a child process, once for each preload.
|
|
5
|
+
*/
|
|
6
|
+
export declare function notInstalled(...packages: [string, ...string[]]): string;
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/preload/index.ts"],"names":[],"mappings":"AAGA;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,GAAG,QAAQ,EAAE,CAAC,MAAM,EAAE,GAAG,MAAM,EAAE,CAAC,GAAG,MAAM,CAMvE"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { extname } from 'node:path';
|
|
2
|
+
import { fileURLToPath } from 'node:url';
|
|
3
|
+
/**
|
|
4
|
+
* The URL of a preload that makes each package, and each of its subpaths,
|
|
5
|
+
* look not installed in the process that loads it. Give it to the `--import`
|
|
6
|
+
* flag of a child process, once for each preload.
|
|
7
|
+
*/
|
|
8
|
+
export function notInstalled(...packages) {
|
|
9
|
+
// The preload next to this module: `.ts` when Node runs the source, `.js` when it runs the build.
|
|
10
|
+
const extension = extname(fileURLToPath(import.meta.url));
|
|
11
|
+
const preload = new URL(`./not-installed${extension}`, import.meta.url);
|
|
12
|
+
for (const name of packages)
|
|
13
|
+
preload.searchParams.append('package', name);
|
|
14
|
+
return preload.href;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/preload/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAEzC;;;;GAIG;AACH,MAAM,UAAU,YAAY,CAAC,GAAG,QAA+B;IAC7D,kGAAkG;IAClG,MAAM,SAAS,GAAG,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;IAC1D,MAAM,OAAO,GAAG,IAAI,GAAG,CAAC,kBAAkB,SAAS,EAAE,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACxE,KAAK,MAAM,IAAI,IAAI,QAAQ;QAAE,OAAO,CAAC,YAAY,CAAC,MAAM,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;IAC1E,OAAO,OAAO,CAAC,IAAI,CAAC;AACtB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"not-installed.d.ts","sourceRoot":"","sources":["../../src/preload/not-installed.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { registerHooks } from 'node:module';
|
|
2
|
+
import { fileURLToPath } from 'node:url';
|
|
3
|
+
/** The packages that `notInstalled` put in the query of this module's URL. */
|
|
4
|
+
const packages = new URL(import.meta.url).searchParams.getAll('package');
|
|
5
|
+
registerHooks({
|
|
6
|
+
resolve(specifier, context, nextResolve) {
|
|
7
|
+
const name = packages.find((hidden) => specifier === hidden || specifier.startsWith(`${hidden}/`));
|
|
8
|
+
// Only the entry point has no importer, and Node gives it as a file URL.
|
|
9
|
+
if (name === undefined || context.parentURL === undefined)
|
|
10
|
+
return nextResolve(specifier, context);
|
|
11
|
+
if (context.conditions.includes('require'))
|
|
12
|
+
throw Object.assign(new Error(`Cannot find module '${specifier}'`), {
|
|
13
|
+
code: 'MODULE_NOT_FOUND',
|
|
14
|
+
});
|
|
15
|
+
throw Object.assign(new Error(`Cannot find package '${name}' imported from ${fileURLToPath(context.parentURL)}`), { code: 'ERR_MODULE_NOT_FOUND' });
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
//# sourceMappingURL=not-installed.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"not-installed.js","sourceRoot":"","sources":["../../src/preload/not-installed.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAEzC,8EAA8E;AAC9E,MAAM,QAAQ,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,YAAY,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;AAEzE,aAAa,CAAC;IACZ,OAAO,CAAC,SAAS,EAAE,OAAO,EAAE,WAAW;QACrC,MAAM,IAAI,GAAG,QAAQ,CAAC,IAAI,CACxB,CAAC,MAAM,EAAE,EAAE,CAAC,SAAS,KAAK,MAAM,IAAI,SAAS,CAAC,UAAU,CAAC,GAAG,MAAM,GAAG,CAAC,CACvE,CAAC;QACF,yEAAyE;QACzE,IAAI,IAAI,KAAK,SAAS,IAAI,OAAO,CAAC,SAAS,KAAK,SAAS;YACvD,OAAO,WAAW,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;QACzC,IAAI,OAAO,CAAC,UAAU,CAAC,QAAQ,CAAC,SAAS,CAAC;YACxC,MAAM,MAAM,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,uBAAuB,SAAS,GAAG,CAAC,EAAE;gBAClE,IAAI,EAAE,kBAAkB;aACzB,CAAC,CAAC;QACL,MAAM,MAAM,CAAC,MAAM,CACjB,IAAI,KAAK,CACP,wBAAwB,IAAI,mBAAmB,aAAa,CAAC,OAAO,CAAC,SAAS,CAAC,EAAE,CAClF,EACD,EAAE,IAAI,EAAE,sBAAsB,EAAE,CACjC,CAAC;IACJ,CAAC;CACF,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zukhruf/testing",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.11",
|
|
4
4
|
"description": "Disposable fixtures for integration tests: Docker containers, database servers, SQLite and DuckDB databases, HTTP servers, streams, temporary directories and intercepted requests. Node's global setup and teardown remove what a killed test run left behind.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -116,6 +116,12 @@
|
|
|
116
116
|
"import": "./dist/msw/msw.js",
|
|
117
117
|
"default": "./dist/msw/msw.js"
|
|
118
118
|
},
|
|
119
|
+
"./preload": {
|
|
120
|
+
"@zukhruf/source": "./src/preload/index.ts",
|
|
121
|
+
"types": "./dist/preload/index.d.ts",
|
|
122
|
+
"import": "./dist/preload/index.js",
|
|
123
|
+
"default": "./dist/preload/index.js"
|
|
124
|
+
},
|
|
119
125
|
"./docker-global-setup": {
|
|
120
126
|
"@zukhruf/source": "./src/scripts/docker-global-setup.ts",
|
|
121
127
|
"types": "./dist/scripts/docker-global-setup.d.ts",
|
|
@@ -144,7 +150,7 @@
|
|
|
144
150
|
"pg": "^8.23.1"
|
|
145
151
|
},
|
|
146
152
|
"dependencies": {
|
|
147
|
-
"@zukhruf/docker": "^0.6.
|
|
153
|
+
"@zukhruf/docker": "^0.6.11",
|
|
148
154
|
"nano-spawn": "^2.1.0",
|
|
149
155
|
"p-memoize": "^8.0.0",
|
|
150
156
|
"p-retry": "^8.0.1"
|