trickle-observe 0.2.112 → 0.2.113
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/jest-setup.js +60 -0
- package/package.json +1 -1
package/jest-setup.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Jest/Vitest setup file for trickle instrumentation.
|
|
3
|
+
*
|
|
4
|
+
* Usage in jest.config.js:
|
|
5
|
+
* setupFiles: ['trickle-observe/jest-setup']
|
|
6
|
+
*
|
|
7
|
+
* Or via CLI:
|
|
8
|
+
* npx jest --setupFiles=trickle-observe/jest-setup
|
|
9
|
+
*
|
|
10
|
+
* This patches database drivers and other modules in the Jest worker
|
|
11
|
+
* process. Unlike observe.js (which uses Module._load hooks that Jest
|
|
12
|
+
* bypasses), this directly patches modules after they're loaded.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
const path = require('path');
|
|
16
|
+
const debug = process.env.TRICKLE_DEBUG === '1';
|
|
17
|
+
|
|
18
|
+
// Load the standard observe-register first (sets up Module._load hooks
|
|
19
|
+
// for future requires within the test)
|
|
20
|
+
try {
|
|
21
|
+
require('./dist/observe-register');
|
|
22
|
+
} catch {}
|
|
23
|
+
|
|
24
|
+
// Then eagerly patch any modules that are already in the require cache
|
|
25
|
+
// or that we can load now (Jest may have pre-loaded some)
|
|
26
|
+
function tryPatch(moduleName, patchFn) {
|
|
27
|
+
try {
|
|
28
|
+
const mod = require(moduleName);
|
|
29
|
+
if (mod) {
|
|
30
|
+
const { [patchFn]: patch } = require('./dist/db-observer');
|
|
31
|
+
patch(mod, debug);
|
|
32
|
+
if (debug) console.log(`[trickle/jest] Patched ${moduleName}`);
|
|
33
|
+
}
|
|
34
|
+
} catch {
|
|
35
|
+
// Module not installed — skip
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Patch database drivers
|
|
40
|
+
tryPatch('better-sqlite3', 'patchBetterSqlite3');
|
|
41
|
+
tryPatch('pg', 'patchPg');
|
|
42
|
+
tryPatch('mysql2', 'patchMysql2');
|
|
43
|
+
|
|
44
|
+
// Patch ORMs
|
|
45
|
+
try {
|
|
46
|
+
const { patchPrisma, patchSequelize, patchKnex, patchTypeORM, patchDrizzle } = require('./dist/db-observer');
|
|
47
|
+
try { patchPrisma(require('@prisma/client'), debug); } catch {}
|
|
48
|
+
try { patchSequelize(require('sequelize'), debug); } catch {}
|
|
49
|
+
try { patchKnex(require('knex'), debug); } catch {}
|
|
50
|
+
try { patchTypeORM(require('typeorm'), debug); } catch {}
|
|
51
|
+
try { patchDrizzle(require('drizzle-orm'), debug); } catch {}
|
|
52
|
+
} catch {}
|
|
53
|
+
|
|
54
|
+
// Patch logging frameworks
|
|
55
|
+
try {
|
|
56
|
+
const logObs = require('./dist/log-observer');
|
|
57
|
+
try { logObs.patchWinston(require('winston'), debug); } catch {}
|
|
58
|
+
try { logObs.patchPino(require('pino'), debug); } catch {}
|
|
59
|
+
try { logObs.patchBunyan(require('bunyan'), debug); } catch {}
|
|
60
|
+
} catch {}
|