aboutsummaryrefslogtreecommitdiff
path: root/test/require.spec.cts
blob: 80f708d94b7e400457efced58148d93f14abc9bf (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
const { describe, expect, it, vi } = await import('vitest');
const { allLocales, SimpleFaker } = require('../dist/index.cjs');

describe('require (cjs)', () => {
  describe.each(
    Object.keys(
      // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
      allLocales
    )
  )('locale imports', (locale) => {
    it(`should be possible to directly require('@faker-js/faker/locale/${locale}')`, () => {
      const { faker } = require(`../dist/locale/${locale}.cjs`);

      expect(faker).toBeDefined();
      expect(faker.string.alpha()).toBeTypeOf('string');
      expect(faker.definitions.metadata.title).toBe(
        allLocales[locale].metadata?.title
      );
    });
  });

  describe('simpleFaker', () => {
    it('should not log anything on startup', () => {
      const spies = Object.keys(console)
        .filter(
          (key) =>
            // @ts-expect-error: cts cant use `as keyof typeof console`
            typeof console[key] === 'function'
        )
        .map((methodName) =>
          vi.spyOn(
            console,
            // @ts-expect-error: cts cant use `as keyof typeof console`
            methodName
          )
        );

      expect(require('..').simpleFaker).toBeDefined();

      expect(new SimpleFaker()).toBeDefined();

      for (const spy of spies) {
        expect(spy).not.toHaveBeenCalled();
        spy.mockRestore();
      }
    });
  });
});