@classytic/arc-testkit 0.1.0 → 0.1.1

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.
@@ -0,0 +1,29 @@
1
+ import { Connection } from "mongoose";
2
+ import { ArcModuleInput, CreateAppOptions } from "@classytic/arc/factory";
3
+ import { FastifyInstance } from "fastify";
4
+
5
+ //#region src/index.d.ts
6
+ interface TestkitContext {
7
+ connection: Connection;
8
+ mongoUri: string;
9
+ }
10
+ type ModulesInput = ArcModuleInput | readonly ArcModuleInput[] | ((ctx: TestkitContext) => ArcModuleInput | readonly ArcModuleInput[]);
11
+ interface BootModuleAppOptions {
12
+ /**
13
+ * Use a single-node replica set instead of a standalone mongod. Required
14
+ * when the domain under test uses multi-document transactions.
15
+ */
16
+ replset?: boolean;
17
+ /** Merged over the testkit defaults (`preset: 'testing'`, `auth: false`). */
18
+ app?: Partial<CreateAppOptions>;
19
+ }
20
+ interface ModuleTestApp {
21
+ app: FastifyInstance;
22
+ connection: Connection;
23
+ mongoUri: string;
24
+ /** Close app → disconnect mongoose → stop the in-memory server. Idempotent. */
25
+ close(): Promise<void>;
26
+ }
27
+ declare function bootModuleApp(modules: ModulesInput, options?: BootModuleAppOptions): Promise<ModuleTestApp>;
28
+ //#endregion
29
+ export { BootModuleAppOptions, ModuleTestApp, ModulesInput, TestkitContext, bootModuleApp };
package/dist/index.js ADDED
@@ -0,0 +1,62 @@
1
+ import { MongoMemoryReplSet, MongoMemoryServer } from "mongodb-memory-server";
2
+ import mongoose from "mongoose";
3
+ import { createApp } from "@classytic/arc/factory";
4
+ //#region src/index.ts
5
+ /**
6
+ * @classytic/spine-testkit — boots a REAL arc app around the module(s) under
7
+ * test on in-memory MongoDB (the Next.js `nextTestSetup` fixture-app pattern,
8
+ * backend-shaped). The contributor contract: if your module boots green here,
9
+ * it composes into any arc vertical.
10
+ *
11
+ * const t = await bootModuleApp(({ connection }) => [
12
+ * createAccountingModule({ connection, country, permissions }),
13
+ * ]);
14
+ * const res = await t.app.inject({ method: 'GET', url: '/accounting/accounts' });
15
+ * await t.close();
16
+ *
17
+ * Modules usually need the live DB connection in their deps, so the primary
18
+ * input is a FACTORY receiving `{ connection, mongoUri }` — mirroring the
19
+ * AppContext convention (wiki/modules.md). Plain module lists also accepted.
20
+ */
21
+ async function bootModuleApp(modules, options = {}) {
22
+ const server = options.replset ? await MongoMemoryReplSet.create({ replSet: { count: 1 } }) : await MongoMemoryServer.create();
23
+ const mongoUri = server.getUri();
24
+ if (mongoose.connection.readyState !== 0) await mongoose.disconnect();
25
+ await mongoose.connect(mongoUri);
26
+ const connection = mongoose.connection;
27
+ const resolved = typeof modules === "function" ? modules({
28
+ connection,
29
+ mongoUri
30
+ }) : modules;
31
+ const list = Array.isArray(resolved) ? resolved : [resolved];
32
+ let app;
33
+ try {
34
+ app = await createApp({
35
+ preset: "testing",
36
+ auth: false,
37
+ sensible: false,
38
+ ...options.app,
39
+ modules: list
40
+ });
41
+ await app.ready();
42
+ } catch (err) {
43
+ await mongoose.disconnect().catch(() => void 0);
44
+ await server.stop().catch(() => void 0);
45
+ throw err;
46
+ }
47
+ let closed = false;
48
+ return {
49
+ app,
50
+ connection,
51
+ mongoUri,
52
+ close: async () => {
53
+ if (closed) return;
54
+ closed = true;
55
+ await app.close().catch(() => void 0);
56
+ if (mongoose.connection.readyState !== 0) await mongoose.disconnect().catch(() => void 0);
57
+ await server.stop().catch(() => void 0);
58
+ }
59
+ };
60
+ }
61
+ //#endregion
62
+ export { bootModuleApp };
package/package.json CHANGED
@@ -1,65 +1,40 @@
1
1
  {
2
2
  "name": "@classytic/arc-testkit",
3
- "version": "0.1.0",
4
- "description": "Boot a real @classytic/arc app around the module(s) under test on in-memory MongoDB — the ecosystem test harness. If your arc module boots green here, it composes into any arc host.",
3
+ "version": "0.1.1",
4
+ "description": "Test harness for @classytic arc modules — boots a real arc app around the module under test on in-memory MongoDB. If your module boots green here, it composes.",
5
5
  "type": "module",
6
+ "license": "UNLICENSED",
7
+ "sideEffects": false,
6
8
  "exports": {
7
9
  ".": {
8
- "types": "./dist/index.d.mts",
9
- "default": "./dist/index.mjs"
10
- }
10
+ "types": "./dist/index.d.ts",
11
+ "default": "./dist/index.js"
12
+ },
13
+ "./package.json": "./package.json"
11
14
  },
