@geekmidas/testkit 10.0.0-alpha.3 → 10.0.0-alpha.30
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 +74 -0
- package/dist/Factory.cjs +51 -2
- package/dist/Factory.cjs.map +1 -0
- package/dist/Factory.d.cts +143 -3
- package/dist/Factory.d.cts.map +1 -0
- package/dist/Factory.d.mts +143 -3
- package/dist/Factory.d.mts.map +1 -0
- package/dist/Factory.mjs +50 -2
- package/dist/Factory.mjs.map +1 -0
- package/dist/KyselyFactory.cjs +237 -4
- package/dist/KyselyFactory.cjs.map +1 -0
- package/dist/KyselyFactory.d.cts +224 -4
- package/dist/KyselyFactory.d.cts.map +1 -0
- package/dist/KyselyFactory.d.mts +224 -4
- package/dist/KyselyFactory.d.mts.map +1 -0
- package/dist/KyselyFactory.mjs +236 -4
- package/dist/KyselyFactory.mjs.map +1 -0
- package/dist/ObjectionFactory.cjs +265 -4
- package/dist/ObjectionFactory.cjs.map +1 -0
- package/dist/ObjectionFactory.d.cts +237 -4
- package/dist/ObjectionFactory.d.cts.map +1 -0
- package/dist/ObjectionFactory.d.mts +237 -4
- package/dist/ObjectionFactory.d.mts.map +1 -0
- package/dist/ObjectionFactory.mjs +264 -4
- package/dist/ObjectionFactory.mjs.map +1 -0
- package/dist/PostgresKyselyMigrator.cjs +94 -3
- package/dist/PostgresKyselyMigrator.cjs.map +1 -0
- package/dist/PostgresKyselyMigrator.d.cts +80 -3
- package/dist/PostgresKyselyMigrator.d.cts.map +1 -0
- package/dist/PostgresKyselyMigrator.d.mts +80 -3
- package/dist/PostgresKyselyMigrator.d.mts.map +1 -0
- package/dist/PostgresKyselyMigrator.mjs +93 -3
- package/dist/PostgresKyselyMigrator.mjs.map +1 -0
- package/dist/{PostgresMigrator-C_QQ6q35.d.mts → PostgresMigrator-CDPnnwMJ.d.cts} +11 -5
- package/dist/PostgresMigrator-CDPnnwMJ.d.cts.map +1 -0
- package/dist/{PostgresMigrator-CeYy-eHF.d.cts → PostgresMigrator-CDPnnwMJ.d.mts} +11 -5
- package/dist/PostgresMigrator-CDPnnwMJ.d.mts.map +1 -0
- package/dist/PostgresMigrator.cjs +178 -2
- package/dist/PostgresMigrator.cjs.map +1 -0
- package/dist/PostgresMigrator.d.cts +1 -1
- package/dist/PostgresMigrator.d.mts +1 -1
- package/dist/PostgresMigrator.mjs +175 -2
- package/dist/PostgresMigrator.mjs.map +1 -0
- package/dist/PostgresObjectionMigrator.cjs +118 -3
- package/dist/PostgresObjectionMigrator.cjs.map +1 -0
- package/dist/PostgresObjectionMigrator.d.cts +75 -3
- package/dist/PostgresObjectionMigrator.d.cts.map +1 -0
- package/dist/PostgresObjectionMigrator.d.mts +75 -3
- package/dist/PostgresObjectionMigrator.d.mts.map +1 -0
- package/dist/PostgresObjectionMigrator.mjs +117 -3
- package/dist/PostgresObjectionMigrator.mjs.map +1 -0
- package/dist/VitestKyselyTransactionIsolator.cjs +66 -3
- package/dist/VitestKyselyTransactionIsolator.cjs.map +1 -0
- package/dist/VitestKyselyTransactionIsolator.d.cts +60 -3
- package/dist/VitestKyselyTransactionIsolator.d.cts.map +1 -0
- package/dist/VitestKyselyTransactionIsolator.d.mts +60 -3
- package/dist/VitestKyselyTransactionIsolator.d.mts.map +1 -0
- package/dist/VitestKyselyTransactionIsolator.mjs +65 -3
- package/dist/VitestKyselyTransactionIsolator.mjs.map +1 -0
- package/dist/VitestObjectionTransactionIsolator.cjs +62 -3
- package/dist/VitestObjectionTransactionIsolator.cjs.map +1 -0
- package/dist/VitestObjectionTransactionIsolator.d.cts +56 -3
- package/dist/VitestObjectionTransactionIsolator.d.cts.map +1 -0
- package/dist/VitestObjectionTransactionIsolator.d.mts +56 -3
- package/dist/VitestObjectionTransactionIsolator.d.mts.map +1 -0
- package/dist/VitestObjectionTransactionIsolator.mjs +61 -3
- package/dist/VitestObjectionTransactionIsolator.mjs.map +1 -0
- package/dist/VitestTransactionIsolator.cjs +187 -4
- package/dist/VitestTransactionIsolator.cjs.map +1 -0
- package/dist/VitestTransactionIsolator.d.cts +219 -2
- package/dist/VitestTransactionIsolator.d.cts.map +1 -0
- package/dist/VitestTransactionIsolator.d.mts +219 -2
- package/dist/VitestTransactionIsolator.d.mts.map +1 -0
- package/dist/VitestTransactionIsolator.mjs +184 -2
- package/dist/VitestTransactionIsolator.mjs.map +1 -0
- package/dist/aws.cjs +3 -4
- package/dist/aws.cjs.map +1 -1
- package/dist/aws.d.cts +3 -8
- package/dist/aws.d.cts.map +1 -1
- package/dist/aws.d.mts +3 -8
- package/dist/aws.d.mts.map +1 -1
- package/dist/aws.mjs +1 -2
- package/dist/aws.mjs.map +1 -1
- package/dist/benchmark.cjs +2 -2
- package/dist/benchmark.cjs.map +1 -1
- package/dist/benchmark.d.cts +4 -6
- package/dist/benchmark.d.cts.map +1 -1
- package/dist/benchmark.d.mts +4 -6
- package/dist/benchmark.d.mts.map +1 -1
- package/dist/benchmark.mjs +1 -1
- package/dist/benchmark.mjs.map +1 -1
- package/dist/better-auth.cjs +31 -15
- package/dist/better-auth.cjs.map +1 -1
- package/dist/better-auth.d.cts +13142 -4
- package/dist/better-auth.d.cts.map +1 -1
- package/dist/better-auth.d.mts +13142 -4
- package/dist/better-auth.d.mts.map +1 -1
- package/dist/better-auth.mjs +29 -13
- package/dist/better-auth.mjs.map +1 -1
- package/dist/browser.cjs +127 -0
- package/dist/browser.cjs.map +1 -0
- package/dist/browser.d.cts +47 -0
- package/dist/browser.d.cts.map +1 -0
- package/dist/browser.d.mts +47 -0
- package/dist/browser.d.mts.map +1 -0
- package/dist/browser.mjs +125 -0
- package/dist/browser.mjs.map +1 -0
- package/dist/context.cjs +80 -0
- package/dist/context.cjs.map +1 -0
- package/dist/context.d.cts +55 -0
- package/dist/context.d.cts.map +1 -0
- package/dist/context.d.mts +55 -0
- package/dist/context.d.mts.map +1 -0
- package/dist/context.mjs +73 -0
- package/dist/context.mjs.map +1 -0
- package/dist/faker.cjs +262 -8
- package/dist/faker.cjs.map +1 -0
- package/dist/faker.d.cts +162 -2
- package/dist/faker.d.cts.map +1 -0
- package/dist/faker.d.mts +162 -2
- package/dist/faker.d.mts.map +1 -0
- package/dist/faker.mjs +255 -2
- package/dist/faker.mjs.map +1 -0
- package/dist/helpers.cjs +6 -5
- package/dist/helpers.cjs.map +1 -1
- package/dist/helpers.d.cts +1 -5
- package/dist/helpers.d.cts.map +1 -1
- package/dist/helpers.d.mts +1 -5
- package/dist/helpers.d.mts.map +1 -1
- package/dist/helpers.mjs +1 -2
- package/dist/helpers.mjs.map +1 -1
- package/dist/initScript.cjs +15 -15
- package/dist/initScript.cjs.map +1 -1
- package/dist/initScript.d.cts +2 -4
- package/dist/initScript.d.cts.map +1 -1
- package/dist/initScript.d.mts +2 -4
- package/dist/initScript.d.mts.map +1 -1
- package/dist/initScript.mjs +10 -12
- package/dist/initScript.mjs.map +1 -1
- package/dist/kysely.cjs +10 -13
- package/dist/kysely.cjs.map +1 -1
- package/dist/kysely.d.cts +10 -14
- package/dist/kysely.d.cts.map +1 -1
- package/dist/kysely.d.mts +10 -14
- package/dist/kysely.d.mts.map +1 -1
- package/dist/kysely.mjs +10 -14
- package/dist/kysely.mjs.map +1 -1
- package/dist/logger.cjs +3 -4
- package/dist/logger.cjs.map +1 -1
- package/dist/logger.d.cts +1 -5
- package/dist/logger.d.cts.map +1 -1
- package/dist/logger.d.mts +1 -5
- package/dist/logger.d.mts.map +1 -1
- package/dist/logger.mjs +1 -2
- package/dist/logger.mjs.map +1 -1
- package/dist/mailbox.cjs +95 -0
- package/dist/mailbox.cjs.map +1 -0
- package/dist/mailbox.d.cts +58 -0
- package/dist/mailbox.d.cts.map +1 -0
- package/dist/mailbox.d.mts +58 -0
- package/dist/mailbox.d.mts.map +1 -0
- package/dist/mailbox.mjs +92 -0
- package/dist/mailbox.mjs.map +1 -0
- package/dist/objection.cjs +10 -13
- package/dist/objection.cjs.map +1 -1
- package/dist/objection.d.cts +10 -14
- package/dist/objection.d.cts.map +1 -1
- package/dist/objection.d.mts +10 -14
- package/dist/objection.d.mts.map +1 -1
- package/dist/objection.mjs +10 -14
- package/dist/objection.mjs.map +1 -1
- package/dist/os/directory.cjs +31 -2
- package/dist/os/directory.cjs.map +1 -0
- package/dist/os/directory.d.cts +9 -2
- package/dist/os/directory.d.cts.map +1 -0
- package/dist/os/directory.d.mts +9 -2
- package/dist/os/directory.d.mts.map +1 -0
- package/dist/os/directory.mjs +25 -2
- package/dist/os/directory.mjs.map +1 -0
- package/dist/os/index.cjs +3 -3
- package/dist/os/index.d.cts +1 -1
- package/dist/os/index.d.mts +1 -1
- package/dist/os/index.mjs +2 -3
- package/dist/requestContext.cjs +10 -11
- package/dist/requestContext.cjs.map +1 -1
- package/dist/requestContext.d.cts +4 -8
- package/dist/requestContext.d.cts.map +1 -1
- package/dist/requestContext.d.mts +4 -8
- package/dist/requestContext.d.mts.map +1 -1
- package/dist/requestContext.mjs +1 -2
- package/dist/requestContext.mjs.map +1 -1
- package/dist/{chunk-CUT6urMc.cjs → rolldown-runtime-VH7oDXx4.cjs} +8 -10
- package/dist/timer.cjs +2 -2
- package/dist/timer.cjs.map +1 -1
- package/dist/timer.d.cts +1 -4
- package/dist/timer.d.cts.map +1 -1
- package/dist/timer.d.mts +1 -4
- package/dist/timer.d.mts.map +1 -1
- package/dist/timer.mjs +1 -1
- package/dist/timer.mjs.map +1 -1
- package/dist/transactions.cjs +123 -0
- package/dist/transactions.cjs.map +1 -0
- package/dist/transactions.d.cts +44 -0
- package/dist/transactions.d.cts.map +1 -0
- package/dist/transactions.d.mts +44 -0
- package/dist/transactions.d.mts.map +1 -0
- package/dist/transactions.mjs +118 -0
- package/dist/transactions.mjs.map +1 -0
- package/package.json +59 -16
- package/dist/Factory-BFVnMMCC.mjs +0 -51
- package/dist/Factory-BFVnMMCC.mjs.map +0 -1
- package/dist/Factory-BOX312yd.d.cts +0 -147
- package/dist/Factory-BOX312yd.d.cts.map +0 -1
- package/dist/Factory-BhjUOBWN.cjs +0 -57
- package/dist/Factory-BhjUOBWN.cjs.map +0 -1
- package/dist/Factory-C6W78ulZ.d.mts +0 -147
- package/dist/Factory-C6W78ulZ.d.mts.map +0 -1
- package/dist/KyselyFactory-BFqVIn_0.cjs +0 -246
- package/dist/KyselyFactory-BFqVIn_0.cjs.map +0 -1
- package/dist/KyselyFactory-Cc2UmOJk.d.mts +0 -228
- package/dist/KyselyFactory-Cc2UmOJk.d.mts.map +0 -1
- package/dist/KyselyFactory-DMswpwji.mjs +0 -241
- package/dist/KyselyFactory-DMswpwji.mjs.map +0 -1
- package/dist/KyselyFactory-Dy5zzV4B.d.cts +0 -228
- package/dist/KyselyFactory-Dy5zzV4B.d.cts.map +0 -1
- package/dist/ObjectionFactory-BYnPr9ZP.d.mts +0 -241
- package/dist/ObjectionFactory-BYnPr9ZP.d.mts.map +0 -1
- package/dist/ObjectionFactory-BeFBYcan.cjs +0 -272
- package/dist/ObjectionFactory-BeFBYcan.cjs.map +0 -1
- package/dist/ObjectionFactory-CD-WFuMJ.d.cts +0 -241
- package/dist/ObjectionFactory-CD-WFuMJ.d.cts.map +0 -1
- package/dist/ObjectionFactory-QCJ7u0Ql.mjs +0 -267
- package/dist/ObjectionFactory-QCJ7u0Ql.mjs.map +0 -1
- package/dist/PostgresKyselyMigrator-B2M6Gjvv.d.cts +0 -84
- package/dist/PostgresKyselyMigrator-B2M6Gjvv.d.cts.map +0 -1
- package/dist/PostgresKyselyMigrator-BGBxjnFv.cjs +0 -101
- package/dist/PostgresKyselyMigrator-BGBxjnFv.cjs.map +0 -1
- package/dist/PostgresKyselyMigrator-CvwXycJE.d.mts +0 -84
- package/dist/PostgresKyselyMigrator-CvwXycJE.d.mts.map +0 -1
- package/dist/PostgresKyselyMigrator-PI0QhvB1.mjs +0 -95
- package/dist/PostgresKyselyMigrator-PI0QhvB1.mjs.map +0 -1
- package/dist/PostgresMigrator-C_QQ6q35.d.mts.map +0 -1
- package/dist/PostgresMigrator-CeYy-eHF.d.cts.map +0 -1
- package/dist/PostgresMigrator-DVAY04qN.mjs +0 -166
- package/dist/PostgresMigrator-DVAY04qN.mjs.map +0 -1
- package/dist/PostgresMigrator-M9jpzOvN.cjs +0 -172
- package/dist/PostgresMigrator-M9jpzOvN.cjs.map +0 -1
- package/dist/PostgresObjectionMigrator-1j6YIB1c.d.mts +0 -79
- package/dist/PostgresObjectionMigrator-1j6YIB1c.d.mts.map +0 -1
- package/dist/PostgresObjectionMigrator-DHVC9h_P.d.cts +0 -79
- package/dist/PostgresObjectionMigrator-DHVC9h_P.d.cts.map +0 -1
- package/dist/PostgresObjectionMigrator-DSaPhwjY.cjs +0 -123
- package/dist/PostgresObjectionMigrator-DSaPhwjY.cjs.map +0 -1
- package/dist/PostgresObjectionMigrator-DjPKdUbm.mjs +0 -118
- package/dist/PostgresObjectionMigrator-DjPKdUbm.mjs.map +0 -1
- package/dist/VitestKyselyTransactionIsolator-1Saieke7.mjs +0 -67
- package/dist/VitestKyselyTransactionIsolator-1Saieke7.mjs.map +0 -1
- package/dist/VitestKyselyTransactionIsolator-BjJSXryR.cjs +0 -72
- package/dist/VitestKyselyTransactionIsolator-BjJSXryR.cjs.map +0 -1
- package/dist/VitestKyselyTransactionIsolator-CwlTo6yf.d.mts +0 -64
- package/dist/VitestKyselyTransactionIsolator-CwlTo6yf.d.mts.map +0 -1
- package/dist/VitestKyselyTransactionIsolator-DJasVuIZ.d.cts +0 -64
- package/dist/VitestKyselyTransactionIsolator-DJasVuIZ.d.cts.map +0 -1
- package/dist/VitestObjectionTransactionIsolator-B_VSWFum.mjs +0 -63
- package/dist/VitestObjectionTransactionIsolator-B_VSWFum.mjs.map +0 -1
- package/dist/VitestObjectionTransactionIsolator-CTTjvTPO.d.cts +0 -60
- package/dist/VitestObjectionTransactionIsolator-CTTjvTPO.d.cts.map +0 -1
- package/dist/VitestObjectionTransactionIsolator-CyZG6nq4.d.mts +0 -60
- package/dist/VitestObjectionTransactionIsolator-CyZG6nq4.d.mts.map +0 -1
- package/dist/VitestObjectionTransactionIsolator-DDoJTu7e.cjs +0 -68
- package/dist/VitestObjectionTransactionIsolator-DDoJTu7e.cjs.map +0 -1
- package/dist/VitestTransactionIsolator-BLaw80cx.d.mts +0 -223
- package/dist/VitestTransactionIsolator-BLaw80cx.d.mts.map +0 -1
- package/dist/VitestTransactionIsolator-CvwPecpl.mjs +0 -188
- package/dist/VitestTransactionIsolator-CvwPecpl.mjs.map +0 -1
- package/dist/VitestTransactionIsolator-DyiX-QtK.cjs +0 -206
- package/dist/VitestTransactionIsolator-DyiX-QtK.cjs.map +0 -1
- package/dist/VitestTransactionIsolator-glcxyS_R.d.cts +0 -223
- package/dist/VitestTransactionIsolator-glcxyS_R.d.cts.map +0 -1
- package/dist/directory-BxjqmtCT.mjs +0 -27
- package/dist/directory-BxjqmtCT.mjs.map +0 -1
- package/dist/directory-DD_xQoSh.cjs +0 -33
- package/dist/directory-DD_xQoSh.cjs.map +0 -1
- package/dist/directory-DGOcVlKD.d.cts +0 -13
- package/dist/directory-DGOcVlKD.d.cts.map +0 -1
- package/dist/directory-DiQ_DkaF.d.mts +0 -13
- package/dist/directory-DiQ_DkaF.d.mts.map +0 -1
- package/dist/faker-B14IEMIN.cjs +0 -304
- package/dist/faker-B14IEMIN.cjs.map +0 -1
- package/dist/faker-BGKYFoCT.mjs +0 -262
- package/dist/faker-BGKYFoCT.mjs.map +0 -1
- package/dist/faker-Cg76aFNO.d.cts +0 -166
- package/dist/faker-Cg76aFNO.d.cts.map +0 -1
- package/dist/faker-D9gz7KjY.d.mts +0 -166
- package/dist/faker-D9gz7KjY.d.mts.map +0 -1
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
//#region src/context.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* The test a request belongs to.
|
|
4
|
+
*
|
|
5
|
+
* A feature test drives an app the way it runs deployed: a browser calls the
|
|
6
|
+
* API, the API calls the auth server, each over a URL. In a test those URLs are
|
|
7
|
+
* served in-process, and every request made on behalf of one test has to reach
|
|
8
|
+
* *that test's* transactions — its own call to the API, and the API's call to
|
|
9
|
+
* the auth server made while handling it.
|
|
10
|
+
*
|
|
11
|
+
* The id travels two ways. In-process, an `AsyncLocalStorage` carries it
|
|
12
|
+
* through every await a test and its handlers make. On the wire it is the
|
|
13
|
+
* `x-test-context-id` header, which a stamped `fetch` adds to every request made
|
|
14
|
+
* inside a context, so whatever serves the URL can find the context again —
|
|
15
|
+
* without any caller, including code under test, writing the header.
|
|
16
|
+
*/
|
|
17
|
+
/** The header a test context travels in between client and server. */
|
|
18
|
+
export declare const TEST_CONTEXT_HEADER = "x-test-context-id";
|
|
19
|
+
/**
|
|
20
|
+
* Which side of a URL the running code is on.
|
|
21
|
+
*
|
|
22
|
+
* `client` is the test itself and the frontend it renders — what a browser
|
|
23
|
+
* does. `server` is a handler serving a request. The difference matters for
|
|
24
|
+
* cookies: a browser attaches its cookies to what it sends, a server forwards
|
|
25
|
+
* only what it was handed. A server call that picked up the browser's cookies
|
|
26
|
+
* would hide the bug of a server that forgets to forward them.
|
|
27
|
+
*/
|
|
28
|
+
export type TestSide = 'client' | 'server';
|
|
29
|
+
export interface TestContext {
|
|
30
|
+
readonly id: string;
|
|
31
|
+
readonly side: TestSide;
|
|
32
|
+
}
|
|
33
|
+
/** The context the running code belongs to, if any. */
|
|
34
|
+
export declare function currentTestContext(): TestContext | undefined;
|
|
35
|
+
/** Run `fn` as the client side of the test `id`. */
|
|
36
|
+
export declare function runInTestContext<T>(id: string, fn: () => T): T;
|
|
37
|
+
/**
|
|
38
|
+
* Run `fn` as a server handling a request for the test `id` — what whatever
|
|
39
|
+
* serves a URL in-process wraps each request in.
|
|
40
|
+
*/
|
|
41
|
+
export declare function runAsServer<T>(id: string, fn: () => T): T;
|
|
42
|
+
/** The test a request was made for, read off its header. */
|
|
43
|
+
export declare function testContextOf(request: Request): string | undefined;
|
|
44
|
+
/** `fetch`, adding the current test's id to every request made inside one. */
|
|
45
|
+
export declare function stampedFetch(inner: typeof fetch): typeof fetch;
|
|
46
|
+
/**
|
|
47
|
+
* Stamp the global `fetch` with the current test's id.
|
|
48
|
+
*
|
|
49
|
+
* Install it after anything else that patches `fetch` — MSW's interceptor in
|
|
50
|
+
* particular — so the stamp is added before the request is intercepted.
|
|
51
|
+
* Returns what puts the previous `fetch` back.
|
|
52
|
+
*/
|
|
53
|
+
export declare function installContextFetch(): () => void;
|
|
54
|
+
//#endregion
|
|
55
|
+
//# sourceMappingURL=context.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.d.mts","names":[],"sources":["../src/context.ts"],"mappings":";;;;;;;;;;;;;;;;;qBAmBa;;;;;;;;;;YAWD;iBAEK;WACP;WACA,MAAM;;;wBAMA,sBAAsB;;wBAKtB,iBAAiB,GAAG,YAAY,UAAU,IAAI;;;;;wBAQ9C,YAAY,GAAG,YAAY,UAAU,IAAI;;wBAKzC,cAAc,SAAS;;wBAKvB,aAAa,cAAc,eAAe;;;;;;;;wBAoB1C"}
|
package/dist/context.mjs
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
//#region src/context.ts
|
|
3
|
+
/**
|
|
4
|
+
* The test a request belongs to.
|
|
5
|
+
*
|
|
6
|
+
* A feature test drives an app the way it runs deployed: a browser calls the
|
|
7
|
+
* API, the API calls the auth server, each over a URL. In a test those URLs are
|
|
8
|
+
* served in-process, and every request made on behalf of one test has to reach
|
|
9
|
+
* *that test's* transactions — its own call to the API, and the API's call to
|
|
10
|
+
* the auth server made while handling it.
|
|
11
|
+
*
|
|
12
|
+
* The id travels two ways. In-process, an `AsyncLocalStorage` carries it
|
|
13
|
+
* through every await a test and its handlers make. On the wire it is the
|
|
14
|
+
* `x-test-context-id` header, which a stamped `fetch` adds to every request made
|
|
15
|
+
* inside a context, so whatever serves the URL can find the context again —
|
|
16
|
+
* without any caller, including code under test, writing the header.
|
|
17
|
+
*/
|
|
18
|
+
/** The header a test context travels in between client and server. */
|
|
19
|
+
const TEST_CONTEXT_HEADER = "x-test-context-id";
|
|
20
|
+
const storage = new AsyncLocalStorage();
|
|
21
|
+
/** The context the running code belongs to, if any. */
|
|
22
|
+
function currentTestContext() {
|
|
23
|
+
return storage.getStore();
|
|
24
|
+
}
|
|
25
|
+
/** Run `fn` as the client side of the test `id`. */
|
|
26
|
+
function runInTestContext(id, fn) {
|
|
27
|
+
return storage.run({
|
|
28
|
+
id,
|
|
29
|
+
side: "client"
|
|
30
|
+
}, fn);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Run `fn` as a server handling a request for the test `id` — what whatever
|
|
34
|
+
* serves a URL in-process wraps each request in.
|
|
35
|
+
*/
|
|
36
|
+
function runAsServer(id, fn) {
|
|
37
|
+
return storage.run({
|
|
38
|
+
id,
|
|
39
|
+
side: "server"
|
|
40
|
+
}, fn);
|
|
41
|
+
}
|
|
42
|
+
/** The test a request was made for, read off its header. */
|
|
43
|
+
function testContextOf(request) {
|
|
44
|
+
return request.headers.get("x-test-context-id") ?? void 0;
|
|
45
|
+
}
|
|
46
|
+
/** `fetch`, adding the current test's id to every request made inside one. */
|
|
47
|
+
function stampedFetch(inner) {
|
|
48
|
+
return (input, init) => {
|
|
49
|
+
const context = currentTestContext();
|
|
50
|
+
if (!context) return inner(input, init);
|
|
51
|
+
const request = new Request(input, init);
|
|
52
|
+
if (!request.headers.has("x-test-context-id")) request.headers.set(TEST_CONTEXT_HEADER, context.id);
|
|
53
|
+
return inner(request);
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Stamp the global `fetch` with the current test's id.
|
|
58
|
+
*
|
|
59
|
+
* Install it after anything else that patches `fetch` — MSW's interceptor in
|
|
60
|
+
* particular — so the stamp is added before the request is intercepted.
|
|
61
|
+
* Returns what puts the previous `fetch` back.
|
|
62
|
+
*/
|
|
63
|
+
function installContextFetch() {
|
|
64
|
+
const previous = globalThis.fetch;
|
|
65
|
+
globalThis.fetch = stampedFetch(previous);
|
|
66
|
+
return () => {
|
|
67
|
+
globalThis.fetch = previous;
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
//#endregion
|
|
71
|
+
export { TEST_CONTEXT_HEADER, currentTestContext, installContextFetch, runAsServer, runInTestContext, stampedFetch, testContextOf };
|
|
72
|
+
|
|
73
|
+
//# sourceMappingURL=context.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.mjs","names":[],"sources":["../src/context.ts"],"sourcesContent":["/**\n * The test a request belongs to.\n *\n * A feature test drives an app the way it runs deployed: a browser calls the\n * API, the API calls the auth server, each over a URL. In a test those URLs are\n * served in-process, and every request made on behalf of one test has to reach\n * *that test's* transactions — its own call to the API, and the API's call to\n * the auth server made while handling it.\n *\n * The id travels two ways. In-process, an `AsyncLocalStorage` carries it\n * through every await a test and its handlers make. On the wire it is the\n * `x-test-context-id` header, which a stamped `fetch` adds to every request made\n * inside a context, so whatever serves the URL can find the context again —\n * without any caller, including code under test, writing the header.\n */\n\nimport { AsyncLocalStorage } from 'node:async_hooks';\n\n/** The header a test context travels in between client and server. */\nexport const TEST_CONTEXT_HEADER = 'x-test-context-id';\n\n/**\n * Which side of a URL the running code is on.\n *\n * `client` is the test itself and the frontend it renders — what a browser\n * does. `server` is a handler serving a request. The difference matters for\n * cookies: a browser attaches its cookies to what it sends, a server forwards\n * only what it was handed. A server call that picked up the browser's cookies\n * would hide the bug of a server that forgets to forward them.\n */\nexport type TestSide = 'client' | 'server';\n\nexport interface TestContext {\n\treadonly id: string;\n\treadonly side: TestSide;\n}\n\nconst storage = new AsyncLocalStorage<TestContext>();\n\n/** The context the running code belongs to, if any. */\nexport function currentTestContext(): TestContext | undefined {\n\treturn storage.getStore();\n}\n\n/** Run `fn` as the client side of the test `id`. */\nexport function runInTestContext<T>(id: string, fn: () => T): T {\n\treturn storage.run({ id, side: 'client' }, fn);\n}\n\n/**\n * Run `fn` as a server handling a request for the test `id` — what whatever\n * serves a URL in-process wraps each request in.\n */\nexport function runAsServer<T>(id: string, fn: () => T): T {\n\treturn storage.run({ id, side: 'server' }, fn);\n}\n\n/** The test a request was made for, read off its header. */\nexport function testContextOf(request: Request): string | undefined {\n\treturn request.headers.get(TEST_CONTEXT_HEADER) ?? undefined;\n}\n\n/** `fetch`, adding the current test's id to every request made inside one. */\nexport function stampedFetch(inner: typeof fetch): typeof fetch {\n\treturn (input, init) => {\n\t\tconst context = currentTestContext();\n\t\tif (!context) return inner(input, init);\n\n\t\tconst request = new Request(input, init);\n\t\tif (!request.headers.has(TEST_CONTEXT_HEADER)) {\n\t\t\trequest.headers.set(TEST_CONTEXT_HEADER, context.id);\n\t\t}\n\t\treturn inner(request);\n\t};\n}\n\n/**\n * Stamp the global `fetch` with the current test's id.\n *\n * Install it after anything else that patches `fetch` — MSW's interceptor in\n * particular — so the stamp is added before the request is intercepted.\n * Returns what puts the previous `fetch` back.\n */\nexport function installContextFetch(): () => void {\n\tconst previous = globalThis.fetch;\n\tglobalThis.fetch = stampedFetch(previous);\n\treturn () => {\n\t\tglobalThis.fetch = previous;\n\t};\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAmBA,MAAa,sBAAsB;AAkBnC,MAAM,UAAU,IAAI,kBAA+B;;AAGnD,SAAgB,qBAA8C;CAC7D,OAAO,QAAQ,SAAS;AACzB;;AAGA,SAAgB,iBAAoB,IAAY,IAAgB;CAC/D,OAAO,QAAQ,IAAI;EAAE;EAAI,MAAM;CAAS,GAAG,EAAE;AAC9C;;;;;AAMA,SAAgB,YAAe,IAAY,IAAgB;CAC1D,OAAO,QAAQ,IAAI;EAAE;EAAI,MAAM;CAAS,GAAG,EAAE;AAC9C;;AAGA,SAAgB,cAAc,SAAsC;CACnE,OAAO,QAAQ,QAAQ,IAAA,mBAAuB,KAAK,KAAA;AACpD;;AAGA,SAAgB,aAAa,OAAmC;CAC/D,QAAQ,OAAO,SAAS;EACvB,MAAM,UAAU,mBAAmB;EACnC,IAAI,CAAC,SAAS,OAAO,MAAM,OAAO,IAAI;EAEtC,MAAM,UAAU,IAAI,QAAQ,OAAO,IAAI;EACvC,IAAI,CAAC,QAAQ,QAAQ,IAAA,mBAAuB,GAC3C,QAAQ,QAAQ,IAAI,qBAAqB,QAAQ,EAAE;EAEpD,OAAO,MAAM,OAAO;CACrB;AACD;;;;;;;;AASA,SAAgB,sBAAkC;CACjD,MAAM,WAAW,WAAW;CAC5B,WAAW,QAAQ,aAAa,QAAQ;CACxC,aAAa;EACZ,WAAW,QAAQ;CACpB;AACD"}
|
package/dist/faker.cjs
CHANGED
|
@@ -1,9 +1,263 @@
|
|
|
1
|
-
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let _faker_js_faker = require("@faker-js/faker");
|
|
3
|
+
//#region src/faker.ts
|
|
4
|
+
/**
|
|
5
|
+
* Atomic counter implementation for thread-safe sequence generation.
|
|
6
|
+
* Provides a clean abstraction for generating sequential numbers in tests.
|
|
7
|
+
* While JavaScript is single-threaded, this class makes the intent explicit.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* const counter = new AtomicCounter(100);
|
|
12
|
+
* console.log(counter.increment()); // 101
|
|
13
|
+
* console.log(counter.increment()); // 102
|
|
14
|
+
* console.log(counter.get()); // 102
|
|
15
|
+
* counter.reset(200);
|
|
16
|
+
* console.log(counter.increment()); // 201
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
var AtomicCounter = class {
|
|
20
|
+
/**
|
|
21
|
+
* The current counter value.
|
|
22
|
+
* @private
|
|
23
|
+
*/
|
|
24
|
+
value;
|
|
25
|
+
/**
|
|
26
|
+
* Creates a new atomic counter.
|
|
27
|
+
* @param initialValue - The starting value (default: 0)
|
|
28
|
+
*/
|
|
29
|
+
constructor(initialValue = 0) {
|
|
30
|
+
this.value = initialValue;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Increments the counter and returns the new value.
|
|
34
|
+
* @returns The incremented value
|
|
35
|
+
*/
|
|
36
|
+
increment() {
|
|
37
|
+
return ++this.value;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Gets the current counter value without incrementing.
|
|
41
|
+
* @returns The current value
|
|
42
|
+
*/
|
|
43
|
+
get() {
|
|
44
|
+
return this.value;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Resets the counter to a specific value.
|
|
48
|
+
* @param value - The new value (default: 0)
|
|
49
|
+
*/
|
|
50
|
+
reset(value = 0) {
|
|
51
|
+
this.value = value;
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* Generates random timestamp fields for database records.
|
|
56
|
+
* Creates a createdAt date in the past and an updatedAt date between creation and now.
|
|
57
|
+
* Milliseconds are set to 0 for cleaner database storage.
|
|
58
|
+
*
|
|
59
|
+
* @returns Object with createdAt and updatedAt Date fields
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```typescript
|
|
63
|
+
* const { createdAt, updatedAt } = timestamps();
|
|
64
|
+
* console.log(createdAt); // 2023-05-15T10:30:00.000Z
|
|
65
|
+
* console.log(updatedAt); // 2023-11-20T14:45:00.000Z
|
|
66
|
+
*
|
|
67
|
+
* // Use in factory
|
|
68
|
+
* const user = {
|
|
69
|
+
* name: 'John Doe',
|
|
70
|
+
* ...timestamps()
|
|
71
|
+
* };
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
function timestamps() {
|
|
75
|
+
const createdAt = faker.date.past();
|
|
76
|
+
const updatedAt = faker.date.between({
|
|
77
|
+
from: createdAt,
|
|
78
|
+
to: /* @__PURE__ */ new Date()
|
|
79
|
+
});
|
|
80
|
+
createdAt.setMilliseconds(0);
|
|
81
|
+
updatedAt.setMilliseconds(0);
|
|
82
|
+
return {
|
|
83
|
+
createdAt,
|
|
84
|
+
updatedAt
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Generates a reverse domain name identifier.
|
|
89
|
+
* Useful for creating unique identifiers that follow domain naming conventions.
|
|
90
|
+
*
|
|
91
|
+
* @param suffix - Optional suffix to append to the identifier
|
|
92
|
+
* @returns A reverse domain name string (e.g., "com.example.feature123")
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```typescript
|
|
96
|
+
* console.log(identifier()); // "com.example.widget1"
|
|
97
|
+
* console.log(identifier('user')); // "org.acme.user"
|
|
98
|
+
* console.log(identifier('api')); // "net.demo.api"
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
function identifier(suffix) {
|
|
102
|
+
return [
|
|
103
|
+
faker.internet.domainSuffix(),
|
|
104
|
+
faker.internet.domainWord(),
|
|
105
|
+
suffix ? suffix : faker.internet.domainWord() + sequence("identifier")
|
|
106
|
+
].join(".");
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Storage for named sequence counters.
|
|
110
|
+
* Each sequence maintains its own independent counter.
|
|
111
|
+
* @private
|
|
112
|
+
*/
|
|
113
|
+
const sequences = /* @__PURE__ */ new Map();
|
|
114
|
+
/**
|
|
115
|
+
* Generates sequential numbers for a named sequence.
|
|
116
|
+
* Useful for creating unique IDs or numbered test data.
|
|
117
|
+
* Each named sequence maintains its own counter.
|
|
118
|
+
*
|
|
119
|
+
* @param name - The sequence name (default: 'default')
|
|
120
|
+
* @returns The next number in the sequence
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* ```typescript
|
|
124
|
+
* console.log(sequence()); // 1
|
|
125
|
+
* console.log(sequence()); // 2
|
|
126
|
+
* console.log(sequence('user')); // 1
|
|
127
|
+
* console.log(sequence('user')); // 2
|
|
128
|
+
* console.log(sequence()); // 3
|
|
129
|
+
*
|
|
130
|
+
* // Use in factories
|
|
131
|
+
* const email = `user${sequence('email')}@example.com`;
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
function sequence(name = "default") {
|
|
135
|
+
if (!sequences.has(name)) sequences.set(name, new AtomicCounter());
|
|
136
|
+
return sequences.get(name).increment();
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Resets a named sequence counter to a specific value.
|
|
140
|
+
* Useful for resetting sequences between test suites.
|
|
141
|
+
*
|
|
142
|
+
* @param name - The sequence name to reset (default: 'default')
|
|
143
|
+
* @param value - The new starting value (default: 0)
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* ```typescript
|
|
147
|
+
* sequence('user'); // 1
|
|
148
|
+
* sequence('user'); // 2
|
|
149
|
+
* resetSequence('user');
|
|
150
|
+
* sequence('user'); // 1
|
|
151
|
+
*
|
|
152
|
+
* resetSequence('order', 1000);
|
|
153
|
+
* sequence('order'); // 1001
|
|
154
|
+
* ```
|
|
155
|
+
*/
|
|
156
|
+
function resetSequence(name = "default", value = 0) {
|
|
157
|
+
if (sequences.has(name)) sequences.get(name).reset(value);
|
|
158
|
+
else sequences.set(name, new AtomicCounter(value));
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Resets all sequence counters.
|
|
162
|
+
* Useful for cleaning up between test suites to ensure predictable sequences.
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* ```typescript
|
|
166
|
+
* // In test setup
|
|
167
|
+
* beforeEach(() => {
|
|
168
|
+
* resetAllSequences();
|
|
169
|
+
* });
|
|
170
|
+
*
|
|
171
|
+
* it('starts sequences from 1', () => {
|
|
172
|
+
* expect(sequence()).toBe(1);
|
|
173
|
+
* expect(sequence('user')).toBe(1);
|
|
174
|
+
* });
|
|
175
|
+
* ```
|
|
176
|
+
*/
|
|
177
|
+
function resetAllSequences() {
|
|
178
|
+
sequences.clear();
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Generates a random price as a number.
|
|
182
|
+
* Converts faker's string price to a numeric value.
|
|
183
|
+
*
|
|
184
|
+
* @returns A random price number
|
|
185
|
+
*
|
|
186
|
+
* @example
|
|
187
|
+
* ```typescript
|
|
188
|
+
* const productPrice = price(); // 29.99
|
|
189
|
+
* const total = price() * quantity; // Numeric calculation
|
|
190
|
+
* ```
|
|
191
|
+
*/
|
|
192
|
+
function price() {
|
|
193
|
+
return +faker.commerce.price();
|
|
194
|
+
}
|
|
195
|
+
function coordinateInRadius(center, radius) {
|
|
196
|
+
const d = radius / 6378137;
|
|
197
|
+
const theta = 2 * Math.PI * Math.random();
|
|
198
|
+
const r = d * Math.sqrt(Math.random());
|
|
199
|
+
const lat1 = center.lat * Math.PI / 180;
|
|
200
|
+
const lng1 = center.lng * Math.PI / 180;
|
|
201
|
+
const lat2 = Math.asin(Math.sin(lat1) * Math.cos(r) + Math.cos(lat1) * Math.sin(r) * Math.cos(theta));
|
|
202
|
+
const lng2 = lng1 + Math.atan2(Math.sin(theta) * Math.sin(r) * Math.cos(lat1), Math.cos(r) - Math.sin(lat1) * Math.sin(lat2));
|
|
203
|
+
return {
|
|
204
|
+
lat: lat2 * 180 / Math.PI,
|
|
205
|
+
lng: lng2 * 180 / Math.PI
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
function coordinateOutsideRadius(center, minRadiusMeters, maxRadiusMeters) {
|
|
209
|
+
const earth = 6378137;
|
|
210
|
+
const minD = minRadiusMeters / earth;
|
|
211
|
+
const maxD = maxRadiusMeters / earth;
|
|
212
|
+
const theta = 2 * Math.PI * Math.random();
|
|
213
|
+
const r = Math.sqrt(minD * minD + (maxD * maxD - minD * minD) * Math.random());
|
|
214
|
+
const lat1 = center.lat * Math.PI / 180;
|
|
215
|
+
const lng1 = center.lng * Math.PI / 180;
|
|
216
|
+
const lat2 = Math.asin(Math.sin(lat1) * Math.cos(r) + Math.cos(lat1) * Math.sin(r) * Math.cos(theta));
|
|
217
|
+
const normalizedLng = ((lng1 + Math.atan2(Math.sin(theta) * Math.sin(r) * Math.cos(lat1), Math.cos(r) - Math.sin(lat1) * Math.sin(lat2))) * 180 / Math.PI + 540) % 360 - 180;
|
|
218
|
+
return {
|
|
219
|
+
lat: lat2 * 180 / Math.PI,
|
|
220
|
+
lng: normalizedLng
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Enhanced faker instance with additional utility methods for testing.
|
|
225
|
+
* Extends @faker-js/faker with custom methods for common test data generation patterns.
|
|
226
|
+
*
|
|
227
|
+
* @example
|
|
228
|
+
* ```typescript
|
|
229
|
+
* import { faker } from '@geekmidas/testkit';
|
|
230
|
+
*
|
|
231
|
+
* // Use standard faker methods
|
|
232
|
+
* const name = faker.person.fullName();
|
|
233
|
+
* const email = faker.internet.email();
|
|
234
|
+
*
|
|
235
|
+
* // Use custom extensions
|
|
236
|
+
* const { createdAt, updatedAt } = faker.timestamps();
|
|
237
|
+
* const id = faker.identifier('user');
|
|
238
|
+
* const orderNumber = faker.sequence('order');
|
|
239
|
+
* const productPrice = faker.price();
|
|
240
|
+
* ```
|
|
241
|
+
*/
|
|
242
|
+
const faker = Object.freeze(Object.assign({}, _faker_js_faker.faker, {
|
|
243
|
+
timestamps,
|
|
244
|
+
identifier,
|
|
245
|
+
sequence,
|
|
246
|
+
resetSequence,
|
|
247
|
+
resetAllSequences,
|
|
248
|
+
price,
|
|
249
|
+
coordinates: {
|
|
250
|
+
within: coordinateInRadius,
|
|
251
|
+
outside: coordinateOutsideRadius
|
|
252
|
+
}
|
|
253
|
+
}));
|
|
254
|
+
//#endregion
|
|
255
|
+
exports.coordinateInRadius = coordinateInRadius;
|
|
256
|
+
exports.faker = faker;
|
|
257
|
+
exports.identifier = identifier;
|
|
258
|
+
exports.resetAllSequences = resetAllSequences;
|
|
259
|
+
exports.resetSequence = resetSequence;
|
|
260
|
+
exports.sequence = sequence;
|
|
261
|
+
exports.timestamps = timestamps;
|
|
2
262
|
|
|
3
|
-
|
|
4
|
-
exports.faker = require_faker.faker;
|
|
5
|
-
exports.identifier = require_faker.identifier;
|
|
6
|
-
exports.resetAllSequences = require_faker.resetAllSequences;
|
|
7
|
-
exports.resetSequence = require_faker.resetSequence;
|
|
8
|
-
exports.sequence = require_faker.sequence;
|
|
9
|
-
exports.timestamps = require_faker.timestamps;
|
|
263
|
+
//# sourceMappingURL=faker.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"faker.cjs","names":["baseFaker"],"sources":["../src/faker.ts"],"sourcesContent":["import { faker as baseFaker } from '@faker-js/faker';\n\n// NOTE: This is a simple way to extend `faker` with additional methods\n\n/**\n * Atomic counter implementation for thread-safe sequence generation.\n * Provides a clean abstraction for generating sequential numbers in tests.\n * While JavaScript is single-threaded, this class makes the intent explicit.\n *\n * @example\n * ```typescript\n * const counter = new AtomicCounter(100);\n * console.log(counter.increment()); // 101\n * console.log(counter.increment()); // 102\n * console.log(counter.get()); // 102\n * counter.reset(200);\n * console.log(counter.increment()); // 201\n * ```\n */\nclass AtomicCounter {\n\t/**\n\t * The current counter value.\n\t * @private\n\t */\n\tprivate value: number;\n\n\t/**\n\t * Creates a new atomic counter.\n\t * @param initialValue - The starting value (default: 0)\n\t */\n\tconstructor(initialValue = 0) {\n\t\tthis.value = initialValue;\n\t}\n\n\t/**\n\t * Increments the counter and returns the new value.\n\t * @returns The incremented value\n\t */\n\tincrement(): number {\n\t\t// In Node.js, JavaScript is single-threaded within the event loop,\n\t\t// so this operation is already atomic. However, this class provides\n\t\t// a cleaner abstraction and makes the intent explicit.\n\t\treturn ++this.value;\n\t}\n\n\t/**\n\t * Gets the current counter value without incrementing.\n\t * @returns The current value\n\t */\n\tget(): number {\n\t\treturn this.value;\n\t}\n\n\t/**\n\t * Resets the counter to a specific value.\n\t * @param value - The new value (default: 0)\n\t */\n\treset(value = 0): void {\n\t\tthis.value = value;\n\t}\n}\n\n/**\n * Generates random timestamp fields for database records.\n * Creates a createdAt date in the past and an updatedAt date between creation and now.\n * Milliseconds are set to 0 for cleaner database storage.\n *\n * @returns Object with createdAt and updatedAt Date fields\n *\n * @example\n * ```typescript\n * const { createdAt, updatedAt } = timestamps();\n * console.log(createdAt); // 2023-05-15T10:30:00.000Z\n * console.log(updatedAt); // 2023-11-20T14:45:00.000Z\n *\n * // Use in factory\n * const user = {\n * name: 'John Doe',\n * ...timestamps()\n * };\n * ```\n */\nexport function timestamps(): Timestamps {\n\tconst createdAt = faker.date.past();\n\tconst updatedAt = faker.date.between({\n\t\tfrom: createdAt,\n\t\tto: new Date(),\n\t});\n\n\tcreatedAt.setMilliseconds(0);\n\tupdatedAt.setMilliseconds(0);\n\n\treturn { createdAt, updatedAt };\n}\n\n/**\n * Generates a reverse domain name identifier.\n * Useful for creating unique identifiers that follow domain naming conventions.\n *\n * @param suffix - Optional suffix to append to the identifier\n * @returns A reverse domain name string (e.g., \"com.example.feature123\")\n *\n * @example\n * ```typescript\n * console.log(identifier()); // \"com.example.widget1\"\n * console.log(identifier('user')); // \"org.acme.user\"\n * console.log(identifier('api')); // \"net.demo.api\"\n * ```\n */\nexport function identifier(suffix?: string): string {\n\treturn [\n\t\tfaker.internet.domainSuffix(),\n\t\tfaker.internet.domainWord(),\n\t\tsuffix ? suffix : faker.internet.domainWord() + sequence('identifier'),\n\t].join('.');\n}\n\n/**\n * Storage for named sequence counters.\n * Each sequence maintains its own independent counter.\n * @private\n */\nconst sequences = new Map<string, AtomicCounter>();\n\n/**\n * Generates sequential numbers for a named sequence.\n * Useful for creating unique IDs or numbered test data.\n * Each named sequence maintains its own counter.\n *\n * @param name - The sequence name (default: 'default')\n * @returns The next number in the sequence\n *\n * @example\n * ```typescript\n * console.log(sequence()); // 1\n * console.log(sequence()); // 2\n * console.log(sequence('user')); // 1\n * console.log(sequence('user')); // 2\n * console.log(sequence()); // 3\n *\n * // Use in factories\n * const email = `user${sequence('email')}@example.com`;\n * ```\n */\nexport function sequence(name = 'default'): number {\n\tif (!sequences.has(name)) {\n\t\tsequences.set(name, new AtomicCounter());\n\t}\n\n\tconst counter = sequences.get(name) as AtomicCounter;\n\treturn counter.increment();\n}\n\n/**\n * Resets a named sequence counter to a specific value.\n * Useful for resetting sequences between test suites.\n *\n * @param name - The sequence name to reset (default: 'default')\n * @param value - The new starting value (default: 0)\n *\n * @example\n * ```typescript\n * sequence('user'); // 1\n * sequence('user'); // 2\n * resetSequence('user');\n * sequence('user'); // 1\n *\n * resetSequence('order', 1000);\n * sequence('order'); // 1001\n * ```\n */\nexport function resetSequence(name = 'default', value = 0): void {\n\tif (sequences.has(name)) {\n\t\tconst counter = sequences.get(name) as AtomicCounter;\n\t\tcounter.reset(value);\n\t} else {\n\t\tsequences.set(name, new AtomicCounter(value));\n\t}\n}\n\n/**\n * Resets all sequence counters.\n * Useful for cleaning up between test suites to ensure predictable sequences.\n *\n * @example\n * ```typescript\n * // In test setup\n * beforeEach(() => {\n * resetAllSequences();\n * });\n *\n * it('starts sequences from 1', () => {\n * expect(sequence()).toBe(1);\n * expect(sequence('user')).toBe(1);\n * });\n * ```\n */\nexport function resetAllSequences(): void {\n\tsequences.clear();\n}\n\n/**\n * Generates a random price as a number.\n * Converts faker's string price to a numeric value.\n *\n * @returns A random price number\n *\n * @example\n * ```typescript\n * const productPrice = price(); // 29.99\n * const total = price() * quantity; // Numeric calculation\n * ```\n */\nfunction price(): number {\n\treturn +faker.commerce.price();\n}\n\ntype Coordinate = {\n\tlat: number;\n\tlng: number;\n};\n\nexport function coordinateInRadius(\n\tcenter: Coordinate,\n\tradius: number,\n): Coordinate {\n\t// Earth's radius in meters\n\tconst earth = 6378137;\n\t// Convert radius from meters to degrees\n\tconst d = radius / earth;\n\n\t// Random bearing and distance\n\tconst theta = 2 * Math.PI * Math.random();\n\tconst r = d * Math.sqrt(Math.random());\n\n\tconst lat1 = (center.lat * Math.PI) / 180;\n\tconst lng1 = (center.lng * Math.PI) / 180;\n\n\tconst lat2 = Math.asin(\n\t\tMath.sin(lat1) * Math.cos(r) +\n\t\t\tMath.cos(lat1) * Math.sin(r) * Math.cos(theta),\n\t);\n\tconst lng2 =\n\t\tlng1 +\n\t\tMath.atan2(\n\t\t\tMath.sin(theta) * Math.sin(r) * Math.cos(lat1),\n\t\t\tMath.cos(r) - Math.sin(lat1) * Math.sin(lat2),\n\t\t);\n\n\treturn {\n\t\tlat: (lat2 * 180) / Math.PI,\n\t\tlng: (lng2 * 180) / Math.PI,\n\t};\n}\n\nfunction coordinateOutsideRadius(\n\tcenter: Coordinate,\n\tminRadiusMeters: number,\n\tmaxRadiusMeters: number,\n): Coordinate {\n\t// Earth's radius in meters\n\tconst earth = 6378137;\n\n\t// Convert radii from meters to radians\n\tconst minD = minRadiusMeters / earth;\n\tconst maxD = maxRadiusMeters / earth;\n\n\t// Random bearing\n\tconst theta = 2 * Math.PI * Math.random();\n\n\t// Random distance in annular ring (uniform distribution by area)\n\t// For uniform distribution in annulus: r = sqrt(r_min² + (r_max² - r_min²) * random)\n\tconst r = Math.sqrt(\n\t\tminD * minD + (maxD * maxD - minD * minD) * Math.random(),\n\t);\n\n\tconst lat1 = (center.lat * Math.PI) / 180;\n\tconst lng1 = (center.lng * Math.PI) / 180;\n\n\tconst lat2 = Math.asin(\n\t\tMath.sin(lat1) * Math.cos(r) +\n\t\t\tMath.cos(lat1) * Math.sin(r) * Math.cos(theta),\n\t);\n\tconst lng2 =\n\t\tlng1 +\n\t\tMath.atan2(\n\t\t\tMath.sin(theta) * Math.sin(r) * Math.cos(lat1),\n\t\t\tMath.cos(r) - Math.sin(lat1) * Math.sin(lat2),\n\t\t);\n\n\t// Normalize longitude to [-180, 180]\n\tconst normalizedLng = (((lng2 * 180) / Math.PI + 540) % 360) - 180;\n\n\treturn {\n\t\tlat: (lat2 * 180) / Math.PI,\n\t\tlng: normalizedLng,\n\t};\n}\n\n/**\n * Enhanced faker instance with additional utility methods for testing.\n * Extends @faker-js/faker with custom methods for common test data generation patterns.\n *\n * @example\n * ```typescript\n * import { faker } from '@geekmidas/testkit';\n *\n * // Use standard faker methods\n * const name = faker.person.fullName();\n * const email = faker.internet.email();\n *\n * // Use custom extensions\n * const { createdAt, updatedAt } = faker.timestamps();\n * const id = faker.identifier('user');\n * const orderNumber = faker.sequence('order');\n * const productPrice = faker.price();\n * ```\n */\nexport const faker = Object.freeze(\n\tObject.assign({}, baseFaker, {\n\t\ttimestamps,\n\t\tidentifier,\n\t\tsequence,\n\t\tresetSequence,\n\t\tresetAllSequences,\n\t\tprice,\n\t\tcoordinates: {\n\t\t\twithin: coordinateInRadius,\n\t\t\toutside: coordinateOutsideRadius,\n\t\t},\n\t}),\n);\n\n/**\n * Type definition for timestamp fields.\n * Used by the timestamps() function to generate date fields.\n */\nexport type Timestamps = {\n\t/** The creation date */\n\tcreatedAt: Date;\n\t/** The last update date */\n\tupdatedAt: Date;\n};\n\n/**\n * Type definition for the enhanced faker factory.\n * Includes all standard faker methods plus custom extensions.\n */\nexport type FakerFactory = typeof faker;\n"],"mappings":"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"}
|
package/dist/faker.d.cts
CHANGED
|
@@ -1,2 +1,162 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
//#region src/faker.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Generates random timestamp fields for database records.
|
|
4
|
+
* Creates a createdAt date in the past and an updatedAt date between creation and now.
|
|
5
|
+
* Milliseconds are set to 0 for cleaner database storage.
|
|
6
|
+
*
|
|
7
|
+
* @returns Object with createdAt and updatedAt Date fields
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* const { createdAt, updatedAt } = timestamps();
|
|
12
|
+
* console.log(createdAt); // 2023-05-15T10:30:00.000Z
|
|
13
|
+
* console.log(updatedAt); // 2023-11-20T14:45:00.000Z
|
|
14
|
+
*
|
|
15
|
+
* // Use in factory
|
|
16
|
+
* const user = {
|
|
17
|
+
* name: 'John Doe',
|
|
18
|
+
* ...timestamps()
|
|
19
|
+
* };
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export declare function timestamps(): Timestamps;
|
|
23
|
+
/**
|
|
24
|
+
* Generates a reverse domain name identifier.
|
|
25
|
+
* Useful for creating unique identifiers that follow domain naming conventions.
|
|
26
|
+
*
|
|
27
|
+
* @param suffix - Optional suffix to append to the identifier
|
|
28
|
+
* @returns A reverse domain name string (e.g., "com.example.feature123")
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* console.log(identifier()); // "com.example.widget1"
|
|
33
|
+
* console.log(identifier('user')); // "org.acme.user"
|
|
34
|
+
* console.log(identifier('api')); // "net.demo.api"
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export declare function identifier(suffix?: string): string;
|
|
38
|
+
/**
|
|
39
|
+
* Generates sequential numbers for a named sequence.
|
|
40
|
+
* Useful for creating unique IDs or numbered test data.
|
|
41
|
+
* Each named sequence maintains its own counter.
|
|
42
|
+
*
|
|
43
|
+
* @param name - The sequence name (default: 'default')
|
|
44
|
+
* @returns The next number in the sequence
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```typescript
|
|
48
|
+
* console.log(sequence()); // 1
|
|
49
|
+
* console.log(sequence()); // 2
|
|
50
|
+
* console.log(sequence('user')); // 1
|
|
51
|
+
* console.log(sequence('user')); // 2
|
|
52
|
+
* console.log(sequence()); // 3
|
|
53
|
+
*
|
|
54
|
+
* // Use in factories
|
|
55
|
+
* const email = `user${sequence('email')}@example.com`;
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
export declare function sequence(name?: string): number;
|
|
59
|
+
/**
|
|
60
|
+
* Resets a named sequence counter to a specific value.
|
|
61
|
+
* Useful for resetting sequences between test suites.
|
|
62
|
+
*
|
|
63
|
+
* @param name - The sequence name to reset (default: 'default')
|
|
64
|
+
* @param value - The new starting value (default: 0)
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```typescript
|
|
68
|
+
* sequence('user'); // 1
|
|
69
|
+
* sequence('user'); // 2
|
|
70
|
+
* resetSequence('user');
|
|
71
|
+
* sequence('user'); // 1
|
|
72
|
+
*
|
|
73
|
+
* resetSequence('order', 1000);
|
|
74
|
+
* sequence('order'); // 1001
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
export declare function resetSequence(name?: string, value?: number): void;
|
|
78
|
+
/**
|
|
79
|
+
* Resets all sequence counters.
|
|
80
|
+
* Useful for cleaning up between test suites to ensure predictable sequences.
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```typescript
|
|
84
|
+
* // In test setup
|
|
85
|
+
* beforeEach(() => {
|
|
86
|
+
* resetAllSequences();
|
|
87
|
+
* });
|
|
88
|
+
*
|
|
89
|
+
* it('starts sequences from 1', () => {
|
|
90
|
+
* expect(sequence()).toBe(1);
|
|
91
|
+
* expect(sequence('user')).toBe(1);
|
|
92
|
+
* });
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
export declare function resetAllSequences(): void;
|
|
96
|
+
/**
|
|
97
|
+
* Generates a random price as a number.
|
|
98
|
+
* Converts faker's string price to a numeric value.
|
|
99
|
+
*
|
|
100
|
+
* @returns A random price number
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```typescript
|
|
104
|
+
* const productPrice = price(); // 29.99
|
|
105
|
+
* const total = price() * quantity; // Numeric calculation
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
declare function price(): number;
|
|
109
|
+
type Coordinate = {
|
|
110
|
+
lat: number;
|
|
111
|
+
lng: number;
|
|
112
|
+
};
|
|
113
|
+
export declare function coordinateInRadius(center: Coordinate, radius: number): Coordinate;
|
|
114
|
+
declare function coordinateOutsideRadius(center: Coordinate, minRadiusMeters: number, maxRadiusMeters: number): Coordinate;
|
|
115
|
+
/**
|
|
116
|
+
* Enhanced faker instance with additional utility methods for testing.
|
|
117
|
+
* Extends @faker-js/faker with custom methods for common test data generation patterns.
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```typescript
|
|
121
|
+
* import { faker } from '@geekmidas/testkit';
|
|
122
|
+
*
|
|
123
|
+
* // Use standard faker methods
|
|
124
|
+
* const name = faker.person.fullName();
|
|
125
|
+
* const email = faker.internet.email();
|
|
126
|
+
*
|
|
127
|
+
* // Use custom extensions
|
|
128
|
+
* const { createdAt, updatedAt } = faker.timestamps();
|
|
129
|
+
* const id = faker.identifier('user');
|
|
130
|
+
* const orderNumber = faker.sequence('order');
|
|
131
|
+
* const productPrice = faker.price();
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
export declare const faker: Readonly<import("@faker-js/faker").Faker & {
|
|
135
|
+
timestamps: typeof timestamps;
|
|
136
|
+
identifier: typeof identifier;
|
|
137
|
+
sequence: typeof sequence;
|
|
138
|
+
resetSequence: typeof resetSequence;
|
|
139
|
+
resetAllSequences: typeof resetAllSequences;
|
|
140
|
+
price: typeof price;
|
|
141
|
+
coordinates: {
|
|
142
|
+
within: typeof coordinateInRadius;
|
|
143
|
+
outside: typeof coordinateOutsideRadius;
|
|
144
|
+
};
|
|
145
|
+
}>;
|
|
146
|
+
/**
|
|
147
|
+
* Type definition for timestamp fields.
|
|
148
|
+
* Used by the timestamps() function to generate date fields.
|
|
149
|
+
*/
|
|
150
|
+
export type Timestamps = {
|
|
151
|
+
/** The creation date */
|
|
152
|
+
createdAt: Date;
|
|
153
|
+
/** The last update date */
|
|
154
|
+
updatedAt: Date;
|
|
155
|
+
};
|
|
156
|
+
/**
|
|
157
|
+
* Type definition for the enhanced faker factory.
|
|
158
|
+
* Includes all standard faker methods plus custom extensions.
|
|
159
|
+
*/
|
|
160
|
+
export type FakerFactory = typeof faker;
|
|
161
|
+
//#endregion
|
|
162
|
+
//# sourceMappingURL=faker.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"faker.d.cts","names":[],"sources":["../src/faker.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;wBAkFgB,cAAc;;;;;;;;;;;;;;;wBA2Bd,WAAW;;;;;;;;;;;;;;;;;;;;;wBAmCX,SAAS;;;;;;;;;;;;;;;;;;;wBA2BT,cAAc,eAAkB;;;;;;;;;;;;;;;;;;wBA0BhC;;;;;;;;;;;;;iBAgBP;KAIJ;EACJ;EACA;;wBAGe,mBACf,QAAQ,YACR,iBACE;iBA8BM,wBACR,QAAQ,YACR,yBACA,0BACE;;;;;;;;;;;;;;;;;;;;qBA2DU,OAAK,mCAAA;;;;;;;;IASf,eAAM;IACN,gBAAO;;;;;;;YASE;;EAEX,WAAW;;EAEX,WAAW;;;;;;YAOA,sBAAsB"}
|