aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShinigami <[email protected]>2024-02-27 17:10:27 +0100
committerGitHub <[email protected]>2024-02-27 17:10:27 +0100
commit682a4276f13d7b8f48e1bd8aafcf011c7bd10390 (patch)
tree28af1816a09b165449adbc9eca9a415ff156077c
parentc1caa900ceb12737a3aa45b7e4dd75797a11a889 (diff)
downloadfaker-682a4276f13d7b8f48e1bd8aafcf011c7bd10390.tar.xz
faker-682a4276f13d7b8f48e1bd8aafcf011c7bd10390.zip
refactor(datatype)!: remove v8 deprecated datatype methods (#2694)
-rw-r--r--docs/guide/upgrading_v9/2694.md15
-rw-r--r--src/faker.ts5
-rw-r--r--src/modules/datatype/index.ts456
-rw-r--r--test/modules/__snapshots__/datatype.spec.ts.snap351
-rw-r--r--test/modules/__snapshots__/helpers.spec.ts.snap82
-rw-r--r--test/modules/datatype.spec.ts506
-rw-r--r--test/modules/helpers.spec.ts6
7 files changed, 61 insertions, 1360 deletions
diff --git a/docs/guide/upgrading_v9/2694.md b/docs/guide/upgrading_v9/2694.md
new file mode 100644
index 00000000..65d2b4f2
--- /dev/null
+++ b/docs/guide/upgrading_v9/2694.md
@@ -0,0 +1,15 @@
+### Remove deprecated datatype methods
+
+Removed deprecated datatype methods
+
+| old | replacement |
+| --------------------------------------- | ------------------------------------------------------------ |
+| `faker.datatype.number()` | `faker.number.int()` or `faker.number.float()` |
+| `faker.datatype.float()` | `faker.number.float()` |
+| `faker.datatype.datetime({ min, max })` | `faker.date.between({ from, to })` or `faker.date.anytime()` |
+| `faker.datatype.string()` | `faker.string.sample()` |
+| `faker.datatype.uuid()` | `faker.string.uuid()` |
+| `faker.datatype.hexadecimal()` | `faker.string.hexadecimal()` or `faker.number.hex()` |
+| `faker.datatype.json()` | your own function to generate complex objects |
+| `faker.datatype.array()` | your own function to build complex arrays |
+| `faker.datatype.bigInt()` | `faker.number.bigInt()` |
diff --git a/src/faker.ts b/src/faker.ts
index c78fe1de..c4f65187 100644
--- a/src/faker.ts
+++ b/src/faker.ts
@@ -155,11 +155,6 @@ export class Faker extends SimpleFaker {
* @default generateMersenne32Randomizer()
*/
randomizer?: Randomizer;
- });
-
- constructor(options: {
- locale: LocaleDefinition | LocaleDefinition[];
- randomizer?: Randomizer;
}) {
super({ randomizer: options.randomizer });
diff --git a/src/modules/datatype/index.ts b/src/modules/datatype/index.ts
index daec9640..6bcc4f83 100644
--- a/src/modules/datatype/index.ts
+++ b/src/modules/datatype/index.ts
@@ -1,286 +1,14 @@
-import { deprecated } from '../../internal/deprecated';
import { SimpleModuleBase } from '../../internal/module-base';
/**
- * Module to generate various primitive values and data types.
+ * Module to generate boolean values.
*
* ### Overview
*
- * Most of the methods in this module are deprecated and have been moved to other modules like [`faker.number`](https://fakerjs.dev/api/number.html) and [`faker.string`](https://fakerjs.dev/api/string.html), see individual entries for replacements.
- *
* For a simple random true or false value, use [`boolean()`](https://fakerjs.dev/api/datatype.html#boolean).
*/
export class DatatypeModule extends SimpleModuleBase {
/**
- * Returns a single random number between zero and the given max value or the given range with the specified precision.
- * The bounds are inclusive.
- *
- * @param options Maximum value or options object. Defaults to `99999`.
- * @param options.min Lower bound for generated number. Defaults to `0`.
- * @param options.max Upper bound for generated number. Defaults to `min + 99999`.
- * @param options.precision Precision of the generated number. Defaults to `1`.
- *
- * @throws When `min` is greater than `max`.
- * @throws When `precision` is negative.
- *
- * @see faker.number.int(): For generating a random integer.
- * @see faker.number.float(): For generating a random floating-point number.
- *
- * @example
- * faker.datatype.number() // 55422
- * faker.datatype.number(100) // 52
- * faker.datatype.number({ min: 1000000 }) // 1031433
- * faker.datatype.number({ max: 100 }) // 42
- * faker.datatype.number({ precision: 0.01 }) // 64246.18
- * faker.datatype.number({ min: 10, max: 100, precision: 0.01 }) // 36.94
- *
- * @since 5.5.0
- *
- * @deprecated Use `faker.number.int()` or `faker.number.float()` instead.
- */
- number(
- options:
- | number
- | {
- /**
- * Lower bound for generated number.
- *
- * @default 0
- */
- min?: number;
- /**
- * Upper bound for generated number.
- *
- * @default min + 99999
- */
- max?: number;
- /**
- * Precision of the generated number.
- *
- * @default 1
- */
- precision?: number;
- } = 99999
- ): number {
- deprecated({
- deprecated: 'faker.datatype.number()',
- proposed: 'faker.number.int()',
- since: '8.0',
- until: '9.0',
- });
-
- if (typeof options === 'number') {
- options = { max: options };
- }
-
- const { min = 0, max = min + 99999, precision = 1 } = options;
-
- return this.faker.number.float({ min, max, multipleOf: precision });
- }
-
- /**
- * Returns a single random floating-point number for the given precision or range and precision.
- *
- * @param options Precision or options object.
- * @param options.min Lower bound for generated number. Defaults to `0`.
- * @param options.max Upper bound for generated number. Defaults to `min + 99999`.
- * @param options.precision Precision of the generated number. Defaults to `0.01`.
- *
- * @throws When `min` is greater than `max`.
- * @throws When `precision` is negative.
- *
- * @see faker.number.float(): For the replacement method.
- *
- * @example
- * faker.datatype.float() // 51696.36
- * faker.datatype.float(0.1) // 52023.2
- * faker.datatype.float({ min: 1000000 }) // 212859.76
- * faker.datatype.float({ max: 100 }) // 28.11
- * faker.datatype.float({ precision: 0.1 }) // 84055.3
- * faker.datatype.float({ min: 10, max: 100, precision: 0.001 }) // 57.315
- *
- * @since 5.5.0
- *
- * @deprecated Use `faker.number.float()` instead.
- */
- float(
- options:
- | number
- | {
- /**
- * Lower bound for generated number.
- *
- * @default 0
- */
- min?: number;
- /**
- * Upper bound for generated number.
- *
- * @default min + 99999
- */
- max?: number;
- /**
- * Precision of the generated number.
- *
- * @default 0.01
- */
- precision?: number;
- } = {}
- ): number {
- deprecated({
- deprecated: 'faker.datatype.float()',
- proposed: 'faker.number.float()',
- since: '8.0',
- until: '9.0',
- });
-
- if (typeof options === 'number') {
- options = {
- precision: options,
- };
- }
-
- const { min = 0, max = min + 99999, precision = 0.01 } = options;
-
- return this.faker.number.float({ min, max, multipleOf: precision });
- }
-
- /**
- * Returns a Date object using a random number of milliseconds since
- * the [Unix Epoch](https://en.wikipedia.org/wiki/Unix_time) (1 January 1970 UTC).
- *
- * @param options Max number of milliseconds since unix epoch or options object.
- * @param options.min Lower bound for milliseconds since base date.
- * When not provided or smaller than `-8640000000000000`, `1990-01-01` is considered
- * as minimum generated date. Defaults to `631152000000`.
- * @param options.max Upper bound for milliseconds since base date.
- * When not provided or larger than `8640000000000000`, `2100-01-01` is considered
- * as maximum generated date. Defaults to `4102444800000`.
- *
- * @see faker.date.anytime(): For generating a random date in either the past or future.
- * @see faker.date.between(): For generating a random date in between two dates.
- *
- * @example
- * faker.datatype.datetime() // '2089-04-17T18:03:24.956Z'
- * faker.datatype.datetime(1893456000000) // '2022-03-28T07:00:56.876Z'
- * faker.datatype.datetime({ min: 1577836800000, max: 1893456000000 }) // '2021-09-12T07:13:00.255Z'
- *
- * @since 5.5.0
- *
- * @deprecated Use `faker.date.between({ from: min, to: max })` or `faker.date.anytime()` instead.
- */
- datetime(
- options:
- | number
- | {
- /**
- * Lower bound for milliseconds since base date.
- *
- * When not provided or smaller than `-8640000000000000`, `1990-01-01` is considered as minimum generated date.
- *
- * @default 631152000000
- */
- min?: number;
- /**
- * Upper bound for milliseconds since base date.
- *
- * When not provided or larger than `8640000000000000`, `2100-01-01` is considered as maximum generated date.
- *
- * @default 4102444800000
- */
- max?: number;
- } = {}
- ): Date {
- deprecated({
- deprecated: 'faker.datatype.datetime({ min, max })',
- proposed: 'faker.date.between({ from, to }) or faker.date.anytime()',
- since: '8.0',
- until: '9.0',
- });
-
- const minMax = 8640000000000000;
-
- let min = typeof options === 'number' ? undefined : options.min;
- let max = typeof options === 'number' ? options : options.max;
-
- if (min == null || min < minMax * -1) {
- min = Date.UTC(1990, 0);
- }
-
- if (max == null || max > minMax) {
- max = Date.UTC(2100, 0);
- }
-
- return this.faker.date.between({ from: min, to: max });
- }
-
- /**
- * Returns a string containing UTF-16 chars between 33 and 125 (`!` to `}`).
- *
- * @param options Length of the generated string or an options object.
- * @param options.length Length of the generated string. Max length is `2^20`. Defaults to `10`.
- *
- * @see faker.string.sample(): For the replacement method.
- *
- * @example
- * faker.datatype.string() // 'Zo!.:*e>wR'
- * faker.datatype.string(5) // '6Bye8'
- * faker.datatype.string({ length: 7 }) // 'dzOT00e'
- *
- * @since 5.5.0
- *
- * @deprecated Use `faker.string.sample()` instead.
- */
- string(
- options:
- | number
- | {
- /**
- * Length of the generated string. Max length is `2^20`.
- *
- * @default 10
- */
- length?: number;
- } = {}
- ): string {
- deprecated({
- deprecated: 'faker.datatype.string()',
- proposed: 'faker.string.sample()',
- since: '8.0',
- until: '9.0',
- });
- if (typeof options === 'number') {
- options = { length: options };
- }
-
- const { length = 10 } = options;
-
- return this.faker.string.sample(length);
- }
-
- /**
- * Returns a UUID v4 ([Universally Unique Identifier](https://en.wikipedia.org/wiki/Universally_unique_identifier)).
- *
- * @see faker.string.uuid(): For the replacement method.
- *
- * @example
- * faker.datatype.uuid() // '4136cd0b-d90b-4af7-b485-5d1ded8db252'
- *
- * @since 5.5.0
- *
- * @deprecated Use `faker.string.uuid()` instead.
- */
- uuid(): string {
- deprecated({
- deprecated: 'faker.datatype.uuid()',
- proposed: 'faker.string.uuid()',
- since: '8.0',
- until: '9.0',
- });
- return this.faker.string.uuid();
- }
-
- /**
* Returns the boolean value true or false.
*
* **Note:**
@@ -329,186 +57,4 @@ export class DatatypeModule extends SimpleModuleBase {
return this.faker.number.float() < probability;
}
-
- /**
- * Returns a [hexadecimal](https://en.wikipedia.org/wiki/Hexadecimal) number.
- *
- * @param options The optional options object.
- * @param options.length Length of the generated number. Defaults to `1`.
- * @param options.prefix Prefix for the generated number. Defaults to `'0x'`.
- * @param options.case Case of the generated number. Defaults to `'mixed'`.
- *
- * @see faker.string.hexadecimal(): For generating a random hexadecimal string.
- * @see faker.number.hex(): For generating a random hexadecimal number.
- *
- * @example
- * faker.datatype.hexadecimal() // '0xB'
- * faker.datatype.hexadecimal({ length: 10 }) // '0xaE13d044cB'
- * faker.datatype.hexadecimal({ prefix: '0x' }) // '0xE'
- * faker.datatype.hexadecimal({ case: 'lower' }) // '0xf'
- * faker.datatype.hexadecimal({ length: 10, prefix: '#' }) // '#f12a974eB1'
- * faker.datatype.hexadecimal({ length: 10, case: 'upper' }) // '0xE3F38014FB'
- * faker.datatype.hexadecimal({ prefix: '', case: 'lower' }) // 'd'
- * faker.datatype.hexadecimal({ length: 10, prefix: '0x', case: 'mixed' }) // '0xAdE330a4D1'
- *
- * @since 6.1.2
- *
- * @deprecated Use `faker.string.hexadecimal()` or `faker.number.hex()` instead.
- */
- hexadecimal(
- options: {
- /**
- * Length of the generated number.
- *
- * @default 1
- */
- length?: number;
- /**
- * Prefix for the generated number.
- *
- * @default '0x'
- */
- prefix?: string;
- /**
- * Case of the generated number.
- *
- * @default 'mixed'
- */
- case?: 'lower' | 'upper' | 'mixed';
- } = {}
- ): string {
- deprecated({
- deprecated: 'faker.datatype.hexadecimal()',
- proposed: 'faker.string.hexadecimal() or faker.number.hex()',
- since: '8.0',
- until: '9.0',
- });
- return this.faker.string.hexadecimal({ ...options, casing: options.case });
- }
-
- /**
- * Returns a string representing JSON object with 7 pre-defined properties.
- *
- * @example
- * faker.datatype.json() // `{"foo":"mxz.v8ISij","bar":29154,"bike":8658,"a":"GxTlw$nuC:","b":40693,"name":"%'<FTou{7X","prop":"X(bd4iT>77"}`
- *
- * @since 5.5.0
- *
- * @deprecated Build your own function to generate complex objects.
- */
- json(): string {
- deprecated({
- deprecated: 'faker.datatype.json()',
- proposed: 'your own function to generate complex objects',
- since: '8.0',
- until: '9.0',
- });
-
- const properties = ['foo', 'bar', 'bike', 'a', 'b', 'name', 'prop'];
- const returnObject: Record<string, string | number> = {};
-
- for (const prop of properties) {
- returnObject[prop] = this.boolean()
- ? this.faker.string.sample()
- : this.faker.number.int();
- }
-
- return JSON.stringify(returnObject);
- }
-
- /**
- * Returns an array with random strings and numbers.
- *
- * @param length Size of the returned array. Defaults to `10`.
- * @param length.min The minimum size of the array.
- * @param length.max The maximum size of the array.
- *
- * @example
- * faker.datatype.array() // [ 94099, 85352, 'Hz%T.C\\l;8', '|#gmtw3otS', '2>:rJ|3$&d', 56864, 'Ss2-p0RXSI', 51084, 2039, 'mNEU[.r0Vf' ]
- * faker.datatype.array(3) // [ 61845, 'SK7H$W3:d*', 'm[%7N8*GVK' ]
- * faker.datatype.array({ min: 3, max: 5 }) // [ 99403, 76924, 42281, "Q'|$&y\\G/9" ]
- *
- * @since 5.5.0
- *
- * @deprecated Use your own function to build complex arrays.
- */
- array(
- length:
- | number
- | {
- /**
- * The minimum size of the array.
- */
- min: number;
- /**
- * The maximum size of the array.
- */
- max: number;
- } = 10
- ): Array<string | number> {
- deprecated({
- deprecated: 'faker.datatype.array()',
- proposed: 'your own function to build complex arrays',
- since: '8.0',
- until: '9.0',
- });
-
- return this.faker.helpers.multiple(
- () =>
- this.boolean() ? this.faker.string.sample() : this.faker.number.int(),
- { count: length }
- );
- }
-
- /**
- * Returns a [BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#bigint_type) number.
- *
- * @param options Maximum value or options object.
- * @param options.min Lower bound for generated bigint. Defaults to `0n`.
- * @param options.max Upper bound for generated bigint. Defaults to `min + 999999999999999n`.
- *
- * @throws When options define `max < min`.
- *
- * @see faker.number.bigInt(): For the replacement method.
- *
- * @example
- * faker.datatype.bigInt() // 55422n
- * faker.datatype.bigInt(100n) // 52n
- * faker.datatype.bigInt({ min: 1000000n }) // 431433n
- * faker.datatype.bigInt({ max: 100n }) // 42n
- * faker.datatype.bigInt({ min: 10n, max: 100n }) // 36n
- *
- * @since 6.0.0
- *
- * @deprecated Use `faker.number.bigInt()` instead.
- */
- bigInt(
- options?:
- | bigint
- | boolean
- | number
- | string
- | {
- /**
- * Lower bound for generated bigint.
- *
- * @default 0n
- */
- min?: bigint | boolean | number | string;
- /**
- * Upper bound for generated bigint.
- *
- * @default min + 999999999999999n
- */
- max?: bigint | boolean | number | string;
- }
- ): bigint {
- deprecated({
- deprecated: 'faker.datatype.bigInt()',
- proposed: 'faker.number.bigInt()',
- since: '8.0',
- until: '9.0',
- });
- return this.faker.number.bigInt(options);
- }
}
diff --git a/test/modules/__snapshots__/datatype.spec.ts.snap b/test/modules/__snapshots__/datatype.spec.ts.snap
index ba5c2ad1..9d6707ba 100644
--- a/test/modules/__snapshots__/datatype.spec.ts.snap
+++ b/test/modules/__snapshots__/datatype.spec.ts.snap
@@ -1,42 +1,5 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
-exports[`datatype > 42 > array > noArgs 1`] = `
-[
- "ky2eiXX/J/",
- 6503186795855872,
- 8453731307749376,
- "!1}2Z=YQ!I",
- "<QYF-%<{C6",
- ")jZ3DP|XL%",
- 5472277529362432,
- 1535944771502080,
- "'"yxzUlD="",
-]
-`;
-
-exports[`datatype > 42 > array > with length 1`] = `
-[
- "ky2eiXX/J/",
- 6503186795855872,
- 8453731307749376,
-]
-`;
-
-exports[`datatype > 42 > array > with length range 1`] = `
-[
- 8563273238577152,
- "eiXX/J/*&K",
- 3005779955154944,
- 1286829863075840,
-]
-`;
-
-exports[`datatype > 42 > bigInt > noArgs 1`] = `379177551410048n`;
-
-exports[`datatype > 42 > bigInt > with value 1`] = `37n`;
-
exports[`datatype > 42 > boolean > noArgs 1`] = `true`;
exports[`datatype > 42 > boolean > noArgs 2`] = `false`;
@@ -51,124 +14,6 @@ exports[`datatype > 42 > boolean > with probability 1`] = `true`;
exports[`datatype > 42 > boolean > with probability option 1`] = `false`;
-exports[`datatype > 42 > datetime > noArgs 1`] = `2031-03-14T21:33:22.114Z`;
-
-exports[`datatype > 42 > datetime > with given number 1`] = `1994-03-20T17:23:00.629Z`;
-
-exports[`datatype > 42 > datetime > with max 1`] = `1994-07-11T09:43:47.230Z`;
-
-exports[`datatype > 42 > datetime > with min 1`] = `1801-04-11T15:13:06.330Z`;
-
-exports[`datatype > 42 > datetime > with min and max 1`] = `1689-09-09T08:39:09.444Z`;
-
-exports[`datatype > 42 > float > noArgs 1`] = `37453.64`;
-
-exports[`datatype > 42 > float > repeated 1`] = `37453.64`;
-
-exports[`datatype > 42 > float > repeated 2`] = `79653.5`;
-
-exports[`datatype > 42 > float > repeated 3`] = `95070.48`;
-
-exports[`datatype > 42 > float > repeated 4`] = `18343.29`;
-
-exports[`datatype > 42 > float > repeated 5`] = `73198.66`;
-
-exports[`datatype > 42 > float > repeated 6`] = `77968.32`;
-
-exports[`datatype > 42 > float > with max 1`] = `25.84`;
-
-exports[`datatype > 42 > float > with min 1`] = `37411.64`;
-
-exports[`datatype > 42 > float > with min and max 1`] = `-0.43`;
-
-exports[`datatype > 42 > float > with min, max and precision 1`] = `-0.4261`;
-
-exports[`datatype > 42 > hexadecimal > noArgs 1`] = `"0x8"`;
-
-exports[`datatype > 42 > hexadecimal > with casing 1`] = `"0x8"`;
-
-exports[`datatype > 42 > hexadecimal > with length 1`] = `"0x8BE4ABdd39321aD7d3fe01FfCE404F4d6db0906bd8"`;
-
-exports[`datatype > 42 > hexadecimal > with length, prefix, and casing 1`] = `"0x8be4abdd39321ad7d3fe"`;
-
-exports[`datatype > 42 > hexadecimal > with prefix 1`] = `"0x8"`;
-
-exports[`datatype > 42 > json 1`] = `"{"foo":"ky2eiXX/J/","bar":"&[email protected]]\\"&","bike":6503186795855872,"a":8453731307749376,"b":"!1}2Z=YQ!I","name":"<QYF-%<{C6","prop":")jZ3DP|XL%"}"`;
-
-exports[`datatype > 42 > number > noArgs 1`] = `37454`;
-
-exports[`datatype > 42 > number > repeated 1`] = `2`;
-
-exports[`datatype > 42 > number > repeated 2`] = `5`;
-
-exports[`datatype > 42 > number > repeated 3`] = `6`;
-
-exports[`datatype > 42 > number > repeated 4`] = `1`;
-
-exports[`datatype > 42 > number > repeated 5`] = `5`;
-
-exports[`datatype > 42 > number > with max 1`] = `26`;
-
-exports[`datatype > 42 > number > with min 1`] = `37412`;
-
-exports[`datatype > 42 > number > with min and max 1`] = `-1`;
-
-exports[`datatype > 42 > number > with min, max and precision 1`] = `-0.43`;
-
-exports[`datatype > 42 > string > noArgs 1`] = `"Cky2eiXX/J"`;
-
-exports[`datatype > 42 > string > with length option 1`] = `"Cky2eiXX/J/*&[email protected]]"&"`;
-
-exports[`datatype > 42 > string > with number 1`] = `"Cky2eiXX/J/*&[email protected]]"&{dnx4!1}2Z=YQ!I#<QYF"`;
-
-exports[`datatype > 42 > uuid 1`] = `"5cf2bc99-2721-407d-8592-ba00fbdf302f"`;
-
-exports[`datatype > 42 > uuid 2`] = `"94980604-8962-404f-a537-1c9368f970d9"`;
-
-exports[`datatype > 42 > uuid 3`] = `"2710fff9-c640-413a-937a-197d02e642ac"`;
-
-exports[`datatype > 42 > uuid 4`] = `"6838920f-dc7f-46ee-a9be-519380f5d6b4"`;
-
-exports[`datatype > 42 > uuid 5`] = `"d95f4984-24c2-410f-86c6-3400d3bbbcc9"`;
-
-exports[`datatype > 1211 > array > noArgs 1`] = `
-[
- 4134441414819840,
- 7010029022478336,
- "-}$_/\`4hHA",
- "afl"h^]dnw",
- "<q|p|5KWu3",
- "CZ|Jh!E=x"",
- 3794869965291520,
- 1431627091673088,
- "V<1bEQuA|p",
- "DW9F=V1(U7",
-]
-`;
-
-exports[`datatype > 1211 > array > with length 1`] = `
-[
- 4134441414819840,
- 7010029022478336,
- "-}$_/\`4hHA",
- "afl"h^]dnw",
-]
-`;
-
-exports[`datatype > 1211 > array > with length range 1`] = `
-[
- "ti5-}$_/\`4",
- 3789861976801280,
- "0afl"h^]dn",
- 3918465303838720,
- "q|p|5KWu3/",
-]
-`;
-
-exports[`datatype > 1211 > bigInt > noArgs 1`] = `948721906162743n`;
-
-exports[`datatype > 1211 > bigInt > with value 1`] = `8n`;
-
exports[`datatype > 1211 > boolean > noArgs 1`] = `false`;
exports[`datatype > 1211 > boolean > noArgs 2`] = `true`;
@@ -183,122 +28,6 @@ exports[`datatype > 1211 > boolean > with probability 1`] = `false`;
exports[`datatype > 1211 > boolean > with probability option 1`] = `false`;
-exports[`datatype > 1211 > datetime > noArgs 1`] = `2092-02-20T03:42:04.341Z`;
-
-exports[`datatype > 1211 > datetime > with given number 1`] = `2000-06-14T02:54:42.082Z`;
-
-exports[`datatype > 1211 > datetime > with max 1`] = `2001-03-20T11:14:25.251Z`;
-
-exports[`datatype > 1211 > datetime > with min 1`] = `2065-11-10T19:27:20.915Z`;
-
-exports[`datatype > 1211 > datetime > with min and max 1`] = `1789-03-26T15:44:45.219Z`;
-
-exports[`datatype > 1211 > float > noArgs 1`] = `92851.09`;
-
-exports[`datatype > 1211 > float > repeated 1`] = `92851.09`;
-
-exports[`datatype > 1211 > float > repeated 2`] = `45901.06`;
-
-exports[`datatype > 1211 > float > repeated 3`] = `89346.28`;
-
-exports[`datatype > 1211 > float > repeated 4`] = `77826.18`;
-
-exports[`datatype > 1211 > float > repeated 5`] = `22556.85`;
-
-exports[`datatype > 1211 > float > repeated 6`] = `12988.8`;
-
-exports[`datatype > 1211 > float > with max 1`] = `64.07`;
-
-exports[`datatype > 1211 > float > with min 1`] = `92809.09`;
-
-exports[`datatype > 1211 > float > with min and max 1`] = `61.07`;
-
-exports[`datatype > 1211 > float > with min, max and precision 1`] = `61.0658`;
-
-exports[`datatype > 1211 > hexadecimal > noArgs 1`] = `"0xE"`;
-
-exports[`datatype > 1211 > hexadecimal > with casing 1`] = `"0xe"`;
-
-exports[`datatype > 1211 > hexadecimal > with length 1`] = `"0xEaDB42F0e3f4A973fAB0AeefCE96DFCF49cD438dF9"`;
-
-exports[`datatype > 1211 > hexadecimal > with length, prefix, and casing 1`] = `"0xeadb42f0e3f4a973fab0"`;
-
-exports[`datatype > 1211 > hexadecimal > with prefix 1`] = `"0xE"`;
-
-exports[`datatype > 1211 > json 1`] = `"{"foo":4134441414819840,"bar":7010029022478336,"bike":"-}$_/\`4hHA","a":"afl\\"h^]dnw","b":"<q|p|5KWu3","name":"CZ|Jh!E=x\\"","prop":3794869965291520}"`;
-
-exports[`datatype > 1211 > number > noArgs 1`] = `92852`;
-
-exports[`datatype > 1211 > number > repeated 1`] = `6`;
-
-exports[`datatype > 1211 > number > repeated 2`] = `3`;
-
-exports[`datatype > 1211 > number > repeated 3`] = `6`;
-
-exports[`datatype > 1211 > number > repeated 4`] = `5`;
-
-exports[`datatype > 1211 > number > repeated 5`] = `1`;
-
-exports[`datatype > 1211 > number > with max 1`] = `64`;
-
-exports[`datatype > 1211 > number > with min 1`] = `92810`;
-
-exports[`datatype > 1211 > number > with min and max 1`] = `61`;
-
-exports[`datatype > 1211 > number > with min, max and precision 1`] = `61.07`;
-
-exports[`datatype > 1211 > string > noArgs 1`] = `"wKti5-}$_/"`;
-
-exports[`datatype > 1211 > string > with length option 1`] = `"wKti5-}$_/\`4hHA0afl"h^"`;
-
-exports[`datatype > 1211 > string > with number 1`] = `"wKti5-}$_/\`4hHA0afl"h^]dnwI<q|p|5KWu3/CZ|J"`;
-
-exports[`datatype > 1211 > uuid 1`] = `"e7ec32f0-a2a3-4c65-b2bb-d0caabde64df"`;
-
-exports[`datatype > 1211 > uuid 2`] = `"f379e325-9f7c-4064-be08-6f23942b68e5"`;
-
-exports[`datatype > 1211 > uuid 3`] = `"d4694649-2183-4b32-90bd-7a336639d699"`;
-
-exports[`datatype > 1211 > uuid 4`] = `"10ab829b-742c-4a8b-a773-298d718d7706"`;
-
-exports[`datatype > 1211 > uuid 5`] = `"7b91ce88-effb-4d1d-b13b-bad759e00b86"`;
-
-exports[`datatype > 1337 > array > noArgs 1`] = `
-[
- "U/4:SK$>6Q",
- 2359372120326144,
- "{:e=+kD)[B",
- "e|/Jqjjj!B",
- "GDWQgC2M;q",
- 3648103756333056,
- "I1.Gm3tRwn",
- 1668592164667392,
- 8623125245722624,
- 3831794621218816,
-]
-`;
-
-exports[`datatype > 1337 > array > with length 1`] = `
-[
- "U/4:SK$>6Q",
- 2359372120326144,
- "{:e=+kD)[B",
- "e|/Jqjjj!B",
-]
-`;
-
-exports[`datatype > 1337 > array > with length range 1`] = `
-[
- 1429298200182784,
- ":SK$>6QX9@",
- 2436284417048576,
-]
-`;
-
-exports[`datatype > 1337 > bigInt > noArgs 1`] = `251225403255239n`;
-
-exports[`datatype > 1337 > bigInt > with value 1`] = `25n`;
-
exports[`datatype > 1337 > boolean > noArgs 1`] = `true`;
exports[`datatype > 1337 > boolean > noArgs 2`] = `false`;
@@ -312,83 +41,3 @@ exports[`datatype > 1337 > boolean > noArgs 5`] = `true`;
exports[`datatype > 1337 > boolean > with probability 1`] = `true`;
exports[`datatype > 1337 > boolean > with probability option 1`] = `false`;
-
-exports[`datatype > 1337 > datetime > noArgs 1`] = `2018-10-28T08:46:11.896Z`;
-
-exports[`datatype > 1337 > datetime > with given number 1`] = `1992-12-13T04:13:59.232Z`;
-
-exports[`datatype > 1337 > datetime > with max 1`] = `1993-03-02T00:10:04.335Z`;
-
-exports[`datatype > 1337 > datetime > with min 1`] = `1747-07-16T01:19:54.159Z`;
-
-exports[`datatype > 1337 > datetime > with min and max 1`] = `1669-06-22T01:21:21.236Z`;
-
-exports[`datatype > 1337 > float > noArgs 1`] = `26202.2`;
-
-exports[`datatype > 1337 > float > repeated 1`] = `26202.2`;
-
-exports[`datatype > 1337 > float > repeated 2`] = `56052.42`;
-
-exports[`datatype > 1337 > float > repeated 3`] = `15868.24`;
-
-exports[`datatype > 1337 > float > repeated 4`] = `21258.55`;
-
-exports[`datatype > 1337 > float > repeated 5`] = `27812.37`;
-
-exports[`datatype > 1337 > float > repeated 6`] = `54307.53`;
-
-exports[`datatype > 1337 > float > with max 1`] = `18.08`;
-
-exports[`datatype > 1337 > float > with min 1`] = `26160.2`;
-
-exports[`datatype > 1337 > float > with min and max 1`] = `-12.92`;
-
-exports[`datatype > 1337 > float > with min, max and precision 1`] = `-12.9153`;
-
-exports[`datatype > 1337 > hexadecimal > noArgs 1`] = `"0x5"`;
-
-exports[`datatype > 1337 > hexadecimal > with casing 1`] = `"0x5"`;
-
-exports[`datatype > 1337 > hexadecimal > with length 1`] = `"0x5c346ba075bd57F5A62B82d72AF39CBBB07a98cbA8"`;
-
-exports[`datatype > 1337 > hexadecimal > with length, prefix, and casing 1`] = `"0x5c346ba075bd57f5a62b"`;
-
-exports[`datatype > 1337 > hexadecimal > with prefix 1`] = `"0x5"`;
-
-exports[`datatype > 1337 > json 1`] = `"{"foo":"U/4:SK$>6Q","bar":2359372120326144,"bike":"{:e=+kD)[B","a":"e|/Jqjjj!B","b":"GDWQgC2M;q","name":3648103756333056,"prop":"I1.Gm3tRwn"}"`;
-
-exports[`datatype > 1337 > number > noArgs 1`] = `26202`;
-
-exports[`datatype > 1337 > number > repeated 1`] = `1`;
-
-exports[`datatype > 1337 > number > repeated 2`] = `3`;
-
-exports[`datatype > 1337 > number > repeated 3`] = `1`;
-
-exports[`datatype > 1337 > number > repeated 4`] = `1`;
-
-exports[`datatype > 1337 > number > repeated 5`] = `1`;
-
-exports[`datatype > 1337 > number > with max 1`] = `18`;
-
-exports[`datatype > 1337 > number > with min 1`] = `26160`;
-
-exports[`datatype > 1337 > number > with min and max 1`] = `-13`;
-
-exports[`datatype > 1337 > number > with min, max and precision 1`] = `-12.92`;
-
-exports[`datatype > 1337 > string > noArgs 1`] = `"9U/4:SK$>6"`;
-
-exports[`datatype > 1337 > string > with length option 1`] = `"9U/4:SK$>6QX9@{:e=+kD)"`;
-
-exports[`datatype > 1337 > string > with number 1`] = `"9U/4:SK$>6QX9@{:e=+kD)[B,e|/Jqjjj!BLGDWQgC"`;
-
-exports[`datatype > 1337 > uuid 1`] = `"48234870-5389-445f-b4b4-1c61a52bf27d"`;
-
-exports[`datatype > 1337 > uuid 2`] = `"cc057669-8c53-474d-ba67-7226d3e8ed92"`;
-
-exports[`datatype > 1337 > uuid 3`] = `"fe6d8b8b-0db9-4fa2-9726-5abc0a5d0ccf"`;
-
-exports[`datatype > 1337 > uuid 4`] = `"0b87afbd-8949-4dfb-84d0-419f4fe7458b"`;
-
-exports[`datatype > 1337 > uuid 5`] = `"0bcea83c-a7ea-428e-9c5d-bd448f2b777a"`;
diff --git a/test/modules/__snapshots__/helpers.spec.ts.snap b/test/modules/__snapshots__/helpers.spec.ts.snap
index 04b416a2..63c7513f 100644
--- a/test/modules/__snapshots__/helpers.spec.ts.snap
+++ b/test/modules/__snapshots__/helpers.spec.ts.snap
@@ -81,28 +81,28 @@ exports[`helpers > 42 > maybe > with value and probability 1`] = `undefined`;
exports[`helpers > 42 > multiple > with method and count 1`] = `
[
- 37454,
- 79654,
- 95071,
- 18343,
- 73199,
+ 3373557438480384,
+ 7174621373661184,
+ 8563273238577152,
+ 1652233682812928,
+ 6593215255805952,
]
`;
exports[`helpers > 42 > multiple > with method and count range 1`] = `
[
- 79654,
- 95071,
- 18343,
- 73199,
+ 7174621373661184,
+ 8563273238577152,
+ 1652233682812928,
+ 6593215255805952,
]
`;
exports[`helpers > 42 > multiple > with only method 1`] = `
[
- 37454,
- 79654,
- 95071,
+ 3373557438480384,
+ 7174621373661184,
+ 8563273238577152,
]
`;
@@ -307,34 +307,34 @@ exports[`helpers > 1211 > maybe > with value and probability 1`] = `undefined`;
exports[`helpers > 1211 > multiple > with method and count 1`] = `
[
- 92852,
- 45901,
- 89347,
- 77826,
- 22557,
+ 8363366036799488,
+ 4134441414819840,
+ 8047677172350976,
+ 7010029022478336,
+ 2031760839802880,
]
`;
exports[`helpers > 1211 > multiple > with method and count range 1`] = `
[
- 45901,
- 89347,
- 77826,
- 22557,
- 12988,
- 99725,
- 3842,
- 67199,
- 15897,
- 68481,
+ 4134441414819840,
+ 8047677172350976,
+ 7010029022478336,
+ 2031760839802880,
+ 1169939440336896,
+ 8982492805595136,
+ 346135076012032,
+ 6052754548064256,
+ 1431911878623232,
+ 6168278875504640,
]
`;
exports[`helpers > 1211 > multiple > with only method 1`] = `
[
- 92852,
- 45901,
- 89347,
+ 8363366036799488,
+ 4134441414819840,
+ 8047677172350976,
]
`;
@@ -528,27 +528,27 @@ exports[`helpers > 1337 > maybe > with value and probability 1`] = `undefined`;
exports[`helpers > 1337 > multiple > with method and count 1`] = `
[
- 26202,
- 56052,
- 15868,
- 21258,
- 27812,
+ 2360108468142080,
+ 5048803172286464,
+ 1429298200182784,
+ 1914819313664000,
+ 2505140957347840,
]
`;
exports[`helpers > 1337 > multiple > with method and count range 1`] = `
[
- 56052,
- 15868,
- 21258,
+ 5048803172286464,
+ 1429298200182784,
+ 1914819313664000,
]
`;
exports[`helpers > 1337 > multiple > with only method 1`] = `
[
- 26202,
- 56052,
- 15868,
+ 2360108468142080,
+ 5048803172286464,
+ 1429298200182784,
]
`;
diff --git a/test/modules/datatype.spec.ts b/test/modules/datatype.spec.ts
index ca6c3328..0dc59fe4 100644
--- a/test/modules/datatype.spec.ts
+++ b/test/modules/datatype.spec.ts
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest';
-import { FakerError, faker } from '../../src';
+import { faker } from '../../src';
import { seededTests } from '../support/seeded-runs';
import { times } from './../support/times';
@@ -7,366 +7,16 @@ const NON_SEEDED_BASED_RUN = 25;
describe('datatype', () => {
seededTests(faker, 'datatype', (t) => {
- t.describe('number', (t) => {
- t.it('noArgs')
- .itRepeated('repeated', 5, 6)
- .it('with min', { min: -42 })
- .it('with max', { max: 69 })
- .it('with min and max', {
- min: -42,
- max: 69,
- })
- .it('with min, max and precision', {
- min: -42,
- max: 69,
- precision: 0.01,
- });
- });
-
- t.describe('float', (t) => {
- t.it('noArgs')
- .itRepeated('repeated', 6)
- .it('with min', { min: -42 })
- .it('with max', { max: 69 })
- .it('with min and max', { min: -42, max: 69 })
- .it('with min, max and precision', {
- min: -42,
- max: 69,
- precision: 0.0001,
- });
- });
-
- t.describe('datetime', (t) => {
- t.it('noArgs')
- .it('with given number', Date.parse('2001-04-03T23:21:10.773Z'))
- .it('with min', {
- min: Date.parse('1622-05-23T13:45:08.843Z'),
- })
- .it('with max', {
- max: Date.parse('2002-01-29T19:47:52.605Z'),
- })
- .it('with min and max', {
- min: Date.parse('1622-05-23T13:45:08.843Z'),
- max: Date.parse('1802-01-29T19:47:52.605Z'),
- });
- });
-
- t.describe('string', (t) => {
- t.it('noArgs')
- .it('with number', 42)
- .it('with length option', { length: 22 });
- });
-
- t.itRepeated('uuid', 5);
-
t.describe('boolean', (t) => {
t.itRepeated('noArgs', 5)
.it('with probability', 0.42)
.it('with probability option', { probability: 0.13 });
});
-
- t.describe('hexadecimal', (t) => {
- t.it('noArgs')
- .it('with length', { length: 42 })
- .it('with prefix', { prefix: '0x' })
- .it('with casing', { case: 'lower' })
- .it('with length, prefix, and casing', {
- length: 20,
- prefix: '0x',
- case: 'lower',
- });
- });
-
- t.it('json');
-
- t.describe('array', (t) => {
- t.it('noArgs')
- .it('with length', 4)
- .it('with length range', { min: 3, max: 5 });
- });
-
- t.describe('bigInt', (t) => {
- t.it('noArgs').it('with value', 42);
- });
});
describe.each(times(NON_SEEDED_BASED_RUN).map(() => faker.seed()))(
'random seeded tests for seed %i',
() => {
- describe('number', () => {
- it('should return a random number given a maximum value as Number', () => {
- const max = 10;
-
- const actual = faker.datatype.number(max);
-
- expect(actual).toBeGreaterThanOrEqual(0);
- expect(actual).toBeLessThanOrEqual(max);
- });
-
- it('should return a random number given a maximum value as Object', () => {
- const options = { max: 10 };
-
- const actual = faker.datatype.number(options);
-
- expect(actual).toBeGreaterThanOrEqual(0);
- expect(actual).toBeLessThanOrEqual(options.max);
- });
-
- it('should return a random number given a maximum value of 0', () => {
- const options = { max: 0 };
-
- const actual = faker.datatype.number(options);
-
- expect(actual).toBe(0);
- });
-
- it('should return a random number given a negative number minimum and maximum value of 0', () => {
- const options = { min: -100, max: 0 };
-
- const actual = faker.datatype.number(options);
-
- expect(actual).toBeGreaterThanOrEqual(options.min);
- expect(actual).toBeLessThanOrEqual(options.max);
- });
-
- it('should return a random number between a range', () => {
- const options = { min: 22, max: 33 };
- for (let i = 0; i < 100; i++) {
- const actual = faker.datatype.number(options);
- expect(actual).toBeGreaterThanOrEqual(options.min);
- expect(actual).toBeLessThanOrEqual(options.max);
- }
- });
-
- it('should return inclusive negative max value', () => {
- let foundNegative4 = false;
- let foundNegative5 = false;
-
- for (let iter = 0; iter < 1000; iter++) {
- const actual = faker.datatype.number({ min: -5, max: -4 });
-
- if (actual === -4) {
- foundNegative4 = true;
- } else if (actual === -5) {
- foundNegative5 = true;
- }
-
- expect(actual).toBeGreaterThanOrEqual(-5);
- expect(actual).toBeLessThanOrEqual(-4);
-
- if (foundNegative4 && foundNegative5) {
- break;
- }
- }
-
- expect(foundNegative4).toBeTruthy();
- expect(foundNegative5).toBeTruthy();
- });
-
- it('provides numbers with a given precision of 0.5 steps', () => {
- const results = [
- ...new Set(
- Array.from({ length: 50 }, () =>
- faker.datatype.float({
- min: 0,
- max: 1.5,
- precision: 0.5,
- })
- )
- ),
- ].sort();
-
- expect(results).toEqual([0, 0.5, 1, 1.5]);
- });
-
- // TODO @Shinigami92 2022-11-24: https://github.com/faker-js/faker/issues/1595
- it.todo('provides numbers with a given precision of 0.4 steps', () => {
- const results = [
- ...new Set(
- Array.from({ length: 50 }, () =>
- faker.datatype.float({
- min: 0,
- max: 1.9,
- precision: 0.4,
- })
- )
- ),
- ].sort();
-
- expect(results).toEqual([0, 0.4, 0.8, 1.2, 1.6]);
- });
-
- it('provides numbers with a with exact precision', () => {
- const options = { min: 0.5, max: 0.99, precision: 0.01 };
- for (let i = 0; i < 100; i++) {
- const actual = faker.datatype.number(options);
- expect(actual).toBe(Number(actual.toFixed(2)));
- }
- });
-
- it('should not mutate the input object', () => {
- const initialMin = 1;
- const initialPrecision = 1;
- const initialOtherProperty = 'hello darkness my old friend';
- const input: {
- min?: number;
- max?: number;
- precision?: number;
- otherProperty: string;
- } = Object.freeze({
- min: initialMin,
- precision: initialPrecision,
- otherProperty: initialOtherProperty,
- });
-
- expect(() => faker.datatype.number(input)).not.toThrow();
- });
-
- it('should throw when min > max', () => {
- const min = 10;
- const max = 9;
-
- expect(() => {
- faker.datatype.number({ min, max });
- }).toThrow(
- new FakerError(`Max ${max} should be greater than min ${min}.`)
- );
- });
-
- it('should throw when precision is negative', () => {
- expect(() => {
- faker.datatype.number({ precision: -0.01 });
- }).toThrow(
- new FakerError('multipleOf/precision should be greater than 0.')
- );
- });
- });
-
- describe('float', () => {
- it('should return a random float with a default precision value (0.01)', () => {
- const number = faker.datatype.float();
- expect(number).toBe(Number(number.toFixed(2)));
- });
-
- it('should return a random float given a precision value', () => {
- const number = faker.datatype.float(0.001);
- expect(number).toBe(Number(number.toFixed(3)));
- });
-
- it('should return a random number given a maximum value as Object', () => {
- const options = { max: 10 };
- expect(faker.datatype.float(options)).toBeGreaterThanOrEqual(0);
- expect(faker.datatype.float(options)).toBeLessThanOrEqual(
- options.max
- );
- });
-
- it('should return a random number given a maximum value of 0', () => {
- const options = { max: 0 };
- expect(faker.datatype.float(options)).toBe(0);
- });
-
- it('should return a random number given a negative number minimum and maximum value of 0', () => {
- const options = { min: -100, max: 0 };
- expect(faker.datatype.float(options)).toBeGreaterThanOrEqual(
- options.min
- );
- expect(faker.datatype.float(options)).toBeLessThanOrEqual(
- options.max
- );
- });
-
- it('should return a random number between a range', () => {
- const options = { min: 22, max: 33 };
- for (let i = 0; i < 5; i++) {
- const randomNumber = faker.datatype.float(options);
- expect(randomNumber).toBeGreaterThanOrEqual(options.min);
- expect(randomNumber).toBeLessThanOrEqual(options.max);
- }
- });
-
- it('provides numbers with a given precision', () => {
- const options = { min: 0, max: 1.5, precision: 0.5 };
- const results = [
- ...new Set(
- Array.from({ length: 50 }, () => faker.datatype.float(options))
- ),
- ].sort();
-
- expect(results).toEqual([0, 0.5, 1, 1.5]);
- });
-
- it('provides numbers with a with exact precision', () => {
- const options = { min: 0.5, max: 0.99, precision: 0.01 };
- for (let i = 0; i < 100; i++) {
- const number = faker.datatype.float(options);
- expect(number).toBe(Number(number.toFixed(2)));
- }
- });
-
- it('should not modify the input object', () => {
- const min = 1;
- const max = 2;
- const opts = { min, max };
-
- faker.datatype.float(opts);
-
- expect(opts.min).toBe(min);
- expect(opts.max).toBe(max);
- });
-
- it('should throw when min > max', () => {
- const min = 10;
- const max = 9;
-
- expect(() => {
- faker.datatype.float({ min, max });
- }).toThrow(
- new FakerError(`Max ${max} should be greater than min ${min}.`)
- );
- });
-
- it('should throw when precision <= 0', () => {
- const min = 1;
- const max = 2;
-
- expect(() => {
- faker.datatype.float({ min, max, precision: 0 });
- }).toThrow(
- new FakerError('multipleOf/precision should be greater than 0.')
- );
- expect(() => {
- faker.datatype.float({ min, max, precision: -1 });
- }).toThrow(
- new FakerError('multipleOf/precision should be greater than 0.')
- );
- });
- });
-
- describe('datetime', () => {
- it('check validity of date and if returned value is created by Date()', () => {
- const date = faker.datatype.datetime();
- expect(date).toBeTypeOf('object');
- expect(date.getTime()).not.toBeNaN();
- expect(Object.prototype.toString.call(date)).toBe('[object Date]');
- });
- });
-
- describe('string', () => {
- it('should generate a string value', () => {
- const generatedString = faker.datatype.string();
- expect(generatedString).toBeTypeOf('string');
- expect(generatedString).toHaveLength(10);
- });
-
- it('should return empty string if negative length is passed', () => {
- const negativeValue = faker.datatype.number({ min: -1000, max: -1 });
- const generatedString = faker.datatype.string(negativeValue);
- expect(generatedString).toBe('');
- expect(generatedString).toHaveLength(0);
- });
- });
-
describe('boolean', () => {
it('generates a boolean value', () => {
const bool = faker.datatype.boolean();
@@ -424,160 +74,6 @@ describe('datatype', () => {
expect(() => faker.datatype.boolean(emptyOptions)).not.toThrow();
});
});
-
- describe('UUID', () => {
- it('generates a valid UUID', () => {
- const UUID = faker.datatype.uuid();
- const RFC4122 =
- /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
- expect(UUID).toMatch(RFC4122);
- });
- });
-
- describe('hexadecimal', () => {
- it('generates single hex character when no additional argument was provided', () => {
- const hex = faker.datatype.hexadecimal();
- expect(hex).toMatch(/^(0x)[0-9a-f]{1}$/i);
- expect(hex.substring(2)).toHaveLength(1);
- });
-
- it('generates a hex string with a provided prefix', () => {
- const hex = faker.datatype.hexadecimal({ prefix: '0x' });
- expect(hex).toMatch(/^(0x)[0-9A-F]+$/i);
- expect(hex).toHaveLength(3);
- });
-
- it('generates a random hex string with a provided length', () => {
- const hex = faker.datatype.hexadecimal({ length: 5 });
- expect(hex).toMatch(/^(0x)[0-9a-f]+$/i);
- expect(hex.substring(2)).toHaveLength(5);
- });
-
- it('generates a hex string with a provided casing', () => {
- const hex = faker.datatype.hexadecimal({ case: 'lower' });
- expect(hex).toMatch(/^(0x)[0-9a-f]+$/i);
- expect(hex.substring(2)).toHaveLength(1);
- });
-
- it('generates a hex string with a provided prefix, length, and casing', () => {
- const hex = faker.datatype.hexadecimal({
- prefix: '0x',
- length: 7,
- case: 'upper',
- });
- expect(hex).toMatch(/^(0x)[0-9A-F]+$/i);
- expect(hex.substring(2)).toHaveLength(7);
- });
- });
-
- describe('json', () => {
- it('generates a valid json object', () => {
- const jsonObject = faker.datatype.json();
- expect(jsonObject).toBeTypeOf('string');
- expect(JSON.parse(jsonObject)).toBeTruthy();
- });
- });
-
- describe('array', () => {
- it('generates an array with passed size', () => {
- const randomSize = faker.datatype.number();
- const generatedArray = faker.datatype.array(randomSize);
- expect(generatedArray).toHaveLength(randomSize);
- });
-
- it('generates an array with 0 element', () => {
- const generatedArray = faker.datatype.array(0);
- expect(generatedArray).toHaveLength(0);
- });
-
- it('generates an array with 1 element', () => {
- const generatedArray = faker.datatype.array(1);
- expect(generatedArray).toHaveLength(1);
- });
-
- it('generates an array with length range', () => {
- const generatedArray = faker.datatype.array({ min: 1, max: 5 });
- expect(generatedArray.length).toBeGreaterThanOrEqual(1);
- expect(generatedArray.length).toBeLessThanOrEqual(5);
- });
- });
-
- describe('bigInt', () => {
- it('should generate a bigInt value', () => {
- const generateBigInt = faker.datatype.bigInt();
- expect(generateBigInt).toBeTypeOf('bigint');
- });
-
- it('should generate a big bigInt value with low delta', () => {
- const min = 999999999n;
- const max = 1000000000n;
- const generateBigInt = faker.datatype.bigInt({ min, max });
- expect(generateBigInt).toBeTypeOf('bigint');
- expect(generateBigInt).toBeGreaterThanOrEqual(min);
- expect(generateBigInt).toBeLessThanOrEqual(max);
- });
-
- it('should return a random bigint given a maximum value as BigInt', () => {
- const max = 10n;
- expect(faker.datatype.bigInt(max)).toBeGreaterThanOrEqual(0n);
- expect(faker.datatype.bigInt(max)).toBeLessThanOrEqual(max);
- });
-
- it('should return a random bigint given a maximum value as Object', () => {
- const options = { max: 10n };
- expect(faker.datatype.bigInt(options)).toBeGreaterThanOrEqual(0n);
- expect(faker.datatype.bigInt(options)).toBeLessThanOrEqual(
- options.max
- );
- });
-
- it('should return a random bigint given a maximum value of 0', () => {
- const options = { max: 0n };
- expect(faker.datatype.bigInt(options)).toBe(0n);
- });
-
- it('should return a random bigint given a negative bigint minimum and maximum value of 0', () => {
- const options = { min: -100n, max: 0n };
- expect(faker.datatype.bigInt(options)).toBeGreaterThanOrEqual(
- options.min
- );
- expect(faker.datatype.bigInt(options)).toBeLessThanOrEqual(
- options.max
- );
- });
-
- it('should return a random bigint between a range', () => {
- const options = { min: 22, max: 33 };
- for (let i = 0; i < 100; i++) {
- const randomBigInt = faker.datatype.bigInt(options);
- expect(randomBigInt).toBeGreaterThanOrEqual(options.min);
- expect(randomBigInt).toBeLessThanOrEqual(options.max);
- }
- });
-
- it('should succeed with success-rate', () => {
- const min = 0n;
- const max = 1000000000000n;
- const randomBigInt = faker.datatype.bigInt({ min, max });
- expect(randomBigInt).toBeGreaterThanOrEqual(min);
- expect(randomBigInt).toBeLessThanOrEqual(max);
- });
-
- it('should not mutate the input object', () => {
- const initialMin = 1n;
- const initialOtherProperty = 'hello darkness my old friend';
- const input: {
- min?: bigint;
- max?: bigint;
- otherProperty: string;
- } = Object.freeze({
- min: initialMin,
- otherProperty: initialOtherProperty,
- });
-
- expect(() => faker.datatype.bigInt(input)).not.toThrow();
- });
- });
}
);
});
diff --git a/test/modules/helpers.spec.ts b/test/modules/helpers.spec.ts
index 6163a0d2..c389fa8e 100644
--- a/test/modules/helpers.spec.ts
+++ b/test/modules/helpers.spec.ts
@@ -173,9 +173,9 @@ describe('helpers', () => {
});
t.describe('multiple', (t) => {
- t.it('with only method', faker.datatype.number)
- .it('with method and count', faker.datatype.number, { count: 5 })
- .it('with method and count range', faker.datatype.number, {
+ t.it('with only method', faker.number.int)
+ .it('with method and count', faker.number.int, { count: 5 })
+ .it('with method and count range', faker.number.int, {
count: { min: 1, max: 10 },
});
});