12
- "main": "./dist/index.mjs",
13
- "types": "./dist/index.d.mts",
14
- "sideEffects": false,
15
15
  "files": [
16
16
  "dist",
17
- "README.md"
17
+ "README.md",
18
+ "CHANGELOG.md"
18
19
  ],
19
- "publishConfig": {
20
- "access": "public"
21
- },
22
- "engines": {
23
- "node": ">=22"
24
- },
25
20
  "scripts": {
26
21
  "build": "tsdown",
27
22
  "typecheck": "tsc --noEmit",
28
- "lint": "biome check src/",
29
- "prepublishOnly": "node ../../scripts/prepublish-gate.mjs"
23
+ "clean": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\""
24
+ },
25
+ "dependencies": {
26
+ "mongodb-memory-server": "^10.2.3"
30
27
  },
31
28
  "peerDependencies": {
32
29
  "@classytic/arc": ">=2.20.0",
33
- "@classytic/mongokit": ">=3.19.0",
34
- "fastify": ">=5.0.0",
35
- "mongodb-memory-server": ">=10.0.0",
36
30
  "mongoose": ">=9.4.1"
37
31
  },
38
- "peerDependenciesMeta": {
39
- "@classytic/mongokit": {
40
- "optional": true
41
- },
42
- "mongodb-memory-server": {
43
- "optional": true
44
- },
45
- "mongoose": {
46
- "optional": true
47
- }
48
- },
49
- "keywords": [
50
- "arc",
51
- "fastify",
52
- "testing",
53
- "test-harness",
54
- "mongodb",
55
- "in-memory",
56
- "classytic"
57
- ],
58
- "license": "MIT",
59
- "author": "Classytic",
60
- "repository": {
61
- "type": "git",
62
- "url": "https://github.com/classytic/arc-ecosystem.git",
63
- "directory": "packages/arc-testkit"
32
+ "devDependencies": {
33
+ "@classytic/arc": "^2.20.0",
34
+ "@types/node": "^24.3.0",
35
+ "mongoose": "^9.7.2",
36
+ "tsdown": "^0.14.2",
37
+ "typescript": "^5.8.2",
38
+ "fastify": "^5.8.4"
64
39
  }
65
40
  }
