aboutsummaryrefslogtreecommitdiff
path: root/test/support/seeded-runs.ts
blob: 04bbd2546cec01b1295cbe5c54bc65ed02f75ce4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import { describe, expect, describe as vi_describe, it as vi_it } from 'vitest';
import type { Faker } from '../../src/faker';
import type { Callable, MethodOf } from '../../src/internal/types';

export const seededRuns = [42, 1337, 1211];

/**
 * A type allowing only the names of faker modules.
 */
type FakerModule = {
  [Key in keyof Faker]: Faker[Key] extends Callable | string | number | number[]
    ? never
    : Key extends 'definitions' | 'locales'
      ? never
      : Key;
}[keyof Faker];

/**
 * Picks only the methods from the given type.
 */
type OnlyMethods<T> = Pick<T, MethodOf<T>>;

/**
 * A Faker type with modules trimmed to only methods.
 */
type OnlyMethodsFaker = {
  [Key in FakerModule]: OnlyMethods<Faker[Key]>;
};

/**
 * The type allowing only the names of methods that have exactly zero arguments.
 */
type NoArgsMethodOf<TObjectType> = MethodOf<TObjectType> &
  {
    [Key in MethodOf<TObjectType, () => unknown>]: TObjectType[Key] extends (
      arg0: string | number | boolean | Record<string, undefined>,
      ...args: unknown[]
    ) => unknown
      ? Key
      : never;
  }[MethodOf<TObjectType, () => unknown>];

/**
 * Method that prepares seeded tests.
 *
 * It ensures that all methods in that module have exactly one test case or block associated to them.
 * Duplicate calls to `t.it(methodName)` or `t.describe(methodName)` will directly throw an error.
 * Before the method returns it will check that there are tests for all methods of the module.
 *
 * You may add custom vitest's `it` and `describe` blocks both on a module and method level,
 * however these will be ignored by the completeness checks and you have to call the `setup()` callback yourself.
 *
 * @param faker The faker instance to use for the tests.
 * @param module The name of the faker module to test.
 * @param factory The factory used to create the seeded tests. Supports both fluent and individual calls.
 *
 * @example
 * seededTests(faker, 'random', (t) => {
 *  t.it('methodWithoutArgs')
 *    .itRepeated('methodWithoutArgs2', 5)
 *    .describe('methodWithArgs3', (t) => {
 *      t.it('noArgs')
 *        .it('withParam1', 1337)
 *        .it('variant1', { min: 0})
 *        .it('variant2', { max: 1337})
 *        .it('variant1And2', { min: 0, max: 1337})
 *    });
 * })
 */
export function seededTests<
  TFakerModule extends FakerModule,
  TModule extends Record<string, Callable> = OnlyMethodsFaker[TFakerModule],
>(
  faker: Faker,
  module: TFakerModule,
  factory: (tg: TestGenerator<TFakerModule, TModule>, setup: () => void) => void
): void {
  describe.each(seededRuns)('%s', (seed) => {
    const testGenerator: TestGenerator<TFakerModule, TModule> =
      new TestGenerator(faker, seed, module);
    factory(testGenerator, () => testGenerator.setup());

    testGenerator.expectAllMethodsToBeTested();
  });
}

/**
 * Generator for seed based tests.
 *
 * The individual methods generate default test blocks, that use test snapshots to verify consistent return values.
 */
class TestGenerator<
  TModuleName extends FakerModule,
  TModule extends Record<string, Callable> = OnlyMethodsFaker[TModuleName],
