@rvoh/psychic-spec-helpers 3.1.0 → 3.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.
@@ -1,14 +1,28 @@
1
- // eslint-disable-next-line
2
- const _server = undefined;
1
+ // The booted spec server is cached for the lifetime of the worker process so it
2
+ // is created and booted exactly once, then reused by every spec — instead of
3
+ // re-booting a brand-new PsychicServer for each spec, which re-runs application
4
+ // initialization and churns database/websocket connections (a significant
5
+ // source of slow suites and flaky, connection-exhaustion-driven failures).
6
+ //
7
+ // The cache lives on `globalThis` rather than in a module-scoped variable
8
+ // because isolating test runners (e.g. Vitest with the default `isolate: true`)
9
+ // reset the module registry between spec files. A module-scoped cache would be
10
+ // discarded at every file boundary, forcing a fresh boot per file; `globalThis`
11
+ // persists for the whole worker process, so the server boots once per worker.
12
+ const CACHED_SPEC_SERVER_KEY = Symbol.for('@rvoh/psychic-spec-helpers:cachedSpecServer');
3
13
  // eslint-disable-next-line
4
14
  export default async function createPsychicServer(PsychicServer) {
5
15
  // eslint-disable-next-line
6
- if (_server)
7
- return _server;
16
+ const store = globalThis;
17
+ // eslint-disable-next-line
18
+ if (store[CACHED_SPEC_SERVER_KEY])
19
+ return store[CACHED_SPEC_SERVER_KEY];
8
20
  // eslint-disable-next-line
9
21
  const server = new PsychicServer();
10
22
  // eslint-disable-next-line
11
23
  await server.boot();
12
24
  // eslint-disable-next-line
25
+ store[CACHED_SPEC_SERVER_KEY] = server;
26
+ // eslint-disable-next-line
13
27
  return server;
14
28
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "@rvoh/psychic-spec-helpers",
4
- "version": "3.1.0",
4
+ "version": "3.1.1",
5
5
  "description": "psychic framework spec helpers",
6
6
  "author": "RVO Health",
7
7
  "repository": {
@@ -80,5 +80,5 @@
80
80
  "dotenv": "^17.2.3",
81
81
  "pluralize-esm": "^9.0.5"
82
82
  },
83
- "packageManager": "pnpm@10.24.0+sha512.01ff8ae71b4419903b65c60fb2dc9d34cf8bb6e06d03bde112ef38f7a34d6904c424ba66bea5cdcf12890230bf39f9580473140ed9c946fef328b6e5238a345a"
83
+ "packageManager": "pnpm@11.1.3+sha512.c85357fe17ca12dd23dd7071822666dfd7e3cb76fe214e3370b5ea2fb34f2a231185509b63e717f3cd0acb38dd3f8d82bcd5e8172400ae678b70ea4fbed0896d"
84
84
  }
@@ -1,15 +1,31 @@
1
- // eslint-disable-next-line
2
- const _server: any = undefined
1
+ // The booted spec server is cached for the lifetime of the worker process so it
2
+ // is created and booted exactly once, then reused by every spec — instead of
3
+ // re-booting a brand-new PsychicServer for each spec, which re-runs application
4
+ // initialization and churns database/websocket connections (a significant
5
+ // source of slow suites and flaky, connection-exhaustion-driven failures).
6
+ //
7
+ // The cache lives on `globalThis` rather than in a module-scoped variable
8
+ // because isolating test runners (e.g. Vitest with the default `isolate: true`)
9
+ // reset the module registry between spec files. A module-scoped cache would be
10
+ // discarded at every file boundary, forcing a fresh boot per file; `globalThis`
11
+ // persists for the whole worker process, so the server boots once per worker.
12
+ const CACHED_SPEC_SERVER_KEY = Symbol.for('@rvoh/psychic-spec-helpers:cachedSpecServer')
3
13
 
4
14
  // eslint-disable-next-line
5
15
  export default async function createPsychicServer(PsychicServer: any) {
6
16
  // eslint-disable-next-line
7
- if (_server) return _server!
17
+ const store = globalThis as Record<symbol, any>
18
+
19
+ // eslint-disable-next-line
20
+ if (store[CACHED_SPEC_SERVER_KEY]) return store[CACHED_SPEC_SERVER_KEY]
8
21
 
9
22
  // eslint-disable-next-line
10
23
  const server = new PsychicServer()
11
24
  // eslint-disable-next-line
12
25
  await server.boot()
26
+
27
+ // eslint-disable-next-line
28
+ store[CACHED_SPEC_SERVER_KEY] = server
13
29
  // eslint-disable-next-line
14
30
  return server
15
31
  }