@geekmidas/testkit 10.0.0-alpha.14 → 10.0.0-alpha.141
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 +85 -0
- package/dist/KyselyFactory.d.cts.map +1 -1
- package/dist/KyselyFactory.d.mts.map +1 -1
- package/dist/ObjectionFactory.d.cts.map +1 -1
- package/dist/ObjectionFactory.d.mts.map +1 -1
- package/dist/PostgresKyselyMigrator.d.cts +1 -1
- package/dist/PostgresKyselyMigrator.d.cts.map +1 -1
- package/dist/PostgresKyselyMigrator.d.mts +1 -1
- package/dist/PostgresKyselyMigrator.d.mts.map +1 -1
- package/dist/{PostgresMigrator-CoHQ95cT.d.cts → PostgresMigrator-CDPnnwMJ.d.cts} +10 -3
- package/dist/PostgresMigrator-CDPnnwMJ.d.cts.map +1 -0
- package/dist/{PostgresMigrator-CoHQ95cT.d.mts → PostgresMigrator-CDPnnwMJ.d.mts} +10 -3
- package/dist/PostgresMigrator-CDPnnwMJ.d.mts.map +1 -0
- package/dist/PostgresMigrator.cjs +14 -5
- package/dist/PostgresMigrator.cjs.map +1 -1
- package/dist/PostgresMigrator.d.cts +1 -1
- package/dist/PostgresMigrator.d.mts +1 -1
- package/dist/PostgresMigrator.mjs +14 -5
- package/dist/PostgresMigrator.mjs.map +1 -1
- package/dist/PostgresObjectionMigrator.d.cts +1 -1
- package/dist/PostgresObjectionMigrator.d.cts.map +1 -1
- package/dist/PostgresObjectionMigrator.d.mts +1 -1
- package/dist/PostgresObjectionMigrator.d.mts.map +1 -1
- package/dist/VitestTransactionIsolator.d.cts.map +1 -1
- package/dist/VitestTransactionIsolator.d.mts.map +1 -1
- package/dist/better-auth.d.cts.map +1 -1
- package/dist/better-auth.d.mts.map +1 -1
- package/dist/browser.cjs +211 -0
- package/dist/browser.cjs.map +1 -0
- package/dist/browser.d.cts +113 -0
- package/dist/browser.d.cts.map +1 -0
- package/dist/browser.d.mts +113 -0
- package/dist/browser.d.mts.map +1 -0
- package/dist/browser.mjs +207 -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 +51 -0
- package/dist/faker.cjs.map +1 -1
- package/dist/faker.d.cts +26 -19
- package/dist/faker.d.cts.map +1 -1
- package/dist/faker.d.mts +26 -19
- package/dist/faker.d.mts.map +1 -1
- package/dist/faker.mjs +51 -1
- package/dist/faker.mjs.map +1 -1
- package/dist/mailbox.cjs +93 -0
- package/dist/mailbox.cjs.map +1 -0
- package/dist/mailbox.d.cts +52 -0
- package/dist/mailbox.d.cts.map +1 -0
- package/dist/mailbox.d.mts +52 -0
- package/dist/mailbox.d.mts.map +1 -0
- package/dist/mailbox.mjs +90 -0
- package/dist/mailbox.mjs.map +1 -0
- package/dist/requestContext.d.cts.map +1 -1
- package/dist/requestContext.d.mts.map +1 -1
- package/dist/transactions.cjs +239 -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 +234 -0
- package/dist/transactions.mjs.map +1 -0
- package/package.json +57 -8
- package/dist/PostgresMigrator-CoHQ95cT.d.cts.map +0 -1
- package/dist/PostgresMigrator-CoHQ95cT.d.mts.map +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser.mjs","names":[],"sources":["../src/browser.ts"],"sourcesContent":["/**\n * A browser, for tests: a `fetch` that keeps cookies the way a browser does.\n *\n * An app's real clients — its typed API client, better-auth's client — run on\n * whatever `fetch` they are given. Give them this one and a test signs in the\n * way a person does: the auth server's `Set-Cookie` lands in the jar, and every\n * later request to a URL the cookie belongs to carries it. The jar follows the\n * browser's rules (RFC 6265 — Domain, Path, Secure, Expires), so a cookie set\n * for the wrong domain is not sent, exactly as a browser would not send it.\n *\n * Extend it once per app with that app's clients:\n *\n * ```ts\n * export class Browser extends TestBrowser {\n * readonly api = createApi({ baseURL: process.env.API_URL, fetch: this.fetch });\n * }\n * ```\n */\n\nimport { randomInt } from 'node:crypto';\nimport { CookieJar } from 'tough-cookie';\nimport { currentTestContext } from './context';\n\n/** How many redirects `fetch` follows before giving up, as browsers do. */\nconst MAX_REDIRECTS = 20;\n\nexport interface BrowserOptions {\n\t/**\n\t * The `fetch` requests go out on. Defaults to the global one at the time\n\t * the browser is created — so a browser made inside a test goes through\n\t * whatever the test installed (MSW, the context stamp) and never through\n\t * itself once installed.\n\t */\n\tfetch?: typeof fetch;\n\t/**\n\t * The address this browser connects from. A fresh one by default, from a\n\t * private range nothing routes to — see {@link Browser.address}.\n\t */\n\taddress?: string;\n\t/**\n\t * The cookies it starts with — another browser's jar, to be the same\n\t * person on another tab. A fresh jar by default.\n\t */\n\tjar?: CookieJar;\n\t/**\n\t * The address of the app's site on every subdomain,\n\t * `https://*.test.shop.localhost:8443`, which {@link Browser.on} puts a\n\t * tenant's slug into. A feature test passes it when the app declares one.\n\t */\n\tsite?: string;\n\t/**\n\t * The page this browser is on: sent as `Origin` on every request that does\n\t * not set its own, and what a relative {@link Browser.visit} resolves\n\t * against. See {@link Browser.on}.\n\t */\n\torigin?: string;\n}\n\nexport class Browser {\n\t/** The cookies this browser holds. */\n\treadonly jar: CookieJar;\n\n\t/**\n\t * The page this browser is on, sent as `Origin` — what a surface's CORS\n\t * and an auth server's trusted origins check, and what a multi-tenant app\n\t * reads its tenant from. None until {@link Browser.on} gives it one.\n\t */\n\treadonly origin?: string;\n\n\t/**\n\t * The address this browser connects from, sent as `x-forwarded-for` the way\n\t * a proxy in front of the app adds it. Each browser is a different person on\n\t * a different connection, so what an app keys by client — an auth server's\n\t * rate limit — counts each on its own. Without it every browser was the same\n\t * client, and concurrent tests queued on the same rate-limit row of each\n\t * other's open transactions. A request that sets the header itself — a test\n\t * that is several machines — keeps its own.\n\t */\n\treadonly address: string;\n\n\t/**\n\t * This browser's `fetch`: cookies from the jar on the way out, `Set-Cookie`\n\t * into the jar on the way back, redirects followed hop by hop so each hop's\n\t * cookies are kept.\n\t *\n\t * On the server side of a test — a handler serving a request — it is the\n\t * plain `fetch`: a server forwards what it was handed and never reaches into\n\t * a browser's jar.\n\t */\n\treadonly fetch: typeof fetch;\n\n\tprivate readonly send: typeof fetch;\n\tprivate readonly site?: string;\n\n\tconstructor(options: BrowserOptions = {}) {\n\t\tthis.send = options.fetch ?? globalThis.fetch;\n\t\tthis.address = options.address ?? privateAddress();\n\t\tthis.jar = options.jar ?? new CookieJar();\n\t\tif (options.site) this.site = options.site;\n\t\tif (options.origin) this.origin = options.origin;\n\t\tthis.fetch = (input, init) => {\n\t\t\tif (currentTestContext()?.side === 'server') {\n\t\t\t\treturn this.send(input, init);\n\t\t\t}\n\t\t\treturn this.start(new Request(input, init), init?.redirect);\n\t\t};\n\t}\n\n\t/**\n\t * Open a URL the way clicking a link does: a GET, redirects followed. A\n\t * path is opened on the page this browser is on.\n\t */\n\tvisit(url: string | URL): Promise<Response> {\n\t\treturn this.fetch(\n\t\t\tthis.origin ? new URL(url, this.origin).toString() : String(url),\n\t\t);\n\t}\n\n\t/**\n\t * The same person on a tenant's host: this browser — its cookies, its\n\t * address, its clients — on `https://<slug>.<domain>`, the app's site on\n\t * every subdomain. Each request it makes says so in `Origin`, as a page on\n\t * that host would, so a test signs in once and calls the app as two\n\t * tenants:\n\t *\n\t * ```ts\n\t * await browser.signIn();\n\t * await browser.on('acme').api.post('/orders', { body });\n\t * expect(await browser.on('globex').api.get('/orders')).toEqual([]);\n\t * ```\n\t */\n\ton(slug: string): this {\n\t\tif (!this.site) throw new NoWildcardSite(slug);\n\t\tif (!SLUG.test(slug)) throw new NotATenantSlug(slug);\n\n\t\tconst origin = new URL(this.site.replace('*', slug)).origin;\n\t\tconst Same = this.constructor as new (options: BrowserOptions) => this;\n\t\treturn new Same({\n\t\t\tfetch: this.send,\n\t\t\taddress: this.address,\n\t\t\tjar: this.jar,\n\t\t\tsite: this.site,\n\t\t\torigin,\n\t\t});\n\t}\n\n\t/** The cookies this browser would send to `url`, as a header value. */\n\tcookieHeader(url: string | URL): Promise<string> {\n\t\treturn this.jar.getCookieString(String(url));\n\t}\n\n\t/**\n\t * Make this browser the global `fetch`, for code that uses the global one —\n\t * an app's module-level clients. Returns what puts the previous one back.\n\t */\n\tinstall(): () => void {\n\t\tconst previous = globalThis.fetch;\n\t\tglobalThis.fetch = this.fetch;\n\t\treturn () => {\n\t\t\tglobalThis.fetch = previous;\n\t\t};\n\t}\n\n\t/** Read the body once, so a redirect that keeps the method can send it again. */\n\tprivate async start(\n\t\trequest: Request,\n\t\tredirect: RequestInit['redirect'],\n\t): Promise<Response> {\n\t\tconst body =\n\t\t\trequest.method === 'GET' || request.method === 'HEAD'\n\t\t\t\t? undefined\n\t\t\t\t: await request.arrayBuffer();\n\t\tconst headers = new Headers(request.headers);\n\t\tif (!headers.has('x-forwarded-for')) {\n\t\t\theaders.set('x-forwarded-for', this.address);\n\t\t}\n\t\tif (this.origin && !headers.has('origin')) {\n\t\t\theaders.set('origin', this.origin);\n\t\t}\n\t\treturn this.request(\n\t\t\t{ url: request.url, method: request.method, headers, body },\n\t\t\tredirect,\n\t\t);\n\t}\n\n\tprivate async request(\n\t\trequest: Hop,\n\t\tredirect: RequestInit['redirect'],\n\t\thops = 0,\n\t): Promise<Response> {\n\t\tconst headers = new Headers(request.headers);\n\t\tconst cookie = await this.jar.getCookieString(request.url);\n\t\tif (cookie) {\n\t\t\tconst existing = headers.get('cookie');\n\t\t\theaders.set('cookie', existing ? `${existing}; ${cookie}` : cookie);\n\t\t}\n\n\t\tconst response = await this.send(request.url, {\n\t\t\tmethod: request.method,\n\t\t\theaders,\n\t\t\tbody: request.body,\n\t\t\tredirect: 'manual',\n\t\t});\n\n\t\tfor (const set of response.headers.getSetCookie()) {\n\t\t\t// A cookie a browser would refuse — the wrong domain, Secure over\n\t\t\t// plain HTTP — is dropped silently, as a browser drops it.\n\t\t\tawait this.jar.setCookie(set, request.url, { ignoreError: true });\n\t\t}\n\n\t\tconst location = response.headers.get('location');\n\t\tif (\n\t\t\tredirect === 'manual' ||\n\t\t\t!location ||\n\t\t\tresponse.status < 300 ||\n\t\t\tresponse.status >= 400\n\t\t) {\n\t\t\treturn response;\n\t\t}\n\n\t\tif (hops >= MAX_REDIRECTS) {\n\t\t\tthrow new TooManyRedirects(request.url, MAX_REDIRECTS);\n\t\t}\n\n\t\t// 303, and 301/302 after a POST, continue as a GET without a body — what\n\t\t// every browser does, whatever the spec once said about 302.\n\t\tconst keepsMethod =\n\t\t\tresponse.status === 307 ||\n\t\t\tresponse.status === 308 ||\n\t\t\trequest.method === 'GET' ||\n\t\t\trequest.method === 'HEAD';\n\n\t\tconst url = new URL(location, request.url).toString();\n\t\tconst next: Hop = keepsMethod\n\t\t\t? { ...request, url }\n\t\t\t: { url, method: 'GET', headers: withoutBody(request.headers) };\n\n\t\treturn this.request(next, redirect, hops + 1);\n\t}\n}\n\n/** One request in a redirect chain, with its body already read. */\ninterface Hop {\n\turl: string;\n\tmethod: string;\n\theaders: Headers;\n\tbody?: ArrayBuffer;\n}\n\n/** Headers for a request that no longer carries the body they described. */\nfunction withoutBody(headers: Headers): Headers {\n\tconst next = new Headers(headers);\n\tnext.delete('content-type');\n\tnext.delete('content-length');\n\treturn next;\n}\n\n/** A redirect chain longer than any browser follows. */\nexport class TooManyRedirects extends Error {\n\tconstructor(\n\t\treadonly url: string,\n\t\treadonly limit: number,\n\t) {\n\t\tsuper(\n\t\t\t`${url} redirected more than ${limit} times. A browser gives up here ` +\n\t\t\t\t`too — look for a redirect loop.`,\n\t\t);\n\t\tthis.name = 'TooManyRedirects';\n\t}\n}\n\n/** One DNS label, as a tenant's slug is. */\nconst SLUG = /^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/;\n\n/** `browser.on(slug)` in an app with no site on every subdomain. */\nexport class NoWildcardSite extends Error {\n\tconstructor(readonly slug: string) {\n\t\tsuper(\n\t\t\t`browser.on('${slug}') puts a tenant's slug into the app's site on ` +\n\t\t\t\t`every subdomain, and this browser knows of none. Declare a ` +\n\t\t\t\t`StaticSite with subdomain: '*', or pass its address as \\`site\\`.`,\n\t\t);\n\t\tthis.name = 'NoWildcardSite';\n\t}\n}\n\n/** `browser.on(slug)` with something no host can start with. */\nexport class NotATenantSlug extends Error {\n\tconstructor(readonly slug: string) {\n\t\tsuper(\n\t\t\t`'${slug}' is not a tenant's slug: one DNS label of lowercase ` +\n\t\t\t\t`letters, digits and inner hyphens, such as 'acme'.`,\n\t\t);\n\t\tthis.name = 'NotATenantSlug';\n\t}\n}\n\n/**\n * An address from 10.0.0.0/8, which nothing routes to. Random rather than\n * counted: test files run in separate workers that share no counter.\n */\nfunction privateAddress(): string {\n\tconst n = randomInt(1, 2 ** 24 - 1);\n\treturn `10.${(n >> 16) & 255}.${(n >> 8) & 255}.${n & 255}`;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAwBA,MAAM,gBAAgB;AAkCtB,IAAa,UAAb,MAAqB;;CAEpB;;;;;;CAOA;;;;;;;;;;CAWA;;;;;;;;;;CAWA;CAEA;CACA;CAEA,YAAY,UAA0B,CAAC,GAAG;EACzC,KAAK,OAAO,QAAQ,SAAS,WAAW;EACxC,KAAK,UAAU,QAAQ,WAAW,eAAe;EACjD,KAAK,MAAM,QAAQ,OAAO,IAAI,UAAU;EACxC,IAAI,QAAQ,MAAM,KAAK,OAAO,QAAQ;EACtC,IAAI,QAAQ,QAAQ,KAAK,SAAS,QAAQ;EAC1C,KAAK,SAAS,OAAO,SAAS;GAC7B,IAAI,mBAAmB,CAAC,EAAE,SAAS,UAClC,OAAO,KAAK,KAAK,OAAO,IAAI;GAE7B,OAAO,KAAK,MAAM,IAAI,QAAQ,OAAO,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AK,OAAO;CACb;AACD;;AAGA,MAAM,OAAO;;AAGb,IAAa,iBAAb,cAAoC,MAAM;CACpB;CAArB,YAAY,MAAuB;EAClC,MACC,eAAe,KAAK,2KAGrB;EALoB,KAAA,OAAA;EAMpB,KAAK,OAAO;CACb;AACD;;AAGA,IAAa,iBAAb,cAAoC,MAAM;CACpB;CAArB,YAAY,MAAuB;EAClC,MACC,IAAI,KAAK,wGAEV;EAJoB,KAAA,OAAA;EAKpB,KAAK,OAAO;CACb;AACD;;;;;AAMA,SAAS,iBAAyB;CACjC,MAAM,IAAI,UAAU,GAAG,KAAK,KAAK,CAAC;CAClC,OAAO,MAAO,KAAK,KAAM,IAAI,GAAI,KAAK,IAAK,IAAI,GAAG,IAAI;AACvD"}
|
package/dist/context.cjs
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let node_async_hooks = require("node:async_hooks");
|
|
3
|
+
//#region src/context.ts
|
|
4
|
+
/**
|
|
5
|
+
* The test a request belongs to.
|
|
6
|
+
*
|
|
7
|
+
* A feature test drives an app the way it runs deployed: a browser calls the
|
|
8
|
+
* API, the API calls the auth server, each over a URL. In a test those URLs are
|
|
9
|
+
* served in-process, and every request made on behalf of one test has to reach
|
|
10
|
+
* *that test's* transactions — its own call to the API, and the API's call to
|
|
11
|
+
* the auth server made while handling it.
|
|
12
|
+
*
|
|
13
|
+
* The id travels two ways. In-process, an `AsyncLocalStorage` carries it
|
|
14
|
+
* through every await a test and its handlers make. On the wire it is the
|
|
15
|
+
* `x-test-context-id` header, which a stamped `fetch` adds to every request made
|
|
16
|
+
* inside a context, so whatever serves the URL can find the context again —
|
|
17
|
+
* without any caller, including code under test, writing the header.
|
|
18
|
+
*/
|
|
19
|
+
/** The header a test context travels in between client and server. */
|
|
20
|
+
const TEST_CONTEXT_HEADER = "x-test-context-id";
|
|
21
|
+
const storage = new node_async_hooks.AsyncLocalStorage();
|
|
22
|
+
/** The context the running code belongs to, if any. */
|
|
23
|
+
function currentTestContext() {
|
|
24
|
+
return storage.getStore();
|
|
25
|
+
}
|
|
26
|
+
/** Run `fn` as the client side of the test `id`. */
|
|
27
|
+
function runInTestContext(id, fn) {
|
|
28
|
+
return storage.run({
|
|
29
|
+
id,
|
|
30
|
+
side: "client"
|
|
31
|
+
}, fn);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Run `fn` as a server handling a request for the test `id` — what whatever
|
|
35
|
+
* serves a URL in-process wraps each request in.
|
|
36
|
+
*/
|
|
37
|
+
function runAsServer(id, fn) {
|
|
38
|
+
return storage.run({
|
|
39
|
+
id,
|
|
40
|
+
side: "server"
|
|
41
|
+
}, fn);
|
|
42
|
+
}
|
|
43
|
+
/** The test a request was made for, read off its header. */
|
|
44
|
+
function testContextOf(request) {
|
|
45
|
+
return request.headers.get("x-test-context-id") ?? void 0;
|
|
46
|
+
}
|
|
47
|
+
/** `fetch`, adding the current test's id to every request made inside one. */
|
|
48
|
+
function stampedFetch(inner) {
|
|
49
|
+
return (input, init) => {
|
|
50
|
+
const context = currentTestContext();
|
|
51
|
+
if (!context) return inner(input, init);
|
|
52
|
+
const request = new Request(input, init);
|
|
53
|
+
if (!request.headers.has("x-test-context-id")) request.headers.set(TEST_CONTEXT_HEADER, context.id);
|
|
54
|
+
return inner(request);
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Stamp the global `fetch` with the current test's id.
|
|
59
|
+
*
|
|
60
|
+
* Install it after anything else that patches `fetch` — MSW's interceptor in
|
|
61
|
+
* particular — so the stamp is added before the request is intercepted.
|
|
62
|
+
* Returns what puts the previous `fetch` back.
|
|
63
|
+
*/
|
|
64
|
+
function installContextFetch() {
|
|
65
|
+
const previous = globalThis.fetch;
|
|
66
|
+
globalThis.fetch = stampedFetch(previous);
|
|
67
|
+
return () => {
|
|
68
|
+
globalThis.fetch = previous;
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
//#endregion
|
|
72
|
+
exports.TEST_CONTEXT_HEADER = TEST_CONTEXT_HEADER;
|
|
73
|
+
exports.currentTestContext = currentTestContext;
|
|
74
|
+
exports.installContextFetch = installContextFetch;
|
|
75
|
+
exports.runAsServer = runAsServer;
|
|
76
|
+
exports.runInTestContext = runInTestContext;
|
|
77
|
+
exports.stampedFetch = stampedFetch;
|
|
78
|
+
exports.testContextOf = testContextOf;
|
|
79
|
+
|
|
80
|
+
//# sourceMappingURL=context.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.cjs","names":["AsyncLocalStorage"],"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,IAAIA,iBAAAA,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"}
|
|
@@ -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.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.d.cts","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"}
|
|
@@ -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
|
@@ -192,6 +192,41 @@ function resetAllSequences() {
|
|
|
192
192
|
function price() {
|
|
193
193
|
return +faker.commerce.price();
|
|
194
194
|
}
|
|
195
|
+
/**
|
|
196
|
+
* A birthdate for someone of the given age today: exactly `min` years old, or
|
|
197
|
+
* anywhere from `min` to `max` inclusive.
|
|
198
|
+
*
|
|
199
|
+
* Through faker's own `date.birthdate`, so seeding faker makes it repeatable.
|
|
200
|
+
*
|
|
201
|
+
* @param min - The youngest age, in whole years
|
|
202
|
+
* @param max - The oldest age, in whole years (default: `min`)
|
|
203
|
+
* @returns The birthdate
|
|
204
|
+
*
|
|
205
|
+
* @example
|
|
206
|
+
* ```typescript
|
|
207
|
+
* faker.age(18); // someone who is 18 today
|
|
208
|
+
* faker.age(18, 24); // someone aged 18 to 24
|
|
209
|
+
* ```
|
|
210
|
+
*/
|
|
211
|
+
function age(min, max = min) {
|
|
212
|
+
if (min < 0 || max < min) throw new AgeRangeInvalid(min, max);
|
|
213
|
+
return _faker_js_faker.faker.date.birthdate({
|
|
214
|
+
mode: "age",
|
|
215
|
+
min,
|
|
216
|
+
max
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
/** An age range no one can be: a negative age, or an oldest below a youngest. */
|
|
220
|
+
var AgeRangeInvalid = class extends Error {
|
|
221
|
+
min;
|
|
222
|
+
max;
|
|
223
|
+
constructor(min, max) {
|
|
224
|
+
super(`faker.age(${min}, ${max}) asks for an age no one can be. Pass the youngest age first and the oldest second, neither negative: faker.age(18) or faker.age(18, 24).`);
|
|
225
|
+
this.min = min;
|
|
226
|
+
this.max = max;
|
|
227
|
+
this.name = "AgeRangeInvalid";
|
|
228
|
+
}
|
|
229
|
+
};
|
|
195
230
|
function coordinateInRadius(center, radius) {
|
|
196
231
|
const d = radius / 6378137;
|
|
197
232
|
const theta = 2 * Math.PI * Math.random();
|
|
@@ -239,19 +274,35 @@ function coordinateOutsideRadius(center, minRadiusMeters, maxRadiusMeters) {
|
|
|
239
274
|
* const productPrice = faker.price();
|
|
240
275
|
* ```
|
|
241
276
|
*/
|
|
277
|
+
/**
|
|
278
|
+
* `faker.internet`, with every address lowercase.
|
|
279
|
+
*
|
|
280
|
+
* An auth server stores addresses lowercased — Better Auth does — so a test
|
|
281
|
+
* that signs in as faker's `Ada.Lovelace@…` and reads back `ada.lovelace@…`
|
|
282
|
+
* fails only when faker happened to capitalise: a flake, not a bug. The rest
|
|
283
|
+
* of the module is faker's own, through the prototype.
|
|
284
|
+
*/
|
|
285
|
+
const internet = Object.assign(Object.create(_faker_js_faker.faker.internet), {
|
|
286
|
+
email: (...args) => _faker_js_faker.faker.internet.email(...args).toLowerCase(),
|
|
287
|
+
exampleEmail: (...args) => _faker_js_faker.faker.internet.exampleEmail(...args).toLowerCase()
|
|
288
|
+
});
|
|
242
289
|
const faker = Object.freeze(Object.assign({}, _faker_js_faker.faker, {
|
|
290
|
+
internet,
|
|
291
|
+
seed: (seed) => _faker_js_faker.faker.seed(seed),
|
|
243
292
|
timestamps,
|
|
244
293
|
identifier,
|
|
245
294
|
sequence,
|
|
246
295
|
resetSequence,
|
|
247
296
|
resetAllSequences,
|
|
248
297
|
price,
|
|
298
|
+
age,
|
|
249
299
|
coordinates: {
|
|
250
300
|
within: coordinateInRadius,
|
|
251
301
|
outside: coordinateOutsideRadius
|
|
252
302
|
}
|
|
253
303
|
}));
|
|
254
304
|
//#endregion
|
|
305
|
+
exports.AgeRangeInvalid = AgeRangeInvalid;
|
|
255
306
|
exports.coordinateInRadius = coordinateInRadius;
|
|
256
307
|
exports.faker = faker;
|
|
257
308
|
exports.identifier = identifier;
|
package/dist/faker.cjs.map
CHANGED
|
@@ -1 +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":";;;;;;;;;;;;;;;;;;AAmBA,IAAM,gBAAN,MAAoB;;;;;CAKnB;;;;;CAMA,YAAY,eAAe,GAAG;EAC7B,KAAK,QAAQ;CACd;;;;;CAMA,YAAoB;EAInB,OAAO,EAAE,KAAK;CACf;;;;;CAMA,MAAc;EACb,OAAO,KAAK;CACb;;;;;CAMA,MAAM,QAAQ,GAAS;EACtB,KAAK,QAAQ;CACd;AACD;;;;;;;;;;;;;;;;;;;;;AAsBA,SAAgB,aAAyB;CACxC,MAAM,YAAY,MAAM,KAAK,KAAK;CAClC,MAAM,YAAY,MAAM,KAAK,QAAQ;EACpC,MAAM;EACN,oBAAI,IAAI,KAAK;CACd,CAAC;CAED,U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C,CACF"}
|
|
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\n/**\n * A birthdate for someone of the given age today: exactly `min` years old, or\n * anywhere from `min` to `max` inclusive.\n *\n * Through faker's own `date.birthdate`, so seeding faker makes it repeatable.\n *\n * @param min - The youngest age, in whole years\n * @param max - The oldest age, in whole years (default: `min`)\n * @returns The birthdate\n *\n * @example\n * ```typescript\n * faker.age(18); // someone who is 18 today\n * faker.age(18, 24); // someone aged 18 to 24\n * ```\n */\nfunction age(min: number, max: number = min): Date {\n\tif (min < 0 || max < min) throw new AgeRangeInvalid(min, max);\n\n\treturn baseFaker.date.birthdate({ mode: 'age', min, max });\n}\n\n/** An age range no one can be: a negative age, or an oldest below a youngest. */\nexport class AgeRangeInvalid extends Error {\n\tconstructor(\n\t\treadonly min: number,\n\t\treadonly max: number,\n\t) {\n\t\tsuper(\n\t\t\t`faker.age(${min}, ${max}) asks for an age no one can be. Pass the ` +\n\t\t\t\t'youngest age first and the oldest second, neither negative: ' +\n\t\t\t\t'faker.age(18) or faker.age(18, 24).',\n\t\t);\n\t\tthis.name = 'AgeRangeInvalid';\n\t}\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 */\n/**\n * `faker.internet`, with every address lowercase.\n *\n * An auth server stores addresses lowercased — Better Auth does — so a test\n * that signs in as faker's `Ada.Lovelace@…` and reads back `ada.lovelace@…`\n * fails only when faker happened to capitalise: a flake, not a bug. The rest\n * of the module is faker's own, through the prototype.\n */\nconst internet: typeof baseFaker.internet = Object.assign(\n\tObject.create(baseFaker.internet),\n\t{\n\t\temail: (...args: Parameters<typeof baseFaker.internet.email>) =>\n\t\t\tbaseFaker.internet.email(...args).toLowerCase(),\n\t\texampleEmail: (\n\t\t\t...args: Parameters<typeof baseFaker.internet.exampleEmail>\n\t\t) => baseFaker.internet.exampleEmail(...args).toLowerCase(),\n\t},\n);\n\nexport const faker = Object.freeze(\n\tObject.assign({}, baseFaker, {\n\t\tinternet,\n\t\t// A prototype method, so the spread above leaves it behind. The modules\n\t\t// copied across still draw from `baseFaker`, so seeding it seeds them.\n\t\tseed: (seed?: number) => baseFaker.seed(seed),\n\t\ttimestamps,\n\t\tidentifier,\n\t\tsequence,\n\t\tresetSequence,\n\t\tresetAllSequences,\n\t\tprice,\n\t\tage,\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":";;;;;;;;;;;;;;;;;;AAmBA,IAAM,gBAAN,MAAoB;;;;;CAKnB;;;;;CAMA,YAAY,eAAe,GAAG;EAC7B,KAAK,QAAQ;CACd;;;;;CAMA,YAAoB;EAInB,OAAO,EAAE,KAAK;CACf;;;;;CAMA,MAAc;EACb,OAAO,KAAK;CACb;;;;;CAMA,MAAM,QAAQ,GAAS;EACtB,KAAK,QAAQ;CACd;AACD;;;;;;;;;;;;;;;;;;;;;AAsBA,SAAgB,aAAyB;CACxC,MAAM,YAAY,MAAM,KAAK,KAAK;CAClC,MAAM,YAAY,MAAM,KAAK,QAAQ;EACpC,MAAM;EACN,oBAAI,IAAI,KAAK;CACd,CAAC;CAED,UAAU,gBAAgB,CAAC;CAC3B,UAAU,gBAAgB,CAAC;CAE3B,OAAO;EAAE;EAAW;CAAU;AAC/B;;;;;;;;;;;;;;;AAgBA,SAAgB,WAAW,QAAyB;CACnD,OAAO;EACN,MAAM,SAAS,aAAa;EAC5B,MAAM,SAAS,WAAW;EAC1B,SAAS,SAAS,MAAM,SAAS,WAAW,IAAI,SAAS,YAAY;CACtE,CAAC,CAAC,KAAK,GAAG;AACX;;;;;;AAOA,MAAM,4BAAY,IAAI,IAA2B;;;;;;;;;;;;;;;;;;;;;AAsBjD,SAAgB,SAAS,OAAO,WAAmB;CAClD,IAAI,CAAC,UAAU,IAAI,IAAI,GACtB,UAAU,IAAI,MAAM,IAAI,cAAc,CAAC;CAIxC,OADgB,UAAU,IAAI,IACjB,CA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MAAa,QAAQ,OAAO,OAC3B,OAAO,OAAO,CAAC,GAAGA,gBAAAA,OAAW;CAC5B;CAGA,OAAO,SAAkBA,gBAAAA,MAAU,KAAK,IAAI;CAC5C;CACA;CACA;CACA;CACA;CACA;CACA;CACA,aAAa;EACZ,QAAQ;EACR,SAAS;CACV;AACD,CAAC,CACF"}
|
package/dist/faker.d.cts
CHANGED
|
@@ -106,38 +106,45 @@ export declare function resetAllSequences(): void;
|
|
|
106
106
|
* ```
|
|
107
107
|
*/
|
|
108
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
109
|
/**
|
|
116
|
-
*
|
|
117
|
-
*
|
|
110
|
+
* A birthdate for someone of the given age today: exactly `min` years old, or
|
|
111
|
+
* anywhere from `min` to `max` inclusive.
|
|
118
112
|
*
|
|
119
|
-
*
|
|
120
|
-
* ```typescript
|
|
121
|
-
* import { faker } from '@geekmidas/testkit';
|
|
113
|
+
* Through faker's own `date.birthdate`, so seeding faker makes it repeatable.
|
|
122
114
|
*
|
|
123
|
-
*
|
|
124
|
-
*
|
|
125
|
-
*
|
|
115
|
+
* @param min - The youngest age, in whole years
|
|
116
|
+
* @param max - The oldest age, in whole years (default: `min`)
|
|
117
|
+
* @returns The birthdate
|
|
126
118
|
*
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
-
*
|
|
131
|
-
* const productPrice = faker.price();
|
|
119
|
+
* @example
|
|
120
|
+
* ```typescript
|
|
121
|
+
* faker.age(18); // someone who is 18 today
|
|
122
|
+
* faker.age(18, 24); // someone aged 18 to 24
|
|
132
123
|
* ```
|
|
133
124
|
*/
|
|
125
|
+
declare function age(min: number, max?: number): Date;
|
|
126
|
+
/** An age range no one can be: a negative age, or an oldest below a youngest. */
|
|
127
|
+
export declare class AgeRangeInvalid extends Error {
|
|
128
|
+
readonly min: number;
|
|
129
|
+
readonly max: number;
|
|
130
|
+
constructor(min: number, max: number);
|
|
131
|
+
}
|
|
132
|
+
type Coordinate = {
|
|
133
|
+
lat: number;
|
|
134
|
+
lng: number;
|
|
135
|
+
};
|
|
136
|
+
export declare function coordinateInRadius(center: Coordinate, radius: number): Coordinate;
|
|
137
|
+
declare function coordinateOutsideRadius(center: Coordinate, minRadiusMeters: number, maxRadiusMeters: number): Coordinate;
|
|
134
138
|
export declare const faker: Readonly<import("@faker-js/faker").Faker & {
|
|
139
|
+
internet: import("@faker-js/faker").InternetModule;
|
|
140
|
+
seed: (seed?: number) => number;
|
|
135
141
|
timestamps: typeof timestamps;
|
|
136
142
|
identifier: typeof identifier;
|
|
137
143
|
sequence: typeof sequence;
|
|
138
144
|
resetSequence: typeof resetSequence;
|
|
139
145
|
resetAllSequences: typeof resetAllSequences;
|
|
140
146
|
price: typeof price;
|
|
147
|
+
age: typeof age;
|
|
141
148
|
coordinates: {
|
|
142
149
|
within: typeof coordinateInRadius;
|
|
143
150
|
outside: typeof coordinateOutsideRadius;
|
package/dist/faker.d.cts.map
CHANGED
|
@@ -1 +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;
|
|
1
|
+
{"version":3,"file":"faker.d.cts","names":[],"sources":["../src/faker.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;wBAkFgB,cAAc;;;;;;;;;;;;;;;wBA2Bd,WAAW;;;;;;;;;;;;;;;;;;;;;wBAmCX,SAAS;;;;;;;;;;;;;;;;;;;wBA2BT,cAAc,eAAkB;;;;;;;;;;;;;;;;;;wBA0BhC;;;;;;;;;;;;;iBAgBP;;;;;;;;;;;;;;;;;iBAoBA,IAAI,aAAa,eAAoB;;qBAOjC,wBAAwB;WAE1B;WACA;EAFV,YACU,aACA;;KAWN;EACJ;EACA;;wBAGe,mBACf,QAAQ,YACR,iBACE;iBA8BM,wBACR,QAAQ,YACR,yBACA,0BACE;qBA8EU,OAAK,mCAAA;;EAKF,OAAA;;;;;;;;;IASb,eAAM;IACN,gBAAO;;;;;;;YASE;;EAEX,WAAW;;EAEX,WAAW;;;;;;YAOA,sBAAsB"}
|
package/dist/faker.d.mts
CHANGED
|
@@ -106,38 +106,45 @@ export declare function resetAllSequences(): void;
|
|
|
106
106
|
* ```
|
|
107
107
|
*/
|
|
108
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
109
|
/**
|
|
116
|
-
*
|
|
117
|
-
*
|
|
110
|
+
* A birthdate for someone of the given age today: exactly `min` years old, or
|
|
111
|
+
* anywhere from `min` to `max` inclusive.
|
|
118
112
|
*
|
|
119
|
-
*
|
|
120
|
-
* ```typescript
|
|
121
|
-
* import { faker } from '@geekmidas/testkit';
|
|
113
|
+
* Through faker's own `date.birthdate`, so seeding faker makes it repeatable.
|
|
122
114
|
*
|
|
123
|
-
*
|
|
124
|
-
*
|
|
125
|
-
*
|
|
115
|
+
* @param min - The youngest age, in whole years
|
|
116
|
+
* @param max - The oldest age, in whole years (default: `min`)
|
|
117
|
+
* @returns The birthdate
|
|
126
118
|
*
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
-
*
|
|
131
|
-
* const productPrice = faker.price();
|
|
119
|
+
* @example
|
|
120
|
+
* ```typescript
|
|
121
|
+
* faker.age(18); // someone who is 18 today
|
|
122
|
+
* faker.age(18, 24); // someone aged 18 to 24
|
|
132
123
|
* ```
|
|
133
124
|
*/
|
|
125
|
+
declare function age(min: number, max?: number): Date;
|
|
126
|
+
/** An age range no one can be: a negative age, or an oldest below a youngest. */
|
|
127
|
+
export declare class AgeRangeInvalid extends Error {
|
|
128
|
+
readonly min: number;
|
|
129
|
+
readonly max: number;
|
|
130
|
+
constructor(min: number, max: number);
|
|
131
|
+
}
|
|
132
|
+
type Coordinate = {
|
|
133
|
+
lat: number;
|
|
134
|
+
lng: number;
|
|
135
|
+
};
|
|
136
|
+
export declare function coordinateInRadius(center: Coordinate, radius: number): Coordinate;
|
|
137
|
+
declare function coordinateOutsideRadius(center: Coordinate, minRadiusMeters: number, maxRadiusMeters: number): Coordinate;
|
|
134
138
|
export declare const faker: Readonly<import("@faker-js/faker").Faker & {
|
|
139
|
+
internet: import("@faker-js/faker").InternetModule;
|
|
140
|
+
seed: (seed?: number) => number;
|
|
135
141
|
timestamps: typeof timestamps;
|
|
136
142
|
identifier: typeof identifier;
|
|
137
143
|
sequence: typeof sequence;
|
|
138
144
|
resetSequence: typeof resetSequence;
|
|
139
145
|
resetAllSequences: typeof resetAllSequences;
|
|
140
146
|
price: typeof price;
|
|
147
|
+
age: typeof age;
|
|
141
148
|
coordinates: {
|
|
142
149
|
within: typeof coordinateInRadius;
|
|
143
150
|
outside: typeof coordinateOutsideRadius;
|
package/dist/faker.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"faker.d.mts","names":[],"sources":["../src/faker.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;wBAkFgB,cAAc;;;;;;;;;;;;;;;wBA2Bd,WAAW;;;;;;;;;;;;;;;;;;;;;wBAmCX,SAAS;;;;;;;;;;;;;;;;;;;wBA2BT,cAAc,eAAkB;;;;;;;;;;;;;;;;;;wBA0BhC;;;;;;;;;;;;;iBAgBP;
|
|
1
|
+
{"version":3,"file":"faker.d.mts","names":[],"sources":["../src/faker.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;wBAkFgB,cAAc;;;;;;;;;;;;;;;wBA2Bd,WAAW;;;;;;;;;;;;;;;;;;;;;wBAmCX,SAAS;;;;;;;;;;;;;;;;;;;wBA2BT,cAAc,eAAkB;;;;;;;;;;;;;;;;;;wBA0BhC;;;;;;;;;;;;;iBAgBP;;;;;;;;;;;;;;;;;iBAoBA,IAAI,aAAa,eAAoB;;qBAOjC,wBAAwB;WAE1B;WACA;EAFV,YACU,aACA;;KAWN;EACJ;EACA;;wBAGe,mBACf,QAAQ,YACR,iBACE;iBA8BM,wBACR,QAAQ,YACR,yBACA,0BACE;qBA8EU,OAAK,mCAAA;;EAKF,OAAA;;;;;;;;;IASb,eAAM;IACN,gBAAO;;;;;;;YASE;;EAEX,WAAW;;EAEX,WAAW;;;;;;YAOA,sBAAsB"}
|