> {
  private readonly tested: Set<MethodOf<TModule>> = new Set();
  private readonly module: TModule;

  constructor(
    private readonly faker: Faker,
    private readonly seed: number,
    private readonly moduleName: TModuleName
  ) {
    this.module = this.faker[moduleName] as unknown as TModule;
  }

  /**
   * Ensures that there is only one test block for each method.
   *
   * @param method The method name to check.
   */
  private expectNotTested(method: MethodOf<TModule>): void {
    expect(
      this.tested.has(method),
      `${method} not to be tested yet`
    ).toBeFalsy();
    this.tested.add(method);
  }

  /**
   * Should never be called from tests.
   *
   * Configures the faker instance for the test by resetting the seed.
   *
   * This method will automatically be called by the default methods
   * and should be called at the beginning of custom vitest's `it` blocks.
   */
  setup(): void {
    this.faker.seed(this.seed);
  }

  /**
   * Runs the instructions for a vitest's `it` block.
   *
   * @param method The method name to call.
   * @param args The arguments to call it with.
   * @param extraStackFrames Additional stack frames to add into the stacktrace.
   * @param repetitions The number of times to call it.
   */
  private callAndVerify<TMethodName extends MethodOf<TModule>>(
    method: TMethodName,
    args: Parameters<TModule[TMethodName]>,
    extraStackFrames: () => string[],
    repetitions: number = 1
  ): void {
    this.setup();
    const callable = this.module[method];
    if (callable == null) {
      throw new Error(`Method ${method} not found in ${this.moduleName}`);
    }

    for (let i = 0; i < repetitions; i++) {
      try {
        const value = callable(...args);
        expect(value).toMatchSnapshot();
      } catch (error: unknown) {
        throw patchExtraStackFrames(error, extraStackFrames);
      }
    }
  }

  /**
   * Permanently ignores this method.
   *
   * @param method The name of the method.
   */
  skip(method: MethodOf<TModule>): this {
    this.expectNotTested(method);
    vi_it.skip(method);
    return this;
  }

  /**
   * Generates a test for a method without arguments.
   *
   * @param method The name of the method.
   */
  it(method: NoArgsMethodOf<TModule>): this {
    return this.itRepeated(method, 1);
  }

  /**
   * Generates a repeated test for a method without arguments.
   * The seed is not reset between repetitions.
   *
   * @param method The name of the method.
   * @param repetitions The number of repetitions to run.
   */
  itRepeated(method: NoArgsMethodOf<TModule>, repetitions: number): this {
    this.expectNotTested(method);
    const extraStackFrames = collectExtraStackFrames();
    vi_it(method, () =>
      this.callAndVerify(
        method,
        [] as unknown as Parameters<TModule[NoArgsMethodOf<TModule>]>,
        extraStackFrames,
        repetitions
      )
    );
    return this;
  }

  /**
   * Generates no argument tests for the given methods.
   *
   * @param methods The names of the methods.
   */
  itEach(...methods: Array<NoArgsMethodOf<TModule>>): this {
    for (const method of methods) {
      this.it(method);
    }

    return this;
  }

  /**
   * Generates a test section for a method.
   * Useful to cover multiple argument variations.
   *
   * @param method The name of the method.
   * @param factory The factory used to generate the individual tests.
   */
  describe<TMethodName extends MethodOf<TModule>>(
    method: TMethodName,
    factory: (tester: MethodTester<TModule[TMethodName]>) => void
  ): this {
    this.expectNotTested(method);
    const callAndVerify: TestGenerator<TModuleName, TModule>['callAndVerify'] =
      this.callAndVerify.bind(this);
    const variantNames = new Set<string>();
    const expectVariantNotTested = (name: string): void => {
      expect(
        variantNames.has(name),
        `${name} test to be unique for ${method}`
      ).toBeFalsy();
      variantNames.add(name);
    };

    const tester: MethodTester<TModule[TMethodName]> = {
      it(name: string, ...args: Parameters<TModule[TMethodName]>) {
        expectVariantNotTested(name);
        const extraStackFrames = collectExtraStackFrames(
          /* t. */ `it('${name}', `.length // ...args)
        );
        vi_it(name, () => callAndVerify(method, args, extraStackFrames));
        return tester;
      },
      itRepeated(
        name: string,
        repetitions: number,
        ...args: Parameters<TModule[TMethodName]>
      ) {
        expectVariantNotTested(name);
        const extraStackFrames = collectExtraStackFrames(
          /* t. */ `itRepeated('${name}', ${repetitions}, `.length // ...args)
        );
        vi_it(name, () =>
          callAndVerify(method, args, extraStackFrames, repetitions)
        );
        return tester;
      },
    };
    vi_describe(method, () => {
      factory(tester);
    });
    return this;
  }

  /**
   * Generates a test section for multiple methods with a similar signature.
   * Useful to cover multiple argument variations.
   *
   * @param methods The names of the methods to generate the tests for.
   */
  describeEach<TMethodName extends MethodOf<TModule>>(
    ...methods: TMethodName[]
  ): (factory: (tester: MethodTester<TModule[TMethodName]>) => void) => this {
    return (factory) => {
      for (const method of methods) {
        this.describe(method, factory);
      }

      return this;
    };
  }

  /**
   * Should never be called from tests.
   *
   * Checks that all methods in the module have associated tests.
   * This method will be called automatically at the end of each run.
   */
  expectAllMethodsToBeTested(): void {
    const actual = [...this.tested].sort();
    const expected = Object.entries(this.module)
      .filter(([, value]) => typeof value === 'function')
      .map(([key]) => key)
      .sort();
    vi_it('should test all methods', () => {
      expect(actual).toEqual(expected);
    });
  }
}

/**
 * Lazily collects the current call stack with an additional offset.
 *
 * Vitest's stacktraces only contain the stacktrace from inside `it(name, () => { here })`.
 * This method collects the location where the `it` block is created instead of executed.
 * The stack frames can then later be added to the error stack to provide a more accurate location.
 *
 * @param extraOffset The additional offset to add to the column numbers to account for the name of the test.
 */
function collectExtraStackFrames(extraOffset: number = 0): () => string[] {
  const stack = new Error('collect').stack;
  if (stack == null) {
    return () => [];
  }

  return () =>
    stack
      .split('\n')
      .map((e) => e.replaceAll('\\', '/')) // Windows to Linux paths
      .filter((e) => e.includes('/test/')) // exclude node_modules
      .filter((e) => !e.includes('/test/support/')) // exclude this file
      .map((e) =>
        e.replace(/:(\d+)$/, (_, column: string) => `:${+column + extraOffset}`)
      );
}

/**
 * Modifies the error stack to include the given additional stack frames after the last occurrence of this file.
 *
 * @param error The error to modify.
 * @param extraStackFrames The additional stack frames to add.
 */
function patchExtraStackFrames(
  error: unknown,
  extraStackFrames: () => string[]
): unknown {
  if (error instanceof Error && error.stack != null) {
    const stack = error.stack.split('\n');
    const index = stack.findLastIndex((e) =>
      e
        .replaceAll('\\', '/') // Windows to Linux paths
        .includes('/test/support/')
    );
    stack.splice(index + 1, 0, ...extraStackFrames());
    error.stack = stack.join('\n');
  }

  return error;
}

/**
 * Simple interface for a test generator for a given method.
 */
interface MethodTester<TMethod extends Callable> {
  /**
   * Generates a test for the method.
   *
   * @param name The name of the test case.
   * @param args The arguments to use in the test.
   */
  it(name: string, ...args: Parameters<TMethod>): this;

  /**
   * Generates a repeated test for the method.
   * The seed is not reset between repetitions.
   *
   * @param name The name of the test case.
   * @param repetitions The number of repetitions to run.
   * @param args The arguments to use in the test.
   */
  itRepeated(
    name: string,
    repetitions: number,
    ...args: Parameters<TMethod>
  ): this;
}