Skip to content

Commit

Permalink
feat: return seed value from seed() (#853)
Browse files Browse the repository at this point in the history
Co-authored-by: ST-DDT <ST-DDT@gmx.de>
  • Loading branch information
Shinigami92 and ST-DDT committed May 1, 2022
1 parent af5606a commit 1851eca
Show file tree
Hide file tree
Showing 25 changed files with 136 additions and 98 deletions.
93 changes: 90 additions & 3 deletions src/faker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Git } from './git';
import { Hacker } from './hacker';
import { Helpers } from './helpers';
import { Image } from './image';
import { deprecated } from './internal/deprecated';
import { Internet } from './internet';
import type { KnownLocale } from './locales';
import { Lorem } from './lorem';
Expand Down Expand Up @@ -49,7 +50,7 @@ export class Faker {

readonly definitions: LocaleDefinition = this.initDefinitions();

seedValue?: number | number[];
private _seedValue: number | number[];

readonly fake: Fake['fake'] = new Fake(this).fake;
readonly unique: Unique['unique'] = new Unique().unique;
Expand Down Expand Up @@ -99,6 +100,24 @@ export class Faker {
this.localeFallback = opts.localeFallback || 'en';
}

/**
* The seed that was last set.
* Please note that generated values are dependent on both the seed and the number of calls that have been made since it was set.
*
* Use the `seed` function to set a new seed.
*
* @deprecated Use the return value of `faker.seed()` instead.
*/
public get seedValue(): number | number[] {
deprecated({
deprecated: 'faker.seedValue',
proposed: 'return value of faker.seed()',
since: '6.3.0',
until: '7.0.0',
});
return this._seedValue;
}

/**
* Creates a Proxy based LocaleDefinition that virtually merges the locales.
*/
Expand Down Expand Up @@ -149,13 +168,81 @@ export class Faker {
});
}