package/README.md DELETED
@@ -1,65 +0,0 @@
1
- # @classytic/arc-testkit
2
-
3
- > Boot a real [`@classytic/arc`](https://www.npmjs.com/package/@classytic/arc) app around the module(s) under test, on a real (in-memory) database.
4
-
5
- The ecosystem's contributor contract for the `@classytic/arc-*` module fleet: **if your module boots green here, it composes into any arc host.**
6
-
7
- ## Install
8
-
9
- ```bash
10
- npm i -D @classytic/arc-testkit @classytic/mongokit mongodb-memory-server
11
- ```
12
-
13
- `@classytic/mongokit` + `mongodb-memory-server` are **optional peers** — needed only for the default backend, and dynamically imported so they never enter a production bundle. Pass your own `backend` to skip them entirely.
14
-
15
- ## Usage
16
-
17
- ```ts
18
- import { bootModuleApp } from '@classytic/arc-testkit';
19
- import { createAccountingModule } from '@classytic/arc-accounting';
20
-
21
- const t = await bootModuleApp(({ connection }) => [
22
- createAccountingModule({ connection, permissions }),
23
- ]);
24
-
25
- const res = await t.app.inject({ method: 'GET', url: '/accounting/accounts' });
26
- expect(res.statusCode).toBe(200);
27
-
28
- await t.close();
29
- ```
30
-
31
- The module input is a factory receiving `{ connection, mongoUri }` (or a plain module / array). It boots arc with `preset: 'testing'`, `auth: false` — override via `options.app`.
32
-
33
- ## Transactions
34
-
35
- Modules that open multi-document transactions need a replica set:
36
-
37
- ```ts
38
- const t = await bootModuleApp(({ connection }) => [createReservationModule({ connection, permissions })],
39
- { replset: true });
40
- ```
41
-
42
- ## Bring your own backend
43
-
44
- The default backend is `mongoMemoryBackend()` from `@classytic/mongokit/testkit`. Override it to point at a shared server, a real Atlas cluster, or a different kit's backend — anything that satisfies the `TestBackend` shape (`setup() → { ctx: { connection, uri }, teardown }`):
45
-
46
- ```ts
47
- import { mongoMemoryBackend } from '@classytic/mongokit/testkit';
48
-
49
- const t = await bootModuleApp(mods, {
50
- backend: mongoMemoryBackend({ uri: process.env.ATLAS_TEST_URI }), // real cluster
51
- });
52
- ```
53
-
54
- ## API
55
-
56
- | Export | |
57
- | --- | --- |
58
- | `bootModuleApp(modules, options?)` | boot arc around modules on a backend; returns `{ app, connection, mongoUri, close }` |
59
- | `TestBackend` | the backend seam — `setup() → { ctx: { connection, uri }, teardown }` |
60
- | `TestkitContext` | what a module factory receives: `{ connection, mongoUri }` |
61
- | `BootModuleAppOptions` | `{ replset?, app?, backend? }` |
62
-
63
- ## License
64
-
65
- MIT © Classytic
package/dist/index.d.mts DELETED
@@ -1,54 +0,0 @@
1
- import { ArcModuleInput, CreateAppOptions } from "@classytic/arc/factory";
2
- import { FastifyInstance } from "fastify";
3
- import { Connection } from "mongoose";
4
-
5
- //#region src/index.d.ts
6
- /** The context a backend yields — a live connection + the URI it connects to. */
7
- interface BackendContext {
8
- connection: Connection;
9
- uri: string;
10
- }
11
- /**
12
- * A pluggable database backend. Structurally satisfied by
13
- * `@classytic/mongokit/testkit`'s `mongoMemoryBackend()` — no import of it in
14
- * the type, so arc-testkit never hard-depends on the kit (one-way: arc-testkit
15
- * consumes a backend by shape).
16
- */
17
- interface TestBackend {
18
- setup(): Promise<{
19
- ctx: BackendContext;
20
- teardown: () => Promise<void>;
21
- }>;
22
- }
23
- /** What a module factory receives — the live connection + its URI. */
24
- interface TestkitContext {
25
- connection: Connection;
26
- mongoUri: string;
27
- }
28
- type ModulesInput = ArcModuleInput | readonly ArcModuleInput[] | ((ctx: TestkitContext) => ArcModuleInput | readonly ArcModuleInput[] | Promise<ArcModuleInput | readonly ArcModuleInput[]>);
29
- interface BootModuleAppOptions {
30
- /**
31
- * Use a single-node replica set instead of a standalone mongod. Required when
32
- * the module under test opens multi-document transactions. Ignored when a
33
- * custom `backend` is supplied. Default false.
34
- */
35
- replset?: boolean;
36
- /** Merged over the testkit defaults (`preset: 'testing'`, `auth: false`). */
37
- app?: Partial<CreateAppOptions>;
38
- /**
39
- * Override the database backend. Default: `mongoMemoryBackend()` from
40
- * `@classytic/mongokit/testkit`. Supply your own to point at a shared server,
41
- * a real Atlas cluster, or a different kit's backend.
42
- */
43
- backend?: TestBackend;
44
- }
45
- interface ModuleTestApp {
46
- app: FastifyInstance;
47
- connection: Connection;
48
- mongoUri: string;
49
- /** Close app → tear the backend down (disconnect + stop server). Idempotent. */
50
- close(): Promise<void>;
51
- }
52
- declare function bootModuleApp(modules: ModulesInput, options?: BootModuleAppOptions): Promise<ModuleTestApp>;
53
- //#endregion
54
- export { BackendContext, BootModuleAppOptions, ModuleTestApp, ModulesInput, TestBackend, TestkitContext, bootModuleApp };
package/dist/index.mjs DELETED
@@ -1,73 +0,0 @@
1
- import { createApp } from "@classytic/arc/factory";
2
- //#region src/index.ts
3
- /**
4
- * `@classytic/arc-testkit` — boot a REAL arc app around the module(s) under
5
- * test, on a real (in-memory) database. The ecosystem's contributor contract:
6
- * **if your module boots green here, it composes into any arc host.**
7
- *
8
- * ```ts
9
- * const t = await bootModuleApp(({ connection }) => [
10
- * createAccountingModule({ connection, permissions }),
11
- * ]);
12
- * const res = await t.app.inject({ method: 'GET', url: '/accounting/accounts' });
13
- * await t.close();
14
- * ```
15
- *
16
- * This is a thin generic core: `bootModuleApp` resolves the module list, opens a
17
- * database via a pluggable **backend**, and boots arc around it. The default
18
- * backend is `mongoMemoryBackend()` from `@classytic/mongokit/testkit` — loaded
19
- * via dynamic `import()` so `@classytic/mongokit` + `mongodb-memory-server` stay
20
- * OPTIONAL peers (needed only when you use the default backend). Pass your own
21
- * `backend` — e.g. one pointed at a shared server, or a real Atlas cluster — to
22
- * override it.
23
- *
24
- * Modules usually need the live DB connection in their deps, so the primary
25
- * input is a FACTORY receiving `{ connection, mongoUri }`. Plain module lists
26
- * are also accepted.
27
- */
28
- /** Load the default backend lazily so mongokit + memory-server stay optional. */
29
- async function defaultBackend(replset) {
30
- try {
31
- const { mongoMemoryBackend } = await import("@classytic/mongokit/testkit");
32
- return mongoMemoryBackend({ replset });
33
- } catch (err) {
34
- throw new Error(`[arc-testkit] the default backend needs \`@classytic/mongokit\` (>=3.19.0) and \`mongodb-memory-server\` installed as devDependencies. Install them, or pass your own \`backend\` to bootModuleApp. Original error: ${err.message}`);
35
- }
36
- }
37
- async function bootModuleApp(modules, options = {}) {
38
- const { ctx, teardown } = await (options.backend ?? await defaultBackend(options.replset ?? false)).setup();
39
- const testkitCtx = {
40
- connection: ctx.connection,
41
- mongoUri: ctx.uri
42
- };
43
- const resolved = typeof modules === "function" ? await modules(testkitCtx) : modules;
44
- const list = Array.isArray(resolved) ? resolved : [resolved];
45
- let app;
46
- try {
47
- app = await createApp({
48
- preset: "testing",
49
- auth: false,
50
- sensible: false,
51
- ...options.app,
52
- modules: list
53
- });
54
- await app.ready();
55
- } catch (err) {
56
- await teardown().catch(() => void 0);
57
- throw err;
58
- }
59
- let closed = false;
60
- return {
61
- app,
62
- connection: ctx.connection,
63
- mongoUri: ctx.uri,
64
- close: async () => {
65
- if (closed) return;
66
- closed = true;
67
- await app.close().catch(() => void 0);
68
- await teardown();
69
- }
70
- };
71
- }
72
- //#endregion
73
- export { bootModuleApp };