seed(seed?: number | number[]): void {
this.seedValue = seed;
/**
* Sets the seed or generates a new one.
*
* Please note that generated values are dependent on both the seed and the
* number of calls that have been made since it was set.
*
* This method is intended to allow for consistent values in a tests, so you
* might want to use hardcoded values as the seed.
*
* In addition to that it can be used for creating truly random tests
* (by passing no arguments), that still can be reproduced if needed,
* by logging the result and explicitly setting it if needed.
*
* @param seed The seed to use. Defaults to a random number.
* @returns The seed that was set.
*
* @example
* // Consistent values for tests:
* faker.seed(42)
* faker.datatype.number(10); // 4
* faker.datatype.number(10); // 8
*
* faker.seed(42)
* faker.datatype.number(10); // 4
* faker.datatype.number(10); // 8
*
* @example
* // Random but reproducible tests:
* // Simply log the seed, and if you need to reproduce it, insert the seed here
* console.log('Running test with seed:', faker.seed());
*/
seed(seed?: number): number;
/**
* Sets the seed array.
*
* Please note that generated values are dependent on both the seed and the
* number of calls that have been made since it was set.
*
* This method is intended to allow for consistent values in a tests, so you
* might want to use hardcoded values as the seed.
*
* In addition to that it can be used for creating truly random tests
* (by passing no arguments), that still can be reproduced if needed,
* by logging the result and explicitly setting it if needed.
*
* @param seedArray The seed array to use.
* @returns The seed array that was set.
*
* @example
* // Consistent values for tests:
* faker.seed([42, 13, 17])
* faker.datatype.number(10); // 4
* faker.datatype.number(10); // 8
*
* faker.seed([42, 13, 17])
* faker.datatype.number(10); // 4
* faker.datatype.number(10); // 8
*
* @example
* // Random but reproducible tests:
* // Simply log the seed, and if you need to reproduce it, insert the seed here
* console.log('Running test with seed:', faker.seed());
*/
seed(seedArray: number[]): number[];
seed(
seed: number | number[] = Math.ceil(Math.random() * Number.MAX_SAFE_INTEGER)
): number | number[] {
this._seedValue = seed;
if (Array.isArray(seed) && seed.length) {
this.mersenne.seed_array(seed);
} else if (!Array.isArray(seed) && !isNaN(seed)) {
this.mersenne.seed(seed);
}

return seed;
}

/**
Expand Down
5 changes: 1 addition & 4 deletions test/address.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,8 @@ describe('address', () => {
});
}

// Create and log-back the seed for debug purposes
faker.seed(Math.ceil(Math.random() * 1_000_000_000));

describe(`random seeded tests for seed ${JSON.stringify(
faker.seedValue
faker.seed()
)}`, () => {
for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) {
describe('countryCode()', () => {
Expand Down
5 changes: 1 addition & 4 deletions test/animal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,8 @@ describe('animal', () => {
});
}

// Create and log-back the seed for debug purposes
faker.seed(Math.ceil(Math.random() * 1_000_000_000));

describe(`random seeded tests for seed ${JSON.stringify(
faker.seedValue
faker.seed()
)}`, () => {
for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) {
for (const functionName of functionNames) {
Expand Down
5 changes: 1 addition & 4 deletions test/commerce.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,8 @@ describe('commerce', () => {
});
}

// Create and log-back the seed for debug purposes
faker.seed(Math.ceil(Math.random() * 1_000_000_000));

describe(`random seeded tests for seed ${JSON.stringify(
faker.seedValue
faker.seed()
)}`, () => {
for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) {
describe(`color()`, () => {
Expand Down
5 changes: 1 addition & 4 deletions test/company.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,8 @@ describe('company', () => {
});
}

// Create and log-back the seed for debug purposes
faker.seed(Math.ceil(Math.random() * 1_000_000_000));

describe(`random seeded tests for seed ${JSON.stringify(
faker.seedValue
faker.seed()
)}`, () => {
for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) {
describe('suffixes()', () => {
Expand Down
5 changes: 1 addition & 4 deletions test/database.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,8 @@ describe('database', () => {
});
}

// Create and log-back the seed for debug purposes
faker.seed(Math.ceil(Math.random() * 1_000_000_000));

describe(`random seeded tests for seed ${JSON.stringify(
faker.seedValue
faker.seed()
)}`, () => {
for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) {
describe('column()', () => {
Expand Down
5 changes: 1 addition & 4 deletions test/datatype.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,11 +421,8 @@ describe('datatype', () => {
});
}

// Create and log-back the seed for debug purposes
faker.seed(Math.ceil(Math.random() * 1_000_000_000));

describe(`random seeded tests for seed ${JSON.stringify(
faker.seedValue
faker.seed()
)}`, () => {
for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) {
describe('number', () => {
Expand Down
5 changes: 1 addition & 4 deletions test/date.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,11 +364,8 @@ describe('date', () => {
});
}

// Create and log-back the seed for debug purposes
faker.seed(Math.ceil(Math.random() * 1_000_000_000));

describe(`random seeded tests for seed ${JSON.stringify(
faker.seedValue
faker.seed()
)}`, () => {
for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) {
describe('past()', () => {
Expand Down
22 changes: 22 additions & 0 deletions test/faker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,28 @@ describe('faker', () => {
// This is only here for coverage
// The actual test is in mersenne.spec.ts
describe('seed()', () => {
it('seed()', () => {
const seed = faker.seed();

expect(seed).toBeDefined();
expect(seed).toBeTypeOf('number');
});

it('should reset the sequence when calling `seed`', () => {
const seed = faker.seed();

const num1 = faker.datatype.number();

const newSeed = faker.seed(seed);
const num2 = faker.datatype.number();

expect(num1).toBe(num2);
expect(newSeed).toBe(seed);

const num3 = faker.datatype.number();
expect(num1).not.toBe(num3);
});

it('seed(number)', () => {
faker.seed(1);

Expand Down
9 changes: 2 additions & 7 deletions test/finance.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,8 @@ describe('finance', () => {
});
}

// Create and log-back the seed for debug purposes
faker.seed(Math.ceil(Math.random() * 1_000_000_000));

describe(`random seeded tests for seed ${JSON.stringify(
faker.seedValue
faker.seed()
)}`, () => {
for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) {
describe('account()', () => {
Expand Down Expand Up @@ -395,9 +392,7 @@ describe('finance', () => {
});

it('should ignore case for issuer', () => {
const seed = faker.seedValue;

faker.seed(seed);
const seed = faker.seed();
const actualNonLowerCase = faker.finance.creditCardNumber('ViSa');

faker.seed(seed);
Expand Down
5 changes: 1 addition & 4 deletions test/finance_iban.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import ibanLib from '../src/utils/iban';
const NON_SEEDED_BASED_RUN = 25;

describe('finance_iban', () => {
// Create and log-back the seed for debug purposes
faker.seed(Math.ceil(Math.random() * 1_000_000_000));

describe('generic IBAN country checks', () => {
it.each(ibanLib.formats.map((entry) => entry.country))('%s', (country) => {
expect(country).toMatch(/^[A-Z]{2}$/);
Expand All @@ -20,7 +17,7 @@ describe('finance_iban', () => {
});

describe(`random seeded tests for seed ${JSON.stringify(
faker.seedValue
faker.seed()
)}`, () => {
for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) {
describe('specific IBAN country checks', () => {
Expand Down
5 changes: 1 addition & 4 deletions test/git.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,8 @@ describe('git', () => {
});
}

// Create and log-back the seed for debug purposes
faker.seed(Math.ceil(Math.random() * 1_000_000_000));

describe(`random seeded tests for seed ${JSON.stringify(
faker.seedValue
faker.seed()
)}`, () => {
for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) {
describe('branch()', () => {
Expand Down
5 changes: 1 addition & 4 deletions test/hacker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,8 @@ describe('name', () => {
});
}

// Create and log-back the seed for debug purposes
faker.seed(Math.ceil(Math.random() * 1_000_000_000));

describe(`random seeded tests for seed ${JSON.stringify(
faker.seedValue
faker.seed()
)}`, () => {
for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) {
describe('abbreviation()', () => {
Expand Down
5 changes: 1 addition & 4 deletions test/helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,11 +490,8 @@ describe('helpers', () => {
});
}

// Create and log-back the seed for debug purposes
faker.seed(Math.ceil(Math.random() * 1_000_000_000));

describe(`random seeded tests for seed ${JSON.stringify(
faker.seedValue
faker.seed()
)}`, () => {
for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) {
describe('randomize()', () => {
Expand Down
5 changes: 1 addition & 4 deletions test/internet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,8 @@ describe('internet', () => {
});
}

// Create and log-back the seed for debug purposes
faker.seed(Math.ceil(Math.random() * 1_000_000_000));

describe(`random seeded tests for seed ${JSON.stringify(
faker.seedValue
faker.seed()
)}`, () => {
for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) {
describe('avatar', () => {
Expand Down
5 changes: 1 addition & 4 deletions test/lorem.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,8 @@ describe('lorem', () => {
});
}

// Create and log-back the seed for debug purposes
faker.seed(Math.ceil(Math.random() * 1_000_000_000));

describe(`random seeded tests for seed ${JSON.stringify(
faker.seedValue
faker.seed()
)}`, () => {
for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) {
describe('word()', () => {
Expand Down
5 changes: 1 addition & 4 deletions test/music.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,8 @@ describe('music', () => {
});
}

// Create and log-back the seed for debug purposes
faker.seed(Math.ceil(Math.random() * 1_000_000_000));

describe(`random seeded tests for seed ${JSON.stringify(
faker.seedValue
faker.seed()
)}`, () => {
for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) {
describe('genre()', () => {
Expand Down
5 changes: 1 addition & 4 deletions test/name.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,8 @@ describe('name', () => {
});
}

// Create and log-back the seed for debug purposes
faker.seed(Math.ceil(Math.random() * 1_000_000_000));

describe(`random seeded tests for seed ${JSON.stringify(
faker.seedValue
faker.seed()
)}`, () => {
for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) {
describe('firstName()', () => {
Expand Down
5 changes: 1 addition & 4 deletions test/phone.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,8 @@ describe('phone', () => {
});
}

// Create and log-back the seed for debug purposes
faker.seed(Math.ceil(Math.random() * 1_000_000_000));

describe(`random seeded tests for seed ${JSON.stringify(
faker.seedValue
faker.seed()
)}`, () => {
for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) {
describe('phoneNumber()', () => {
Expand Down

0 comments on commit 1851eca

Please sign in to comment.